Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1744ce4

Browse files
Create SortingAlgorithms.cpp
1 parent 99dce37 commit 1744ce4

File tree

1 file changed

+155
-0
lines changed

1 file changed

+155
-0
lines changed

‎SortingAlgorithms.cpp‎

Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
#include <iostream>
2+
#include <stdlib.h>
3+
#include <time.h>
4+
#include <fstream>
5+
#include "windows.h"
6+
7+
using namespace std;
8+
9+
int partition( int * a, int l, int r) {
10+
int pivot, i, j, t;
11+
pivot = a[l];
12+
i = l; j = r+1;
13+
14+
while(1){
15+
do ++i; while( a[i] <= pivot && i <= r );
16+
do --j; while( a[j] > pivot );
17+
if( i >= j ) break;
18+
t = a[i]; a[i] = a[j]; a[j] = t;
19+
}
20+
t = a[l]; a[l] = a[j]; a[j] = t;
21+
return j;
22+
}
23+
24+
void quickSort( int * a, int l, int r){
25+
int j;
26+
27+
if( l < r ){
28+
// divide and conquer
29+
j = partition( a, l, r);
30+
quickSort( a, l, j-1);
31+
quickSort( a, j+1, r);
32+
}
33+
}
34+
35+
void merge(int* list,int low,int mid,int high,int size){
36+
int i, mi, k, lo, temp[size];
37+
38+
lo = low;
39+
i = low;
40+
mi = mid + 1;
41+
while ((lo <= mid) && (mi <= high))
42+
{
43+
if (list[lo] <= list[mi])
44+
{
45+
temp[i] = list[lo];
46+
lo++;
47+
}
48+
else
49+
{
50+
temp[i] = list[mi];
51+
mi++;
52+
}
53+
i++;
54+
}
55+
if (lo > mid)
56+
{
57+
for (k = mi; k <= high; k++)
58+
{
59+
temp[i] = list[k];
60+
i++;
61+
}
62+
}
63+
else
64+
{
65+
for (k = lo; k <= mid; k++)
66+
{
67+
temp[i] = list[k];
68+
i++;
69+
}
70+
}
71+
72+
for (k = low; k <= high; k++)
73+
list[k] = temp[k];
74+
}
75+
76+
void merge_sort(int* a, int first, int last, int size) {
77+
78+
if (first < last ){
79+
int middle = (first + last) / 2;
80+
merge_sort(a, first, middle, size);
81+
merge_sort(a, middle+1, last, size);
82+
merge(a, first, middle, last, size);
83+
}
84+
}
85+
86+
void insertionSort(int* a, int size) {
87+
int key,i;
88+
for (int j = 1; j < size; j++) {
89+
key=a[j];
90+
i=j-1;
91+
while(i>=0 && a[i]>key){
92+
a[i+1]=a[i];
93+
i=i-1;
94+
}
95+
a[i+1]=key;
96+
}
97+
}
98+
99+
int* populateArray(int size) {
100+
101+
int* a = new int[size];
102+
for (int i = 0; i < size; i++)
103+
a[i] = rand();
104+
return a;
105+
}
106+
107+
int main() {
108+
int sizes[8] = { 500, 1000, 10000,100000,200000,300000,400000,500000 };
109+
srand(time(NULL));
110+
111+
for (int i = 0; i < 8; i++) {
112+
int size = sizes[i];
113+
long long mergeSortTimeSum=0,insertionSortTimeSum=0,quickSortTimeSum=0 ;
114+
SYSTEMTIME time;
115+
int* a ;
116+
117+
for(int j=1;j<=5;j++){
118+
//sayılar random üretildiği için 5 defa çalışıp sürelerin ortalamasi alinacak
119+
120+
a = populateArray(size);
121+
GetSystemTime(&time);
122+
long before = (time.wMinute*60 * 1000)+(time.wSecond * 1000) + time.wMilliseconds;
123+
insertionSort(a, size);
124+
GetSystemTime(&time);
125+
long after = (time.wMinute*60 * 1000)+(time.wSecond * 1000) + time.wMilliseconds;
126+
insertionSortTimeSum+=after-before;
127+
128+
a = populateArray(size);
129+
GetSystemTime(&time);
130+
before = (time.wMinute*60 * 1000)+(time.wSecond * 1000) + time.wMilliseconds;
131+
merge_sort(a, 0, size-1, size);
132+
GetSystemTime(&time);
133+
after = (time.wMinute*60 * 1000)+(time.wSecond * 1000) + time.wMilliseconds;
134+
mergeSortTimeSum+=after-before;
135+
136+
a = populateArray(size);
137+
GetSystemTime(&time);
138+
before = (time.wMinute*60 * 1000)+(time.wSecond * 1000) + time.wMilliseconds;
139+
quickSort(a, 0, size-1);
140+
GetSystemTime(&time);
141+
after = (time.wMinute*60 * 1000)+(time.wSecond * 1000) + time.wMilliseconds;
142+
quickSortTimeSum+=after-before;
143+
}
144+
cout << "Size: "<< size << " Merge Sort Time: " << mergeSortTimeSum/5<<"ms";
145+
cout << " Insertion Sort Time: " << insertionSortTimeSum/5<<"ms";
146+
cout << " Quick Sort Time: " << quickSortTimeSum/5 << "ms"<<endl;
147+
/* ofstream myfile;
148+
myfile.open ("ordered.txt");
149+
for(int k=0;k<size;k++)
150+
myfile << a[k]<<",";
151+
myfile.close();
152+
*/
153+
}
154+
return 0;
155+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /