forked from Apurvgeek/programs-in-cpp-and-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuick_Sort.cpp
More file actions
45 lines (44 loc) · 1.54 KB
/
Quick_Sort.cpp
File metadata and controls
45 lines (44 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include<iostream>
#include<iomanip>
using namespace std;
/*************** Recursive ****************************/
int partition(int numbers[],int left,int right)
{
int pivot,temp;
pivot = numbers[left];
int Pos_pivot=left;
cout<<"\n"<<setw(4)<<pivot; //Display Pivot
while (left < right)
{
while ((numbers[right] >= pivot) && (left < right)) right--;
while ((numbers[left] <= pivot) && (left < right)) left++;
if(left<right) //swap left with right position element
{ temp=numbers[right];
numbers[right] = numbers[left];
numbers[left] =temp; }//if end
} //while end
if(pivot!=right) //swap pivot with right position element
{ numbers[Pos_pivot]=numbers[right]; numbers[right]=pivot; }//if end
return right;
}
void quicksort(int numbers[], int left, int right)
{
int i,pivot;
static int Pass=1;
pivot=partition(numbers,left,right);
cout<<" Pass "<<setw(2)<<Pass++;
for(i=0;i<10;i++)
cout<<setw(4)<<numbers[i];
if (left < pivot) //recursion for left sublist
quicksort(numbers, left, pivot-1);
if (right > pivot) //recursion for right sublist
quicksort(numbers, pivot+1, right);
}
int main()
{
int i,a[10]={56,-90,80,78,234,654,432,12,0,-11};
quicksort(a,0,9);
cout<<"\nQuick Sort: ";
for(i=0;i<10;i++)
cout<<setw(4)<<a[i];
return 0; }//main end