forked from JayeshShelar/DSA-Practicals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAss14_Quicksort.cpp
88 lines (72 loc) · 930 Bytes
/
Ass14_Quicksort.cpp
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include<iostream>
using namespace std;
class Quick_Sort
{
public:
int arr[10];
int pivot,low,high,temp;
public:
void accept()
{
cout<<"Enter the array: ";
for(int i=0;i<5;i++)
{
cin>>arr[i];
}
}
int partition(int low,int high)
{
int i=low;
int j=high;
int pivot=low;
while(i<j)
{
while(arr[i]<=arr[pivot] && i<high)
{
i++;
}
while(arr[j]>arr[pivot])
{
j--;
}
if(i<j)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
else
{
temp=arr[pivot];
arr[pivot]=arr[j];
arr[j]=temp;
}
}
return j;
}
void Quicksort(int low,int high)
{
if(low<high)
{
int j=partition(low,high);
Quicksort(low,j-1);
Quicksort(j+1,high);
}
}
void display()
{
cout<<"Sorted array is: ";
for(int i=0;i<5;i++)
{
cout<<arr[i];
}
}
};
int main()
{
Quick_Sort q;
q.accept();
q.Quicksort(0,5);
q.display();
return 0;
}