forked from Haresh1204/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickSort_First_Pivot.cpp
More file actions
65 lines (60 loc) · 1.16 KB
/
quickSort_First_Pivot.cpp
File metadata and controls
65 lines (60 loc) · 1.16 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
#include <iostream>
using namespace std;
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
return;
}
int partition(int arr[], int start, int end)
{
int pivot = arr[start];
int i = start;
int j = end;
while (i <= j)
{
while (arr[i] <= pivot)
{
i++;
}
while (arr[j] > pivot)
{
j--;
}
if (i <= j)
swap(arr[i], arr[j]);
else
break;
}
swap(arr[start], arr[j]);
return j;
}
void quickSort(int arr[], int start, int end)
{
if (start < end)
{
int part = partition(arr, start, end);
quickSort(arr, start, part - 1);
quickSort(arr, part + 1, end);
}
}
int main()
{
int size;
cout << "Enter size: ";
cin >> size;
int arr[size];
cout << "Enter array: ";
for (int i = 0; i < size; i++)
{
cin >> arr[i];
}
quickSort(arr, 0, size);
cout << "Sorted array is: ";
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
cout << endl;
}