跨平台C++毫秒計時——以Quick Sort演算法為例子,quicksort

來源:互聯網
上載者:User

跨平台C++毫秒計時——以Quick Sort演算法為例子,quicksort

#include <iostream>#include <stdlib.h>using namespace std;void Qsort(int a[],int low,int high){if(low >= high) return;int first = low;int last = high;int key = a[first];while(first < last){while(first < last && a[last] >= key) --last;a[first] = a[last];while(first < last && a[first] <= key) ++first;a[last] = a[first];}a[first] = key;Qsort(a, low, first - 1);Qsort(a, first + 1, high);}int main(int argc, char **argv){//命令列參數數量判斷if (argc == 0) {cout << "Usage: quicksort N\n";return 0;}//命令列第2個參數轉換為長整數long N = atoi(argv[1]);//隨機化數srand( time(0) );//動態分配記憶體int *a;a = new int[N];    //C++for(long i = 0; i < N; i++)a[i] = rand();//計時clock_t t1 = clock();Qsort(a, 0, N-1);/*這裡原文第三個參數要減1否則記憶體泄露*/clock_t t2 = clock();cout << (double)(t2 - t1) / CLOCKS_PER_SEC << "毫秒" << endl;/*//輸出for(int i=0;i<sizeof(a)/sizeof(a[0]);i++){cout<<a[i]<<"";}*///釋放申請的記憶體delete []a;  //C++return 0;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.