-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment3.cpp
181 lines (157 loc) · 3.64 KB
/
assignment3.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
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
#include<iostream>
#include<ctime>
#include<cstdlib>
#include<chrono>
using namespace std;
using namespace std::chrono;
class SortingAnalysis{
public:
//bubble sort
void bubbleSort(int nums[],int n){
for(int i=0;i<n-1;i++){
for(int j=0;j<n-i-1;j++){
if(nums[j]>nums[j+1]){
int temp=nums[j];
nums[j]=nums[j+1];
nums[j+1]=temp;
}
}
}
}
//insertion sort
void insertionSort(int nums[],int n){
for(int i=0;i<n-1;i++){
for(int j=i+1;j>0;j--){
if(nums[j]<nums[j-1]){
int temp=nums[j];
nums[j]=nums[j-1];
nums[j-1]=temp;
}
}
}
}
//selection sort
void selecionSort(int nums[],int n){
for(int i=0;i<n;i++){
int max=findMax(nums,i,n);
//lets swap the curr max with the last element
int temp=nums[max];
nums[max]=nums[n-i-1];
nums[n-i-1]=temp;
}
}
//merger sort
//merger sort
void mergeSort(int arr[],int start,int end){
if(start>=end) return;
int mid=(start+end)/2;
mergeSort(arr,start,mid);
mergeSort(arr,mid+1,end);
merge(arr,start,mid,end);
}
void merge(int arr[],int s,int m,int e){
int *a=new int[e-s+1];
int i=s;
int j=m+1;
int k=0;
while (i<=m && j<=e)
{
if(arr[i]<=arr[j]){
a[k]=arr[i++];
}
else{
a[k]=arr[j++];
}
k++;
}
while (i<=m)
{
a[k]=arr[i++];
k++;
}
while (j<=e)
{
a[k]=arr[j++];
k++;
}
for(int l=0;l<e-s+1;l++){
arr[l+s]=a[l];
}
}
//quick sort
int partition(int arr[],int low,int high){
int s=low;
int e=high;
int pivot=low;
while(s<=e){
while(s<=e && arr[s]<=arr[pivot]) s++;
while(s<=e && arr[e]>arr[pivot]) e--;
if(s<e){
swap(arr[s],arr[e]);
}
}
swap(arr[pivot],arr[e]);
pivot=e;
return pivot;
}
void quickSort(int arr[],int low,int high){
if(low<high){
int idx=partition(arr,low,high);
quickSort(arr,low,idx-1);
quickSort(arr,idx+1,high);
}
}
//heap sort
void heapify(int arr[], int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if(left < n && arr[largest] < arr[left]) {
largest = left;
}
if(right < n && arr[largest] < arr[right]) {
largest = right;
}
if(largest != i) {
swap(arr[largest], arr[i]);
heapify(arr, n, largest);
}
}
void heapSort(int arr[], int n) {
for(int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
for(int i = n - 1; i > 0; i--) {
swap(arr[0], arr[i]);
heapify(arr, i, 0);
}
}
private:
int findMax(int nums[],int idx,int size){
int maxx=0;
for(int i=0;i<=size-idx-1;i++){
if(nums[i]>nums[maxx]) maxx=i;
}
return maxx;
}
};
void tester(){
int size[]={100,500,1000,2000,5000,10000,25000,50000,100000};
for(int j=0;j<9;j++){
int n=size[j];
int * arr=new int[n];
for(int i=0;i<n;i++){
arr[i]=rand()%n;
}
SortingAnalysis s;
auto before=high_resolution_clock::now();
s.heapSort(arr,n);
auto after=high_resolution_clock::now();
auto duration = duration_cast<milliseconds>(after-before);
cout<<"time for "<<n<<"="<<duration.count()<<endl;
}
}
int main(){
tester();
return 0;
}