-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain orig.cpp
More file actions
155 lines (135 loc) · 3.48 KB
/
main orig.cpp
File metadata and controls
155 lines (135 loc) · 3.48 KB
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
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <unistd.h>
#include <cmath>
#include <string>
#include "quicksort.h"
#include "mergesort.h"
using namespace std;
//check if array sorted correctly - by comparing two sorted arrays
void arrayCheck(int arraysize, int *array1, int *array2) {
for (int i = 0; i < arraysize; i++) {
if (array1[i] != array2[i]) {
cout << "Error with the previous sortings!\n\n";
return;
}
}
}
//debugging purposes
void printList(int arr[], int length){
for (int i = 0; i<length; i++){
cout<<arr[i]<<" ";
}
cout<<endl;
}
// Merge sort
void merge(int *arr, int low, int high, int mid, int len) {
int i, j, k, c[len];
i = low;
k = low;
j = mid + 1;
while (i <= mid && j <= high) {
if (arr[i] < arr[j]) {
c[k] = arr[i];
k++;
i++;
}
else {
c[k] = arr[j];
k++;
j++;
}
}
while (i <= mid) {
c[k] = arr[i];
k++;
i++;
}
while (j <= high) {
c[k] = arr[j];
k++;
j++;
}
for (i = low; i < k; i++) {
arr[i] = c[i];
}
}
void mergeSort(int *arr, int low, int high, int len) {
int mid;
if (low < high){
//divide the array at mid and sort independently using merge sort
mid=(low+high)/2;
mergeSort(arr,low,mid,len);
mergeSort(arr,mid+1,high,len);
//merge or conquer sorted arrays
merge(arr,low,high,mid,len);
}
}
//quick sort
void quickSort(int *arr, int left, int right) {
int i = left, j = right;
int tmp;
int pivot = arr[(left + right) / 2];
/* partition */
while (i <= j) {
while (arr[i] < pivot)
i++;
while (arr[j] > pivot)
j--;
if (i <= j) {
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
i++;
j--;
}
};
/* recursion */
if (left < j){
quickSort(arr, left, j);
}if (i < right){
quickSort(arr, i, right);
}
}
int main(int argc, char *argv[]) {
// switch (getopt(argc, argv, "nt:")) {
// case '--name': // check for name
// cout << "Josh Miltier" << endl;
// return 0;
// }
// check for name
if (string(argv[1]) == "--name") { cout << "Josh Miltier" << endl; return 0; }
const int arraysize = 100;
// cout << "Using array size of " << arraysize << endl
// << endl;
//dynamically allocate arrays
int *array1, *array2;
array1 = new int[arraysize];
array2 = new int[arraysize];
for (int i = 0; i < arraysize; i++) {
array1[i] = rand() % 1000 + 1;
array2[i] = array1[i]; //=array1[i]; //to sure all of the arrays are the same (FAIRNESS!)
}
// printList(array1, arraysize);
time_t begin, end; // time_t is a datatype to store time values.
// quick sort
time(&begin);
quickSort(array1, 0, arraysize - 1);
time(&end);
double timediff = difftime(end, begin);
cout << "Run time: " << timediff << " seconds" << endl
<< endl;
// merge sort
time(&begin);
mergeSort(array2, 0, arraysize - 1, arraysize);
time(&end);
cout << "Run time: " << difftime(end, begin) << " seconds" << endl
<< endl;
arrayCheck(arraysize, array1, array2);
// printList(array1, arraysize);
// clean-up
delete[] array1;
delete[] array2;
return 0;
}