Advertisement
Guest User

Untitled

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