-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathQuick_sort.cpp
More file actions
59 lines (49 loc) · 934 Bytes
/
Quick_sort.cpp
File metadata and controls
59 lines (49 loc) · 934 Bytes
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
#include<bits/stdc++.h>
using namespace std;
int partition_pivot(int arr[],int x,int y)
{
int pivot_element = arr[y];
int i=x-1;
for(int j=x;j<y;j++)
{
if(arr[j]<=pivot_element)
{
i++;
swap(arr[i],arr[j]);
}
}
swap(arr[i+1],arr[y]);
return i+1;
}
int partition_rand(int arr[],int l,int h)
{
int random1= l + rand()% (h-l);
swap(arr[random1],arr[h]);
int p = partition_pivot(arr,l,h);
return p;
}
void quick_Sort(int arr[],int low,int high){
if(low<high)
{
int x= partition_rand(arr,low,high);
quick_Sort(arr,low,x-1);
quick_Sort(arr,x+1,high);
}
}
int main()
{
int n;
cin>>n;
int arr[n];
cout<<"enter the array"<<endl;
for (int i = 0; i < n; i++)
{
cin>>arr[i];
}
quick_Sort(arr, 0, n - 1);
cout<<"Array after sorting :"<<endl;
for(int i=0;i<n;i++)
{
cout<<arr[i]<<" \n";
}
}