fork download
  1.  
  2. class SelectionSortA{
  3. public static void selectionSort(int[] a){
  4.  
  5. //STEP 01
  6.  
  7. int min=a[0];
  8. for (int i = 0; i < a.length - 1; i++){
  9.  
  10. //STEP 02
  11. int index = i;
  12. for (int j = i + 1; j < a.length; j++){
  13. if (a[j] < a[index]){
  14. index = j;//min index serch
  15.  
  16. }
  17.  
  18. }
  19. //STEP 03
  20. min = a[index];
  21. a[index] = a[i];
  22. a[i] = min;
  23.  
  24. }
  25. }
  26.  
  27. public static void main(String[] args){
  28. int[] b = {9,14,3,2,43,11,58,22};
  29. System.out.println("Before Selection Sort");
  30. for(int i:b){
  31. System.out.print(i+" ");
  32. }
  33. System.out.println();
  34.  
  35. selectionSort(b);//method call(selection sorting array)
  36.  
  37. System.out.println("After Selection Sort");
  38. for(int i:b){
  39. System.out.print(i+" ");
  40. }
  41. }
  42. }
Success #stdin #stdout 0.05s 2184192KB
stdin
Standard input is empty
stdout
Before Selection Sort
9 14 3 2 43 11 58 22 
After Selection Sort
2 3 9 11 14 22 43 58