C++
- voluntas
- @ignis_fatuus
- ブン
- @Linda_pp
- 清楚なC++メイドBOT
- @tzik_tack
- 長谷川一輝
- wraith13
- @jj1bdx
- @cpp_akira
- 安藤敏彦
- @srz_zumix
- Siv3D
- @okdshin
- @hnokx
- @ishidakei
- @take_cheeze
- TAKEI Yuya
- @mumumu
- I (@wx257osn2)
- Tommy6
- わたやん
- @KorekaraSEDB
- @kariya_mitsuru
- @ciniml
- @beam2d
- @grafi_tt
- @nekketsuuu
- LouiS0616
- @volanja
- 大鎌広
- むてら
- ガチKGB
- 三重野賢人
x
66
1
2
3
4
using namespace std;5
using namespace std::chrono;6
7
8
using high_resolution_clock = steady_clock;9
10
struct Data {11
int* array;12
int time;13
};14
void partition(int* array, int first, int last, int &pivotIndex){15
int lastSmaller, firstUnknown;16
int pivot = array[first];17
lastSmaller= first;18
firstUnknown = first+1;19
for(;firstUnknown<=last;firstUnknown++){20
if(array[firstUnknown]<pivot){21
lastSmaller++;22
swap(array[lastSmaller],array[firstUnknown]); //std::swap has been used23
}24
}25
swap(array[first],array[lastSmaller]);26
pivotIndex = lastSmaller;27
// cout<<"End of partitioning" << pivotIndex << array[pivotIndex]<<endl;28
29
}30
31
int* quickSort(int* array, int first, int last){32
33
int pivotIndex;34
if(first<last){ 35
partition(array,first,last, pivotIndex);36
quickSort(array, first, pivotIndex-1);37
quickSort(array, pivotIndex+1, last);38
}39
// cout<<" "; //look at this line, uncommenting this outputs some time else zero40
return array;41
}42
43
Data quickSortWithTime(int* array, int first, int last){44
auto start_time = std::chrono::high_resolution_clock::now();45
46
int* a = quickSort(array, first, last);47
48
auto end_time = std::chrono::high_resolution_clock::now();49
auto duration = duration_cast<microseconds>(end_time - start_time);50
51
cout << "Time taken by function: " << duration.count() << " microseconds" << endl; 52
53
Data d;54
d.array = a;55
d.time = duration.count();56
return d;57
}58
59
int main(){60
int a[19] = {32,34,78,1,889,212,23,90,12,100,345,3254,123,12,1,4,2435,64,636};61
Data b = quickSortWithTime(a,0,999);62
for (int i = 0; i < 19; i++) 63
{cout << b.array[i]<<endl;}64
cout <<endl<< b.time<<endl;65
}66
$ g++ prog.cc -Wall -Wextra -O2 -march=native -std=gnu++2a -pedantic
Start
Time taken by function: 386 microseconds -1744627456 -1339457309 -1331406946 -1328942812 -975058896 -959643648 -959576101 -957386392 -654052623 0 0 0 0 0 0 0 0 0 0 386
Segmentation fault
Finish