-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.cpp
More file actions
79 lines (65 loc) · 1.78 KB
/
QuickSort.cpp
File metadata and controls
79 lines (65 loc) · 1.78 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
#include <bits/stdc++.h>
#include <iostream>
#include<vector>
#include<chrono>
#include <cstdlib>
#include <ctime>
using namespace std;
using namespace std::chrono;
void print(int *arr, int size){
for (int i= 0; i<size; i++){
cout << *(arr+i) <<" ";
}
cout <<endl;
}
void quick_sort(int *arr, int first, int last){
if ((last- first)>1){
int pivot = *(arr +last-1);
int i = first-1;
int temp;
for(int j=first ;j<last-1;j++){
if (*(arr+j)< pivot ){
i++;
temp = *(arr+i);
*(arr+i) = *(arr+j);
*(arr+j) = temp;
//print(arr,5 );
}
}
i++;
*(arr+last-1) = *(arr+i);
*(arr+i) = pivot;
quick_sort(&arr[0], first, i);
quick_sort(&arr[0], i+1, last);
}
}
int main(){
auto start = high_resolution_clock::now();
// Initialize random number generator
srand(time(0));
// Get array size from user
int size =1000;
// Allocate memory for the array
int* arr = new int[size];
// Fill array with random numbers between 1 and 100
for(int i = 0; i < size; i++) {
arr[i] = rand() % 20000;
}
/*cout << "Original array: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;*/
//print(arr, size);
quick_sort(arr,0,size);
// cout << "Sorted array: ";
// for (int i = 0; i < size; i++) {
// cout << arr[i] << " ";
// }
// cout << endl;
auto stop = high_resolution_clock::now();
auto duration = duration_cast<microseconds>(stop - start);
double time = duration.count();
cout << "time taken is: " << time << "micro seconds" << endl;
return 0;
}