Guest User

Untitled

a guest
May 27th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.01 KB | None | 0 0
  1. public void selectionSort(int a[], int n)
  2. {
  3. recurSelectionSort(a, n, 0);
  4. }
  5.  
  6. // Recursive selection sort. n is size of a[] and index
  7. // is index of starting element.
  8. static void recurSelectionSort(int a[], int n, int index)
  9. {
  10.  
  11. // Return when starting and size are same
  12. if (index == n)
  13. return;
  14.  
  15. // calling minimum index function for minimum index
  16. int k = minIndex(a, index, n-1);
  17.  
  18. // Swapping when index nd minimum index are not same
  19. if (k != index){
  20. // swap
  21. int temp = a[k];
  22. a[k] = a[index];
  23. a[index] = temp;
  24. }
  25. // Recursively calling selection sort function
  26. recurSelectionSort(a, n, index + 1);
  27. }
  28.  
  29. // Return minimum index
  30. static int minIndex(int a[], int i, int j)
  31. {
  32. if (i == j)
  33. return i;
  34.  
  35. // Find minimum of remaining elements
  36. int k = minIndex(a, i + 1, j);
  37.  
  38. // Return minimum of current and remaining.
  39. }
Add Comment
Please, Sign In to add comment