rootUser

Quick Sort 10.12.15

May 26th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<stdio.h>
  2. #include<limits.h>
  3. #include<iostream>
  4.  
  5. int a[1000];
  6. int n ;
  7.  
  8. void input();
  9. void output();
  10. void sorting(int , int);
  11.  
  12.  
  13.  
  14. int main()
  15. {
  16.  
  17.      input();
  18.      sorting(1,n);
  19.      output();
  20.  
  21. }
  22.  
  23. void input()
  24. {
  25.      printf("How many numbers:");
  26.      scanf("%d",&n);
  27.  
  28.      printf("Insert numbers:\n");
  29.  
  30.      int i;
  31.  
  32.      for(i=1 ; i<=n ; i++)
  33.      {
  34.          scanf("%d",&a[i]);
  35.      }
  36. }
  37.  
  38. void output()
  39. {
  40.      printf("\nSorted array:");
  41.  
  42.      for(int i=1 ; i<=n ; i++)
  43.      {
  44.           printf("%d ",a[i]);
  45.      }
  46. }
  47.  
  48.  
  49. void sorting(int f , int l)
  50. {
  51.      if(f < l)
  52.      {
  53.           int i=f+1 ;
  54.  
  55.           while(a[i] < a[f])
  56.                i++;
  57.  
  58.           int j=l;
  59.  
  60.           while(a[j]>a[f])
  61.                j--;
  62.  
  63.           if(i<j)
  64.           {
  65.                std::swap(a[i] , a[j]);
  66.  
  67.                 while(a[i] < a[f])  i++;
  68.                 while(a[j]>a[f])    j--;
  69.           }
  70.  
  71.           std::swap(a[f] , a[j]);
  72.  
  73.           sorting(f , j-1);
  74.           sorting(j+1 , l);
  75.      }
  76. }
Add Comment
Please, Sign In to add comment