Tuesday, November 20, 2012

Selection Sort

public class selectionSorting
{
  public static void main(String a[])
{
  int i;
  int array[] = {12,9,4,1,3,10,5};
  System.out.println("Values Before the sort:\n"); 
  for(i = 0; i < array.length; i++)
  System.out.print( array[i]+"  ");
  System.out.println();
  selection_srt(array, array.length); 
  System.out.print("Values after the sort:\n"); 
  for(i = 0; i <array.length; i++)
  System.out.print(array[i]+"  ");
  System.out.println();

  }

  public static void selection_srt(int array[], int n)
{
  for(int x=0; x<n; x++)
{
  int index_of_minimum = x;
  for(int y=x; y<n; y++)
{
  if(array[index_of_minimum]<array[y])
{
  index_of_minimum = y;
  }
  }
  int temp = array[x];
  array[x] = array[index_of_minimum];
  array[index_of_minimum] = temp;
  }
  }
}

No comments: