# Field Manual for Hackers

## Java | Sort the array in descending order.

1. import java.util.Scanner;

class Test{
public static void main(String a[]){
int ar[]=new int[100];
int number;
System.out.print("Enter the number of elements: ");
Scanner o=new Scanner(System.in);
number=o.nextInt();
System.out.println("Now enter the values of array: ");
for(int i=0;i<number;i++){
ar[i]=o.nextInt();
}
for(int k=0;k<number;k++){
for(int i=0;i<number-k;i++){
if(ar[i]<ar[i+1]){
ar[i]+=ar[i+1];
ar[i+1]=ar[i]-ar[i+1];
ar[i]-=ar[i+1];
}
}
}
System.out.println("The sorted array is: ");
for(int k=0;k<number;k++)
System.out.print("\t"+ar[k]);
}
}

OUTPUT
Enter the number of elements: 8
Now enter the values of array:
12 78 65 34 95 40 51 32
The sorted array is:
95 78 65 51 40 34 32 12

2. This comment has been removed by the author.

3. This comment has been removed by the author.

4. This comment has been removed by the author.

5. import java.util.Scanner;
class Descending
{
public static void main(String args[])
{
int temp;
Scanner scan=new Scanner(System.in);
System.out.println("How many number you want to add");
int n=scan.nextInt();
int ar[]=new int[n];
System.out.println("Enter Numbers");
for (int i=0;iar[i])
{
temp=ar[j];
ar[j]=ar[i];
ar[i]=temp;
}
}
}
System.out.println("Descending order List");
for (int i=0;i<n;i++)
{
System.out.print(ar[i]+" ");
}
}
}

6. import java.util.Scanner;
class D
{
public static void main(String args[])
{
Scanner obj=new Scanner(System.in);
int n;
System.out.println("Enter the value of n");
n=obj.nextInt();
int arr[]=new int[n];
int temp,i,j;
System.out.println("Enter the array");
for(i=0;i<n;i++)
{
arr[i]=obj.nextInt();
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(arr[i]<arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
System.out.println("The sorted array in desecending order is=");
for(i=0;i<n;i++)
{
System.out.println(arr[i]);
}
}
}

7. This comment has been removed by the author.

8. import java.util.Scanner;

class sort
{
public static void main(String args[])

{
int temp;
Scanner scan=new Scanner(System.in);
System.out.println("How many number you want to add");
int n=scan.nextInt();
int ar[]=new int[n];
System.out.println("Enter elements in array");
for (int i=0;iar[j])
{
temp=ar[i];
ar[i]=ar[j];
ar[j]=temp;
}
}
}
System.out.println("New array after sorting ");
for (int i=0;i<n;i++)
{
System.out.print(ar[i]+" ");
}
}
}

OUTPUT:

How many number you want to add
5
Enter elements in array
12
65
87
4
65
New array after sorting
87 65 65 12 4

9. import java.util.Scanner;
class Sort
{
public static void main(String Args[])
{
System.out.println("How many numbers you want to enter?");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int temp,i,j;
int AR[]=new int[n];
System.out.println("enter the values");
for( i=0;i<n;i++)
{
AR[i]=sc.nextInt();
}
for(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(AR[i]<AR[j])
{
temp=AR[i];
AR[i]=AR[j];
AR[j]=temp;
}
}
}
System.out.println("sorting array:");
for(i=0;i<n;i++)
{
System.out.println(AR[i]+"");

}
}
}

10. import java.util.Scanner;
class Sort1{
public static void main(String ar[]){
Scanner obj=new Scanner(System.in);
int n,i,j,flag;
System.out.println("enter the value of no. of elements");
n=obj.nextInt();
int arr[]=new int[n];
for(i=0;i<n;i++){
System.out.println("enter the element"+(i+1));
arr[i]=obj.nextInt();
}
for(i=0;i<n;i++){
for(j=i+1;j<n;j++){
if(arr[i]<arr[j]){
flag=arr[i];
arr[i]=arr[j];
arr[j]=flag;
}
}
}
for(i=0;i<n;i++)
System.out.println(arr[i]);
}

}

11. import java.util.Scanner;
class Descend{
public static void main(String ar[]){

int temp,i=0,j=0;
Scanner scan=new Scanner(System.in);
int arr[]=new int[10];
System.out.println("Enter the size:");
int num=scan.nextInt();
System.out.println("Enter the elements:");
for(i=0;i<num;i++){
arr[i]=scan.nextInt();
}
for(i=0;i<num;i++){
for(j=0;j<num;j++){
if(arr[j]<arr[j+1]){
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
System.out.println("array is:"+arr[j]);
}
}
}
for(i=0;i<num;i++){
System.out.println("The array in descending order is:"+arr[i]);
}
}
}

12. import java.util.Scanner;
class Sdecending{
public static void main(String args[]){
int size,i=0,temp=0,j=0;
Scanner scan=new Scanner(System.in);
System.out.println("enter the size of array");
size=scan.nextInt();
int ar[]=new int[size];
for(i=0;i<size;i++){
System.out.println("enter the value"+(i+1));
ar[i]=scan.nextInt();
}
System.out.println("Array :");
for(i=0;i<size;i++){
System.out.printf(ar[i]+" ");
}
for(i=0;i<size;i++)
{
for(j=i+1;j<size;j++)
{
if(ar[i]<ar[j]){
temp=ar[i];
ar[i]=ar[j];
ar[j]=temp;
}
}

}
System.out.println("Sorted array:");
for(i=0;i<size;i++)
System.out.println(+ar[i]);
}
}

13. This comment has been removed by the author.

14. import java.util.Scanner;
class Array5{
public static void main(String ar[]){
int temp,i=0,j=0,min,n;
Scanner s=new Scanner(System.in);
System.out.println("Enter the size:");
n=s.nextInt();
int a[]=new int[n];
System.out.println("Enter the elements:");
for(i=0;i0;i--)
{
min=0;
for(j=1;j<=i;j++)
{
if(a[j]<a[min])
min=j;
}
temp=a[min];
a[min]=a[i];
a[i]=temp;
}

System.out.println("descending order: ");
for(i=0;i<n;i++)
{
System.out.println(a[i]);
}
}
}