forked from zatch3301/RTU-DigitalLibrary
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quicksort.java
47 lines (45 loc) · 981 Bytes
/
Quicksort.java
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
#include <iostream>
#include <stdio.h>
template <class t>
using namespace std;
void quick_sort(t a[], int low, int high)
{
t k;
int i, j, flag = 1;
if (low < high) {
k = a[low];
i = low + 1;
j = high;
while (flag) {
while ((a[i] <= k) && (i < j)) i++; while (a[j] > k)
j--;
if (i < j)
swap(a, i, j);
else
flag = 0;
}
swap(a, low, j);
quick_sort(a, low, j - 1);
quick_sort(a, j + 1, high);
}
}
template
void swap(t1 a[], int x, int y)
{
t1 temp;
temp = a[x];
a[x] = a[y];
a[y] = temp;
}
int main()
{
int i, n, a[20];
cout << "Enter the number of elements to be sort::"; cin >> n;
cout << "Enter elements:\n";
for (i = 0; i < n; i++) cin >> a[i];
quick_sort(a, 0, n - 1);
cout << "The sorted elements are:\n";
for (i = 0; i < n; i++)
cout << a[i] << endl;
;
}