Sequential Sort
In this technique, each element of an array is compared with all of the rest of the elements and swapped if needed.
import java.util.*;
class SequentialSort{
public static void main(String args[])throws InputMismatchException{
Scanner scan=new Scanner(System.in);
int n,i,j,t;
System.out.println("Enter the number of elements : ");
n=scan.nextInt();
int a[]=new int[n]; //declare array
System.out.println("Enter "+n+" elements.");
for(i=0;i < n;i++)
a[i]=scan.nextInt();
//sort the array in ascending order
for(i=0;i< n;i++){
for(j=i+1;j< n;j++){
if(a[i]>a[j]){
t=a[i];
a[i]=a[j];
a[j]=t;
}//end of if
}//end of inner loop
}//end of outer loop
//display the sorted array
for(i=0;i< n;i++){
System.out.println(a[i]+" ");
}//end of loop
}//end of main
}//end of class
Have something to say? Log in to comment on this post.
0 comments