fork(1) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int n;
  5.  
  6. void swap(int *a, int *b)
  7. {
  8. int temp= *a;
  9. *a=*b;
  10. *b=temp;
  11. }
  12.  
  13. int partition(int a[], int low, int high)
  14. {
  15. int i=low-1,temp;
  16. int cmp= a[high];
  17.  
  18. for(int j=low;j<=high-1;j++)
  19. {
  20. if(a[j]<=cmp)
  21. {
  22. i++;
  23. swap(&a[i],&a[j]);
  24.  
  25. }
  26. }
  27.  
  28. swap(&a[i+1],&a[high]);
  29.  
  30. return (i+1);
  31.  
  32. }
  33.  
  34. int quicksort(int a[], int low, int high, int m)
  35. {
  36. if(low<=high && (high-low+1)>=m)
  37. {
  38. int pivot= partition(a,low,high);
  39. //cout<<pivot<<endl;
  40.  
  41. if((high-pivot)== m-1)
  42. return a[pivot];
  43.  
  44. else if((high-pivot)>m-1)
  45. return quicksort(a,pivot+1,high, m);
  46. else
  47. return quicksort(a,low,pivot-1, m-(high-pivot+1));
  48. }
  49. }
  50.  
  51.  
  52. int main() {
  53. // your code goes here
  54. cin>>n;
  55. int a[n+1],i;
  56. for(i=0;i<n;i++)
  57. cin>>a[i];
  58.  
  59. cout<<quicksort(a,0,n-1,3)<<endl;
  60.  
  61. /* cout<<"The sorted array is : ";
  62. for(i=0;i<n;i++)
  63. cout<<a[i]<<" ";
  64. */
  65. return 0;
  66. }
Success #stdin #stdout 0s 3100KB
stdin
4
102 3194 1293 324
stdout
324