import java.util.*;
//from Ratan
public class Main {
public static boolean main(String[] args) {
int[] arr = {10,14,67,1,0,78,3,33,5,111,543,5,100,6,89};
System.out.println("Selection Sort: lenght of array: " + arr.length);
int pass=0;
//find the minimum in the right and replace with current position is current position is greter
for(int i=0;i < arr.length;i++)
{
int tempMin=99999999;
int minIndex=i; // assume initial element is the smallest element // imp
for(int j= i+1 ;j<arr.length;j++)
{
if(arr[j] < tempMin)
{
tempMin= arr[j];
minIndex=j; // saving index
}
}// end of this loop will have the minimum value from current element to end of the array
System.out.println("tempMin:" + tempMin);
if(arr[i] > tempMin)
{
//swap
System.out.println("swaaping :" + arr[i] + "and" + tempMin);
int temp = arr[i];
arr[i]=arr[minIndex];
arr[minIndex] =temp;
}
System.out.println(" ");
System.out.print("Pass:"+ i + ":: ");
for(int c : arr)
{
System.out.print(","+c);
}
}
return true;
}
}
// search the min element from next position to end
//.. if current element is small than min then swap.
// imp save the index of min-element(searched) for the swapping.
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.