-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort.cpp
More file actions
165 lines (142 loc) · 2.98 KB
/
Copy pathsort.cpp
File metadata and controls
165 lines (142 loc) · 2.98 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
156
157
158
159
160
161
162
163
164
#include<iostream>
#include<time.h>
#include<chrono>
using namespace std;
using namespace std::chrono;
void swap(int *xp, int *yp)
{
int temp = *xp;
*xp = *yp;
*yp = temp;
}
int partition (int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low - 1);
for (int j = low; j <= high - 1; j++)
{
if (arr[j] < pivot)
{
i++;
swap(&arr[i], &arr[j]);
}
}
swap(&arr[i + 1], &arr[high]);
return (i + 1);
}
void quickSort(int arr[], int low, int high)
{
auto start = high_resolution_clock::now();
if (low < high)
{
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
auto stop = high_resolution_clock::now();
auto duration = duration_cast<nanoseconds>(stop - start);
cout << "Time taken by function: "<< duration.count() << " nanoseconds" << endl;
}
void merge(int arr[], int l, int m, int r)
{
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
int L[n1], R[n2];
for (i = 0; i < n1; i++)
L[i] = arr[l + i];
for (j = 0; j < n2; j++)
R[j] = arr[m + 1+ j];
i = 0;
j = 0;
k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
void mergeSort(int arr[], int l, int r)
{
auto start = high_resolution_clock::now();
if (l < r)
{
int m = l+(r-l)/2;
mergeSort(arr, l, m);
mergeSort(arr, m+1, r);
merge(arr, l, m, r);
}
auto stop = high_resolution_clock::now();
auto duration = duration_cast<nanoseconds>(stop - start);
cout << "Time taken by function: "<< duration.count() << " nanoseconds" << endl;
}
void printArray(int arr[], int size)
{
int i;
cout <<"\nThe Sorted Array is \n";
for (i=0; i < size; i++)
cout <<arr[i] << " \n";
cout << endl;
}
int main()
{
int ch;
int i,arr[5];
int userBeg,userEnd;
cout<<"Enter a start value"<<endl;
cin>>userBeg;
cout<<"Enter an end value"<<endl;
cin>>userEnd;
srand(time(NULL));
for(i=0;i<5;i++)
arr[i]=rand()%((userEnd-userBeg)+1)+userBeg;
cout<<"\nThe Array is : \n";
for(i=0;i<5;i++)
cout<<arr[i]<<" ";
do
{
cout<<"\n1.Quick Sort\n2.Merge Sort\n3.Exit"<<endl;
cout<<"Choose your choice: ";
cin>>ch;
int n = sizeof(arr) / sizeof(arr[0]);
switch(ch)
{
case 1:
quickSort(arr, 0, n - 1);
printArray(arr, n);
break;
case 2:
mergeSort(arr, 0, n - 1);
printArray(arr, n);
break;
case 3:
cout<<"Thank You"<<endl;
break;
default:
cout<<"Wrong Input"<<endl;
break;
}
}while(ch!=3);
return 0;
}