Cross-platform C ++ millisecond timing -- Taking the Quick Sort Algorithm as an example, quicksort

Source: Internet
Author: User

Cross-platform C ++ millisecond timing -- Taking the Quick Sort Algorithm as an example, 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) {// determine the number of command line parameters if (argc = 0) {cout <"Usage: quicksort N \ n"; return 0 ;} // The 2nd parameters in the command line are converted to a long integer long N = atoi (argv [1]); // randomization number srand (time (0 )); // dynamically allocate memory int * a; a = new int [N]; // C ++ for (long I = 0; I <N; I ++) a [I] = rand (); // timing clock_t t1 = clock (); Qsort (a, 0, N-1 ); /* The third parameter in the original text must be reduced by 1; otherwise, memory leakage */clock_t t2 = clock (); cout <(double) (t2-t1) /CLOCKS_PER_SEC <"millisecond" <endl; // output for (int I = 0; I <sizeof (a)/sizeof (a [0]); I ++) {cout <a [I] <"" ;}* // release the applied memory delete [] a; // C ++ return 0 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.