FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
programming/sorting/SortingTimeComplx.cpp at master · hacktoberfest17/programming · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
hacktoberfest17
/
programming
Public
Notifications
You must be signed in to change notification settings
Fork
765
Star
333
Code
Issues
271
Pull requests
583
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
programming
/
sorting
/
SortingTimeComplx.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
205 lines (179 loc) · 4.78 KB
Breadcrumbs
programming
/
sorting
/
SortingTimeComplx.cpp
Copy path
File metadata and controls
205 lines (179 loc) · 4.78 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#
include
<
iostream
>
#
include
<
string
>
#
include
<
ctime
>
#
include
<
iomanip
>
#
include
<
random
>
using
namespace
std
;
void
input
(
int
[],
int
);
int
binarySearch
(
int
[],
int
,
int
,
int
);
void
insertionSort
(
int
[],
int
);
void
bubbleSort
(
int
[],
int
);
void
swap
(
int
*,
int
*);
void
selectionSort
(
int
[],
int
);
void
time_it
(
double
,
double
, string);
void
modified_bubbleSort
(
int
[],
int
);
void
mergeSort
(
int
[],
int
,
int
);
void
merge
(
int
[],
int
,
int
,
int
);
void
cpu_clock_cycles
(
double
,
double
);
int
binarySearch
(
int
a[],
int
low,
int
high,
int
x) {
if
(high <= low)
return
(x > a[low])? (low +
1
): low;
int
mid = (low + high)/
2
;
if
(x == a[mid])
return
mid+
1
;
if
(x > a[mid])
return
binarySearch
(a, mid+
1
, high, x);
return
binarySearch
(a, low, mid-
1
, x);
}
void
insertionSort
(
int
a[],
int
n) {
int
loc, i, j, selected;
for
(i=
1
; i<n; i++) {
selected = a[i];
j = i -
1
;
loc =
binarySearch
(a,
0
, j, selected);
while
(j >= loc){
a[j+
1
] = a[j];
j--;
}
a[j+
1
] = selected;
}
}
void
bubbleSort
(
int
b[],
int
n) {
int
i, j;
for
(i=n-
1
;i>=
0
;i--) {
for
(j=
0
; j<i; j++) {
if
(b[j+
1
] < b[j]) {
swap
(b+j,b+j+
1
);
}
}
}
}
void
swap
(
int
*a,
int
*b) {
int
temp = *a;
*a = *b;
*b = temp;
}
void
selectionSort
(
int
c[],
int
n) {
int
min, i, j;
for
(i=
0
; i<n-
1
; i++) {
for
(j=i+
1
; j<n; j++) {
if
(c[j] < c[i]){
swap
(c+i,c+j);
}
}
}
}
void
time_it
(
double
strt,
double
lst, string str) {
double
time_taken =
double
(lst - strt)/
double
(
CLOCKS_PER_SEC
);
cout <<
"
Time taken by
"
<< str <<
"
Sort is:
"
<< fixed << time_taken <<
setprecision
(
3
);
cout <<
"
sec(s)
\n
"
;
}
void
cpu_clock_cycles
(
double
_2,
double
_1) {
cout <<
"
CPU cycles taken:
"
<< _1 - _2 << endl;
}
void
input
(
int
x[],
int
n) {
int
i;
random_device random_device;
mt19937
random_engine
(
random_device
());
for
(i=
0
; i<n; i++) {
uniform_int_distribution<
int
>
distribution_1_1000
(
1
,
1000
);
x[i] =
distribution_1_1000
(random_engine);
}
}
void
modified_bubbleSort
(
int
b[],
int
n) {
int
i, j;
bool
swapped;
for
(i=n-
1
;i>=
0
;i--) {
swapped =
false
;
for
(j=
0
; j<i; j++) {
if
(b[j+
1
] < b[j]) {
swap
(b+j,b+j+
1
);
swapped =
true
;
}
}
if
(!swapped)
break
;
}
}
void
mergeSort
(
int
a[],
int
lo,
int
hi) {
int
mid;
if
(lo < hi) {
mid = (lo + hi)/
2
;
mergeSort
(a, lo, mid);
mergeSort
(a, mid+
1
, hi);
merge
(a, lo, hi, mid);
}
}
void
merge
(
int
a[],
int
l,
int
h,
int
m) {
int
i, j, k, temp[h-l+
1
];
i = l;
j = m +
1
;
k =
0
;
while
(i <= m && j <= h) {
if
(a[i] < a[j])
temp[k++] = a[i++];
else
temp[k++] = a[j++];
}
while
(i <= m)
temp[k++] = a[i++];
while
(j <= h)
temp[k++] = a[j++];
for
(i=l, j=
0
; i<=h; i++, j++)
a[i] = temp[j];
}
int
main
() {
clock_t
start, end;
int
size, elem, i;
cout <<
"
Enter the size of the array
\n
"
;
cin >> size;
//
Dynamic array allocation during runtime to minimize space complexity
int
*a =
new
int
[size];
int
*b =
new
int
[size];
int
*c =
new
int
[size];
int
*d =
new
int
[size];
int
*e =
new
int
[size];
cout <<
"
Filling random array elements
\n
"
;
//
Array gets automatically filled with random numbers
input
(a, size);
for
(i=
0
; i < size; i++)
e[i] = d[i] = c[i] = b[i] = a[i];
cout <<
"
Done
\n
"
;
cout <<
"
\n
Sorting the array using Merge Sort...
\n
"
;
start =
clock
();
mergeSort
(e,
0
, size-
1
);
end =
clock
();
delete[]
e;
time_it
(start, end,
"
Merge
"
);
cpu_clock_cycles
(start, end);
cout <<
"
\n
Sorting the array using Binary Insertion Sort...
\n
"
;
start =
clock
();
insertionSort
(a, size);
end =
clock
();
delete[]
a;
//
Time function calculates execution time for sorting
time_it
(start, end,
"
Binary Insertion
"
);
cpu_clock_cycles
(start, end);
cout <<
"
\n
Sorting the array using Selection Sort...
\n
"
;
start =
clock
();
selectionSort
(c, size);
end =
clock
();
//
Array memory is cleared after the array is no longer required
delete[]
c;
time_it
(start, end,
"
Selection
"
);
cpu_clock_cycles
(start, end);
cout <<
"
\n
Sorting the array using Bubble Sort...
\n
"
;
start =
clock
();
bubbleSort
(b, size);
end =
clock
();
delete[]
b;
time_it
(start, end,
"
Bubble
"
);
cpu_clock_cycles
(start, end);
cout <<
"
\n
Sorting the array using Modified Bubble Sort...
\n
"
;
start =
clock
();
modified_bubbleSort
(d, size);
end =
clock
();
delete[]
d;
time_it
(start, end,
"
Modified Bubble
"
);
cpu_clock_cycles
(start, end);
return
0
;
}
Back
|
FazBrowse Home
|
New Git URL