-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathquick-sort.cpp
More file actions
68 lines (60 loc) · 1.08 KB
/
quick-sort.cpp
File metadata and controls
68 lines (60 loc) · 1.08 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include<iostream>
#include<fstream>
#include<string.h>
#include<cstdlib>
#include<ctime>
using namespace std;
void printfArray(int arr[],int n)
{
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" ";
}
cout<<endl;
}
int partition(int arr[],int low,int high)
{
int pivote=arr[low];
int i=low+1;
int j=high;
int tem=0,tem1=0;
do{
while(arr[i]<=pivote)
{
i++;
}
while(arr[j]>pivote)
{
j--;
}
if(i<j)
{
tem=arr[i];
arr[i]=arr[j];
arr[j]=tem;
}
}while(i<j);
tem1=arr[low];
arr[low]=arr[j];
arr[j]=tem1;
return j;
}
void quickSort(int arr[],int low,int high)
{
for(int i=0;i<1000000;i++);
if(low<high)
{
int partitionIndex=partition(arr,low,high);
quickSort(arr,low,partitionIndex-1);
quickSort(arr,partitionIndex+1,high);
}
}
int main()
{
int n=6;
int a[n]={34,12,33,2,56,2};
printfArray(a,n);
quickSort(a,0,n-1);
cout<<endl;
printfArray(a,n);
}