Sorting: Sorting is the most basic program required by many software.Sorting means to sort the list of given elements in format as required by the user.Sorting has many application areas,sorting does not mean sorting in increasing or decreasing order but as according to the requirement of the program
Here is the source code of the program in Java to sort the elements in increasing order.
Java Sorting program
import java.util.Scanner;
class sorting
{
public static void main(String args[])
{
int a,b,c,temp,i;
Scanner s=new Scanner(System.in);
System.out.println("\n\t\t\t*** SORTING IN JAVA ***\n");
System.out.println("Enter the no of elements");
int n=s.nextInt();
int []pr=new int[n];
System.out.println("Enter the list");
for( i=0;i<pr.length;i++)
{
pr[i]=s.nextInt();
}
for(i=0;i<n-1;i++)
{
for(int k=i+1;k<n;k++)
{
if(pr[i]>pr[k])
{
temp=pr[i];
pr[i]=pr[k];
pr[k]=temp;
}
}
}
System.out.println("The sorted list is");
for( i=0;i<n;i++)
{
System.out.println(pr[i]+"\t");
}
}
}
Output of the program is:
Here is the source code of the program in Java to sort the elements in increasing order.
Java Sorting program
import java.util.Scanner;
class sorting
{
public static void main(String args[])
{
int a,b,c,temp,i;
Scanner s=new Scanner(System.in);
System.out.println("\n\t\t\t*** SORTING IN JAVA ***\n");
System.out.println("Enter the no of elements");
int n=s.nextInt();
int []pr=new int[n];
System.out.println("Enter the list");
for( i=0;i<pr.length;i++)
{
pr[i]=s.nextInt();
}
for(i=0;i<n-1;i++)
{
for(int k=i+1;k<n;k++)
{
if(pr[i]>pr[k])
{
temp=pr[i];
pr[i]=pr[k];
pr[k]=temp;
}
}
}
System.out.println("The sorted list is");
for( i=0;i<n;i++)
{
System.out.println(pr[i]+"\t");
}
}
}
Output of the program is:
0 comments: