Posted By

jsmith on 01/10/12


Tagged

sort java algorithm sort-algorithm selection-sort


Versions (?)

Selection sort


 / Published in: Java
 

  1. public void selectionSort(int[] arr) {
  2. int i, j, minIndex, tmp;
  3. int n = arr.length;
  4. for (i = 0; i < n - 1; i++) {
  5. minIndex = i;
  6. for (j = i + 1; j < n; j++)
  7. if (arr[j] < arr[minIndex])
  8. minIndex = j;
  9. if (minIndex != i) {
  10. tmp = arr[i];
  11. arr[i] = arr[minIndex];
  12. arr[minIndex] = tmp;
  13. }
  14. }
  15. }

Report this snippet  

You need to login to post a comment.