Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void selectionSort(int a[], int n)
- {
- recurSelectionSort(a, n, 0);
- }
- // Recursive selection sort. n is size of a[] and index
- // is index of starting element.
- static void recurSelectionSort(int a[], int n, int index)
- {
- // Return when starting and size are same
- if (index == n)
- return;
- // calling minimum index function for minimum index
- int k = minIndex(a, index, n-1);
- // Swapping when index nd minimum index are not same
- if (k != index){
- // swap
- int temp = a[k];
- a[k] = a[index];
- a[index] = temp;
- }
- // Recursively calling selection sort function
- recurSelectionSort(a, n, index + 1);
- }
- // Return minimum index
- static int minIndex(int a[], int i, int j)
- {
- if (i == j)
- return i;
- // Find minimum of remaining elements
- int k = minIndex(a, i + 1, j);
- // Return minimum of current and remaining.
- }
Add Comment
Please, Sign In to add comment