C++STL排序算法驗證
/**
這里是vs2010工程中用來測試的程序,一并附上
**/
// mySort.cpp : 定義控制臺應用程序的入口點。
// 關于一些排序的知識:
/
對于一些語言(如java),一般的對象排序,通常比較元素最為費事,而移動元素時比較多。在排序算法中,歸并排序比較次數最少,
所有在java標準庫中排序大多數都是用歸并排序。
對于C++而言,對于一般排序,當對象很大時,復制對象耗時比較多,而比較元素耗時較少。 在排序算法中,快速排序復制對象次數最少。
而C++的標準庫中的排序算法也一般采用快速排序(變種快排或快排與其他排序結合,而不是簡單的采用標準快排)。C++對象比較耗時少,
是因為編譯器處理函數模板的擴展時具有強大的執行在線優化的能力。快速排序也是已知實踐中最快的排序算法,時間復雜度為O(NlogN)。
雖然快排的最壞情況為二次函數,但稍加優化就能有效避免這種情況。/
include "stdafx.h"
include <iostream>
include "quickSort.h"
include "heapSort.h"
include "shellSort.h"
include "mergeSort.h"
include <time.h>
include <algorithm>
define random(x) (rand()%x)
int _tmain(int argc, _TCHAR* argv[])
{
long long start, end;
start = clock();
vector<int> vecInt;
int i= 0;
for (; i < 15; i++)
{
vecInt.push_back(random(67));
cout<<vecInt[i]<<" ";
}
cout << endl;
//sort(vecInt.begin(), vecInt.end());
//quickSort(vecInt);
//heapMaxSort(vecInt);
//heapMinSort(vecInt);
//shellSort(vecInt);
//mergeSort(vecInt);
i = 0;
for (;i < 15; i++)
{
cout<<vecInt[i]<<" ";
}
cout <<endl;
end = clock();
long long time = end -start;
cout<<time<<"@@@@@@@@@@@@"<<endl;
return 0;
}</pre>
本文由用戶 b36g 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!