Introduction to Algorithms: Quick sort and insert sort

Source: Internet
Author: User

Code implementation
1 #ifndef _sort_h2 #define_sort_h3 4 //Goal:quicksort and Insertsort5 //time:12/2/20146 //Author:zrss7 //reference:introduction to Algorithms8 9 classSort {Ten  Public: One     voidQuickSort (intA[],intPintR); A     voidInsertsort (intA[],intPintR); -  - Private: the     intPartitionintA[],intPintR);//For QuickSort - }; -  - intSort::p artition (intA[],intPintr) { +     intx = A[r];//Use A[r] as pivot node -      +     inti = P-1;// point to left part A  at     //divide A Array to three part -     //A[p-i] <= x -     //a[(i + 1)-J] > x -     //a[(j + 1)-R) Unknown -      for(intj = P; J < R; ++j) { -         if(A[j] <=x) { in++i; -             if(I! = j) {//swap a[i] and A[j] to                 inttemp =A[i]; +A[i] =A[j]; -A[J] =temp; the             } *         } $     }Panax Notoginseng  -     //Exchange A[i + 1] and A[r] theA[r] = a[i +1]; +A[i +1] =x; A  the     return(i +1); + } -  $ voidSort::quicksort (intA[],intPintr) { $     if(P <r) { -         intQ =partition (A, p, R); -QuickSort (A, p, Q-1); theQuickSort (A, q +1, R); -     }Wuyi } the  - voidSort::insertsort (intA[],intPintr) { Wu      for(intj = p +1; J <= R; ++j) { -         intKey =A[j]; About         inti = J-1; $          while(I >= p && a[i] > key) {//Move Backward -A[i +1] =A[i]; ---i; -         } A  +         if(i +1! = j) {//Insert A[j] to right position theA[i +1] =key; -         } $     } the } the  the  the #endif
Insert sort vs. Quick Sort Run time comparison

Randomly generate 100,000 int test data

1#include <cstdio>2#include <cstdlib>3#include"windows.h"4#include"sort.h"5 6 intMainvoid) {7     Const intLength =100000;8     intNumber[length];9 Ten      for(inti =0; i < length; ++i) { OneNumber[i] =rand (); A     } -  - sort sort; the  -DWORD st =GetTickCount (); -  -Sort.insertsort (number,0, Length-1); +     //sort.quicksort (number, 0, length-1); -  +DWORD ed =GetTickCount (); A  atprintf"Insertsort Use time:%dms\n", Ed-St); -     //printf ("QuickSort Use time:%dms\n", ed-st); -  -System"Pause"); -     return 0; -}

Insertsort Use time:2200ms

QuickSort Use time:0ms

System Information

System:windows 7 Professional Service Pack 1

Cpu:intel (R) Pentium (r) Dual CPU E2220 @ 2.40GHz 2.40GHz

memory:3.00 GB

Conclusion

The efficiency of fast sorting is better than the insertion sort when the input is 100, 000 orders of magnitude, and the input data is a random value

Introduction to Algorithms: Quick sort and insert sort

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.