-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuick Sort.cpp
More file actions
67 lines (57 loc) · 1 KB
/
Quick Sort.cpp
File metadata and controls
67 lines (57 loc) · 1 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
#include<iostream>
using namespace std;
int *a, n;
void input()
{
cout<<"\nEnter the number of elements : ";
cin>>n;
a= new int[n];
cout<<"\nEnter the elements: ";
for(int i =0; i<n; i++)
{
cin>>a[i];
}
}
void display()
{
cout<<"\nThe elements in the array are: ";
for(int i =0; i<n; i++)
{
cout<<a[i]<<"\t";
}
}
void quicksort(int a[25],int first,int last)
{
int i, j, pivot, temp;
if(first<last)
{
pivot=first;
i=first;
j=last;
while(i<j)
{
while(a[i]<=a[pivot]&&i<last)
i++;
while(a[j]>a[pivot])
j--;
if(i<j)
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
temp=a[pivot];
a[pivot]=a[j];
a[j]=temp;
quicksort(a,first,j-1);
quicksort(a,j+1,last);
}
}
int main()
{
input();
quicksort(a,0,n-1);
display();
return 0;
}