Selection Sort

  these picture shows that the algorithm of the selection sort the code is below in (java)
******************************************************************
public class selectionSort{
public void sort(int[] A){
for(int i=0; i<A.length; i++){
int minIndex =i;
for(int j=i; j<A.length; j++){
if(A[minIndex] > A[j]){
minIndex = j;
}
}
   // Swapping the minIndex Element and first element 
int temp = A[i];
A[i] = A[minIndex];
A[minIndex] = temp;
}
}
}
******************************************************************

public class selectionSortDemo{
public static void main(String args[]){
int A[] = {7,3,2,5,1};
selectionSort S = new selectionSort();
S.sort(A);
for(int i=0; i<A.length; i++){
System.out.print(A[i]+” “);
}
}

}

******************************************************************************
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s