Chinese University mooc-data Structure basic problem sets, 07-1, sorting

Source: Internet
Author: User

title Link:http://www.patest.cn/contests/mooc-ds/07-1

Title Analysis: This two parts in the test of various different sorting algorithms in various data situations performance. Therefore there is no standard answer. The purpose of this post is to give the C + + implementation form of various sorting algorithms, and compare and summarize the results. Hope to bring you help .

Code for various sorting algorithms:

1. Bubble Sort:

1 voidBubble_sort (ElementType a[],intN)2 {3      for(intp=n-1; p>=0; p--)4     {5         intFlag =0;6          for(intI=0; i<p; i++)/*a trip to bubble sort*/7         {8             if(A[i] > a[i+1])9             {TenElementType temp = a[i+1]; Onea[i+1] =A[i]; AA[i] =temp; -Flag =1; -             } the         } -         if(Flag = =0) Break; -     } -}

2. Insert Sort:

1 voidInsertion_sort (ElementType a[],intN)2 {3      for(intp=1; p<n; p++)4     {5ElementType Tmp = a[p];/*Touch a card*/6         inti;7          for(I=p; i>0&& a[i-1]>tmp; i--)8A[i] = a[i-1];/*move out of empty space*/9A[i] = TMP;/*new card Drop position*/Ten     } One}

3. Hill sort

1 voidShell_sort (ElementType a[],intN)2 {3      for(intd=n/2; D>0; D/=2)/*Hill Increment sequence*/4     {5          for(intP=d; p<n; p++)6         {7ElementType TMP =A[p];8             inti;9              for(i=p; I>=d && a[i-d]>tmp; i-=D)TenA[i] = a[i-D]; OneA[i] =Tmp; A         } -     } -}

4. Select sort

1 voidSelection_sort (ElementType a[],intN)2 {3      for(intI=0; i<n; i++)4     {5         intMinposition =i;6         intMinnum =A[i];7          for(intJ=i; j<n; J + +)8         {9             if(A[j] <minnum)Ten             { OneMinnum =A[j]; AMinposition =J; -             } -         } theElementType tmp =A[i]; -A[i] =A[minposition]; -A[minposition] =tmp; -     } +}

5. header file Declaration and main function

1#include <iostream>2#include <algorithm>3 4 #defineElementType int5 #defineMaxnum 1000000006 7 using namespacestd;8 9 intcmpConst void*a,Const void*b)Ten { One     return*(int*) A-* (int*) b; A } -Insert the above several functions here - intMain () the { - ElementType N; -CIN >>N; -ElementType *a =New ElementType[n]; +      for(intI=0; i<n; i++) -CIN >>A[i]; +     //Bubble_sort (A, n); A     //Insertion_sort (A, n); at     //Shell_sort (A, n); -     //Selection_sort (A, n); -     //sort (A, a+n); -     //Stable_sort (A, a+n); -Qsort (A, N,sizeof(int), CMP); -      for(intI=0; i<n; i++) in     { -         if(I! = N1) tocout << A[i] <<" "; +         Else -cout <<A[i]; the     } *     return 0; $}

Summary of various sorting algorithms:

Time complexity is the unit is MS, space complexity of the unit is KB, if there are errors in the table, please leave a message!

Of course, the following sort function is only a few parts, heap sorting and merge sort code is relatively large, bloggers have time to update. As you can see from the table, O (NLOGN) is really a lot faster!

Sorting algorithms Complexity of Time Stability CASE0: only 1 Case1:11 Case2:10^3 Case3:10^4 Case4:10^5 Case5:10^5 Order CASE6:10^5 reverse Order CASE7:10^5 Basic Order case8:10^5 Random positive integers
Bubble sort O (n^2) Stability 1 1 3 155 Timeout 39 Timeout 278 Timeout
Insert Sort O (n^2) Stability 1 1 2 21st 175T 39 3266 53 1652
Hill sort O (NLGN) Not stable 1 1 1 6 50 41 42 41 42
Select sort O (n^2) Stability 1 1 2 38 3279 3265 3266 3267 3267
Sort function O (NLGN) Not stable 1 1 2 5 44 40 47 44 36
Stable_sort function O (NLGN) Stability 1 1 1 5 45 41 41 40 38
Qsort function O (NLGN) Not stable 1 1 2 6 49 42 43 43 41

  

Sorting algorithms Complexity of space Stability CASE0: only 1 Case1:11 Case2:10^3 Case3:10^4 Case4:10^5 Case5:10^5 Order CASE6:10^5 reverse Order CASE7:10^5 Basic Order case8:10^5 Random positive integers
Bubble sort O () Stability 360 256 360 456 Timeout 1156 Timeout 1140 Timeout
Insert Sort O () Stability 236 284 232 260 1152 1260 1140 1184 1024
Hill sort O () Not stable 232 232 232 360 1128 1256 1128 1128 1000
Select sort O () Stability 284 232 232 376 1128 1144 1150 1180 1004
Sort function O () Not stable 232 236 364 376 1156 1152 1140 1180 1004
Stable_sort function O () Stability 232 232 232 360 1188 1188 1188 1188 1060
Qsort function O () Not stable 364 236 364 284 1144 1232 1144 1156 1136

Chinese University mooc-data Structure basic problem sets, 07-1, 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.