Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <math.h>
- int counter = 0;
- int findNth(int* set, int setSize, int n)
- {
- if(n == 0)
- return set[setSize];
- int i, z, temp;
- int max = set[setSize - 1];
- int swapTo = setSize - 1;
- printf(".[ ");
- for(z = 0; z < setSize; z++)
- printf("%i ",set[z]);
- printf("] (recursion)\n");
- for(i = 0; i < setSize; i++)
- {
- while(set[i] > max)
- {
- max = set[i];
- set[i] = set[swapTo];
- set[swapTo] = max;
- swapTo--;
- printf("..[ ");
- for(z = 0; z < setSize; z++)
- printf("%i ",set[z]);
- printf("] (swap)\n");
- counter++;
- }
- }
- counter += setSize;
- if(swapTo == setSize - 1)
- return findNth(set,swapTo,n - 1);
- if(swapTo + n >= setSize)
- return findNth(set,swapTo + 1,n + swapTo + 1 - setSize);
- return set[swapTo + n];
- }
- main()
- {
- int test[] = {3, 2, 8, 5, 1, 9, 2, 66, 4, 9};
- printf("5th Largest: %i\n",findNth(test,sizeof(test)/sizeof(int),5));
- printf("Comaprisons: %i",counter);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement