-
Notifications
You must be signed in to change notification settings - Fork 1
/
daa2.cpp
48 lines (45 loc) · 941 Bytes
/
daa2.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
#include <iostream>
using namespace std;
int Partition(int arr[], int s, int e)
{
int pivot = arr[e];
int pIndex = s;
for (int i = s; i <= e - 1; i++)
{
if (arr[i] <= pivot)
{
swap(arr[i], arr[pIndex]);
pIndex++;
}
}
swap(arr[e], arr[pIndex]);
return pIndex;
}
void QuickSort(int arr[], int s, int e)
{
if (s < e)
{
int p = Partition(arr, s, e);
QuickSort(arr, s, p - 1);
QuickSort(arr, p + 1, e);
}
}
int main()
{
int n;
int arr[10];
cout << "Enter the size of the array:";
cin >> n;
cout << "Enter the array elements:" << endl;
for (int i = 0; i < n; i++)
{
cin >> arr[i];
}
QuickSort(arr, 0, n - 1);
cout << "Sorted Array:" << endl;
for (int i = 0; i < n; i++)
{
cout << arr[i] << endl;
}
return 0;
}