-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path564_01fe20bcs327_eval3.cpp
More file actions
151 lines (139 loc) · 3.31 KB
/
564_01fe20bcs327_eval3.cpp
File metadata and controls
151 lines (139 loc) · 3.31 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
#include<iostream>
#include<time.h>
#include<stdlib.h>
using namespace std;
void merge_arays(int arr[],int start,int end,int mid,long int &swaps_merge)
{
int size=(end-start)+1;
int *arr1=new int[size];
int k=0;
int i=start;
int j=mid+1;
//first we will copy all the elements in chronological order till where possible
while(i<=mid && j<=end)
{
if(arr[i]<arr[j])
arr1[k++]=arr[i++];
else
arr1[k++]=arr[j++];
swaps_merge++;
}
//copy down the remaining elements
while(i<=mid)
arr1[k++]=arr[i++];
while(j<=end)
arr1[k++]=arr[j++];
//copy back all elements from new array to old array
for(int l=0;l<size;l++)
arr[start++]=arr1[l];
}
void merge_sort(int arr[],int start,int end,long int &swaps)
{
if(start>=end)
return;
else
{
int mid=(start+end)/2;
merge_sort(arr,start,mid,swaps);
merge_sort(arr,mid+1,end,swaps);
merge_arays(arr,start,end,mid,swaps);
}
}
void bubble_sort(int arr[],int n,long int &swaps)
{
for(int i=0;i<n-1;i++)
{
for(int j=0;j<n-i-1;j++)
{
if(arr[j]>arr[j+1])
{
swaps++;
int temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}
}
}
}
void insertion_sort(int arr[],int n,long int &swaps)
{
for(int i=1;i<n;i++)
{
int j=i-1;
int temp=arr[i];
while(j>0 && arr[j]>temp)
{
swaps++;
arr[j+1]=arr[j];
j--;
}
arr[j+1]=temp;
}
}
void selection_sort(int arr[],int n,long int &swaps)
{
for(int i=0;i<n-1;i++)
{
int select=i+1;
for(int j=i+1;j<n;j++)
{
if(arr[j]<arr[select])
{
select=j;
}
}
swaps++;
int temp=arr[select];
arr[select]=arr[i];
arr[i]=temp;
}
}
int main()
{
srand(time(NULL));
int n=abs(rand());
n=n%1000000;
int *arr=new int[n];
for(int i=0;i<n;i++)
arr[i]=rand();
long int swaps=0;
clock_t start_time,end_time;
start_time=clock();
bubble_sort(arr,n,swaps);
end_time=clock();
double t=double(end_time-start_time)/CLOCKS_PER_SEC;
cout<<"Total Swaps bubble sort : "<<swaps<<endl;
cout<<"Total time bubble sort : "<<t<<endl;
cout<<endl;
for(int i=0;i<n;i++)
arr[i]=rand();
swaps=0;
start_time=clock();
insertion_sort(arr,n,swaps);
end_time=clock();
t=double(end_time-start_time)/CLOCKS_PER_SEC;
cout<<"Total Swaps insertion sort : "<<swaps<<endl;
cout<<"Total time insertion sort : "<<t<<endl;
cout<<endl;
for(int i=0;i<n;i++)
arr[i]=rand();
swaps=0;
start_time=clock();
selection_sort(arr,n,swaps);
end_time=clock();
t=double(end_time-start_time)/CLOCKS_PER_SEC;
cout<<"Total Swaps selection sort : "<<swaps<<endl;
cout<<"Total time selection sort : "<<t<<endl;
cout<<endl;
for(int i=0;i<n;i++)
arr[i]=rand();
swaps=0;
start_time=clock();
merge_sort(arr,0,n-1,swaps);
end_time=clock();
t=double(end_time-start_time)/CLOCKS_PER_SEC;
cout<<"Total Swaps Merge sort : "<<0<<endl;
cout<<"Total time Merge sort : "<<t<<endl;
cout<<endl;
cout<<"INPUT SIZE : "<<n<<endl;
}