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]+" ");
}
}

}
******************************************************************************

Comments

Popular posts from this blog

Missionaries & Canibal Problem in AI using Pro Log

Hide the navigation bar in jHipster Angular 2

Spring Boot - No Need to Restart the Development Server for Each Time