Rakibul_Ahasan

Quick sort

Oct 14th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. /// Divide and Conquer
  2.  
  3. #include<bits/stdc++.h>
  4. using namespace std;
  5.  
  6. int partition(int A[],int low,int high){
  7.     int i,j,pivot,temp;
  8.  
  9.     pivot=A[high];
  10.     i=low-1;
  11.  
  12.     for(j=low; j<high; j++){
  13.         if(A[j]<=pivot){
  14.             i=i+1;
  15. //            temp=A[j];
  16. //            A[j]=A[i];
  17. //            A[i]=temp;
  18.            swap(A[i],A[j]);
  19.         }
  20.     }
  21.  
  22. //    temp=A[high];
  23. //    A[high]=A[i+1];
  24. //    A[i+1]=temp;
  25.       swap(A[i+1],A[high]);
  26.  
  27.     return i+1;
  28. }
  29.  
  30. void quick_sort(int A[],int low,int high){
  31.     if(low>=high){
  32.         return;
  33.     }
  34.  
  35.     int p = partition(A,low,high);
  36.  
  37.     quick_sort(A,low,p-1);
  38.     quick_sort(A,p+1,high);
  39. }
  40.  
  41. void printArray(int A[], int size){
  42.     int i;
  43.     for (i = 0; i < size; i++)
  44.         cout << A[i] << " ";
  45.     cout << endl;
  46. }
  47.  
  48. int main()
  49. {
  50.     int A[]={45,23,78,45,78,90};
  51.     int n=6;
  52.     quick_sort(A,0,n-1);
  53.  
  54.     cout << "Sorted array: \n";
  55.     printArray(A,n);
  56.  
  57.     return 0;
  58. }
Add Comment
Please, Sign In to add comment