Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct stack{
  6. int val;
  7. stack *next;
  8. };
  9.  
  10. void swap(int tab[], int i, int j){
  11. int tmp=tab[i];
  12. tab[i]=tab[j];
  13. tab[j]=tmp;
  14. }
  15.  
  16. int partition(int tab[], int left, int right){
  17. int x=tab[right-1];
  18. int index=left-1;
  19. for(int i=left;i<right;i++){
  20.     if(tab[i]<=right){
  21.         index++;
  22.         swap(tab, i, index);
  23.     }
  24. }
  25. swap(tab, right, index+1);
  26. return index+1;
  27. }
  28.  
  29. void push(int value, stack *&kot){
  30. stack *nowy=new stack;
  31. nowy->val=value;
  32. nowy->next=kot;
  33. kot=nowy;
  34. }
  35.  
  36. int pop(stack *&kot){
  37. stack *tmp=kot;
  38. int result=kot->val;
  39. kot=kot->next;
  40. delete tmp;
  41. return result;
  42. }
  43.  
  44. bool empty(stack *kot){
  45. if(kot==NULL) return true;
  46. else return false;
  47. }
  48.  
  49. void quicksort(int tab[], int left, int right){
  50. stack *stos=new stack;
  51. stos=NULL;
  52. int q;
  53. while(left<right || !empty(stos)){
  54.         if(left<right){
  55. q=partition(tab, left, right);
  56.  for(int i=0;i<10;i++) cout<<tab[i];
  57.  cout<<endl;
  58. push(q+1, stos);
  59. push(right, stos);
  60. right=q-1;
  61.         }
  62. else{
  63.     right=pop(stos);
  64.     left=pop(stos);
  65. }
  66. }
  67. }
  68.  
  69. int main(){
  70.     int tab[10]={4, 5, 2, 4, 6, 7, 3, 4, 7, 4};
  71.     quicksort(tab, 0, 9);
  72.     for(int i=0;i<10;i++) cout<<tab[i];
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement