-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuick_sort.cpp
More file actions
56 lines (48 loc) · 826 Bytes
/
Quick_sort.cpp
File metadata and controls
56 lines (48 loc) · 826 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
#include<bits/stdc++.h>
using namespace std;
int partition(int* arr,int low,int high)
{
int pivot=low+rand()%(high-low);
int j=low-1;
int temp=arr[pivot];
arr[pivot]=arr[high-1];
arr[high-1]=temp;
pivot=high-1;
for(int i=low;i<high-1;i++)
{
if(arr[i]<arr[pivot])
{
j++;
temp=arr[j];
arr[j]=arr[i];
arr[i]=temp;
}
}
temp=arr[j+1];
arr[j+1]=arr[pivot];
arr[pivot]=temp;
return (j+1);
}
void quicksort(int* arr,int low,int high)
{
if(low<high)
{
int pivot = partition(arr,low,high);
quicksort(arr,low,pivot);
quicksort(arr,pivot+1,high);
}
}
int main()
{
int n=5;
cout<<"Enter the number of elements : ";
cin>>n;
cout<<"Enter the array elements \n";
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
quicksort(arr,0,n);
for(int i=0;i<n;i++)
cout<<arr[i]<<" ";
return 0;
}