-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick_sort.cpp
124 lines (104 loc) · 2.01 KB
/
quick_sort.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream.h>
#include <conio.h>
#include <timer.h>
#include <stdlib.h>
#include <malloc.h>
class ArrayList
{
int *a,n;
int median(int,int);
public :
void read();
void quicksort();
void actual_sort(int,int);
void print();
void randomize(int);
};
void ArrayList :: read()
{
cout << "\nGive the number of elements : ";
cin >> n;
a = new int [n];
cout << "\nNow give elements one - by - one\n";
for(int i = 0;i < n;i++) cin >> a[i];
}
void ArrayList :: quicksort()
{
actual_sort(0,n - 1);
}
void ArrayList :: actual_sort(int low,int high)
{
int l;
if(low < high)
{
l = median(low,high);
actual_sort(low,l - 1);
actual_sort(l + 1,high);
}
}
int ArrayList :: median(int left,int right)
{
int pivot,l,r;
l = left;r = right;
pivot = left;
while(1)
{
int tmp;
if(pivot == l)
{
while(a[r] > a[pivot] && r > l) r--;
if(r > l)
{
tmp = a[pivot];
a[pivot] = a[r];
a[r] = tmp;
pivot = r;
}
else break;
}
if(pivot == r)
{
while(a[l] < a[pivot] && l < r) l++;
if(l < r)
{
tmp = a[pivot];
a[pivot] = a[l];
a[l] = tmp;
pivot = l;
}
else break;
}
}
return pivot;
}
void ArrayList :: print()
{
cout << "\nThe array after sorting is\n";
cout << "\n[ " << a[0];
for(int i = 1;i < n;i++) cout << " , " << a[i];
cout << " ]\n";
}
void ArrayList :: randomize(int size)
{
Timer t;
a = new int [5000];
for(int i = 0;i < size;i++) a[i] = size - i;
t.start();actual_sort(0,size - 1);t.stop();
cout << "For operation " << size << " time taken is : " << t.time();
cout << " seconds\n";
free(a);
}
int main()
{
clrscr();
Timer t;
ArrayList obj;
obj.read();
t.start();obj.quicksort();t.stop();
obj.print();
cout << "\nTime taken for this is : " << t.time() << " seconds\n";
//uncomment this only for this bulk operation
//for(int i = 500;i <= 3500;i += 500) obj.randomize(i);
getch();
return 0;
}