Selection Sort with iteration wise per step


package com.kartik.sorting;
/**
 *
 *
 * @author MandalKC
 *
 */
public class SelectionSort {
 /**
  *
  * @param arr
  * @return
  */
 public static int[] doSelectionSort(int[] arr) {
  System.out.println("Before Selection Soring --->>");
  printNumbers(arr);
  System.out.println("After Selection Soring start--->>");
  for (int i = 0; i < arr.length - 1; i++) {
   int index = i;
   for (int j = i + 1; j < arr.length; j++)
    if (arr[j] < arr[index])
     index = j;
   int smallerNumber = arr[index];
   arr[index] = arr[i];
   arr[i] = smallerNumber;
   printNumbers(arr);
  }
  return arr;
 }
 /**
  *
  * @param input
  */
 private static void printNumbers(int[] input) {
  for (int i = 0; i < input.length; i++) {
   System.out.print(input[i] + ", ");
  }
  System.out.println("\n");
 }
 /**
  *
  * @param a
  */
 public static void main(String a[]) {
  int[] arr1 = { 10, 34, 2, 56, 7, 67, 88, 42 };
  doSelectionSort(arr1);
  // int[] arr2 = doSelectionSort(arr1);
  /*
   * for(int i:arr2){ System.out.print(i); System.out.print(", "); }
   */
 }
}


Out Put:

Before Selection Soring --->>


10, 34, 2, 56, 7, 67, 88, 42,





After Selection Soring start--->>


2, 34, 10, 56, 7, 67, 88, 42,





2, 7, 10, 56, 34, 67, 88, 42,





2, 7, 10, 56, 34, 67, 88, 42,





2, 7, 10, 34, 56, 67, 88, 42,





2, 7, 10, 34, 42, 67, 88, 56,





2, 7, 10, 34, 42, 56, 88, 67,





2, 7, 10, 34, 42, 56, 67, 88,
Previous
Next Post »