-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicksort.cpp
64 lines (55 loc) · 851 Bytes
/
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
#include <iostream>
#define MAX 50
using namespace std;
int i,j;
void swap(int *a,int *b)
{
int t = *a;
*a = *b;
*b = t;
}
int partition(int arr[],int l,int h)
{
int pivot = arr[h];
int i = l-1;
for (j = l; j < h; j++)
{
if (arr[j] <= pivot)
{
i++;
swap(&arr[j],&arr[i]);
}
}
swap(&arr[i+1],&arr[h]);
return i+1;
}
void quicksort(int arr[],int l,int h)
{
if (l < h)
{
int pi = partition(arr,l,h);
quicksort(arr,l,pi-1);
quicksort(arr,pi,h);
}
}
int main()
{
int n;
int arr[MAX];
cout << "\nEnter the number of elements (max 50): ";
cin >> n;
int s = n;
cout << "\nEnter the elements in the array: ";
for (i = 0; i < n; i++)
{
cin >> arr[i];
}
quicksort(arr,0,s-1);
cout << "abcd " << n;
cout << "\nThe sorted array is: ";
for (i = 0; i < n; i++)
{
cout << arr[i] << " ";
}
return 0;
}