Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //This program recursively sorts an array
- #include<stdio.h>
- void rec_sort(int values[], int n);
- main()
- {
- int vals[4];
- vals[0] = 37; vals[1] = 48; vals[2] = 56; vals[3] = 63;
- printf("this array sorted: %x\n", rec_sort(vals[], 4));
- system("Pause");
- return 0;
- }
- void rec_sort(int values[], int n) {
- //Base case
- if (n<2) return;
- int maxIndex=0;
- int i;
- //Find max item in array in indexes 0 through n-1
- for(i=1; i<n;i++) {
- if(values[i] > values[maxIndex])
- maxIndex=i;
- }
- //Swap this element with one stored in n-1
- //Set temp to n-1, set n-1 in array to max, set max to temp
- int temp = values[n-1]; //Store temp as last element in array
- values[n-1] = values[maxIndex]; //Store last element as max value in array
- values[maxIndex] = temp; //temp will keep on changing, as array is sorted
- //Recursively sort the array values of length n-1
- sort(values, n-1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement