Most basic sort: insert Sort, select Sort, and bubble sort implementation

Source: Internet
Author: User

These three sorts, while not very useful, and inefficient, but they are stable sorting algorithm, and easy to confuse, this gives the implementation code to help readers distinguish

First, insert sort

1#include <iostream>2 using namespacestd;3 Const intmaxn=1000005;4 intN;5 intA[MAXN];6 voidInsert_sort (int*a)7 {8      for(intI=2; i<=n;i++)9     {Ten         inttmp=A[i]; One         intj=i-1; A          while(j>=0&&tmp<A[j]) -         { -a[j+1]=A[j]; thej--; -         } -a[j+1]=tmp; -     } + } - intMain () + { AStd::ios::sync_with_stdio (false); atCin>>N; -      for(intI=1; i<=n;i++) -Cin>>A[i]; - Insert_sort (a); -      for(intI=1; i<=n;i++) -cout<<a[i]<<" "; incout<<Endl; -     return 0; to}

The implementation of the insertion sort is really the idea of inserting the current element into the corresponding size of the position, and then the larger than the elements are moved back one, but when writing to pay attention to the subscript control, otherwise error-prone

Then the selection sort

1#include <iostream>2 using namespacestd;3 Const intmaxn=1000005;4 Const intinf=0x7f7f7f7f;5 intN;6 intA[MAXN];7 voidSelect_sort (int*a)8 {9      for(intI=1; i<=n;i++)Ten     { One         inttmp=i; A          for(intj=i+1; j<=n;j++) -         { -             if(a[j]<a[tmp]) thetmp=J; -         } -         intt=A[i]; -a[i]=a[tmp]; +a[tmp]=T; -     } + } A intMain () at { -Std::ios::sync_with_stdio (false); -Cin>>N; -      for(intI=1; i<=n;i++) -Cin>>A[i]; - Select_sort (a); in      for(intI=1; i<=n;i++) -cout<<a[i]<<" "; tocout<<Endl; +     return 0; -}

There are two ways to choose a sort, the first is to constantly go for the minimum, each to find a minimum value in an array, and then in the original array to mark it, calculate the next minimum value of the time no longer to access it

The second method is best written in all sorting algorithms (do not mention the sort function), we enumerate each position, and for each position, enumerate all elements after this position, and change the smallest one.

This article provides the code for the second method

Preferably a bubble sort

1#include <iostream>2 using namespacestd;3 Const intmaxn=1000005;4 Const intinf=0x7f7f7f7f;5 intN;6 intA[MAXN];7 voidBubble_sort (int*a)8 {9      for(intI=1; i<=n;i++)Ten      for(intj=1; j<=n-i;j++) One     { A         if(a[j]>a[j+1]) -         { -             intt=A[j]; thea[j]=a[j+1]; -a[j+1]=T; -         } -     } + }  - intMain () + { AStd::ios::sync_with_stdio (false); atCin>>N; -      for(intI=1; i<=n;i++) -Cin>>A[i]; - Bubble_sort (a); -      for(intI=1; i<=n;i++) -cout<<a[i]<<" "; incout<<Endl; -     return 0; to}

Remember bubble sort just need to remember a little, each time is two adjacent elements to compare and exchange, each time the big element has been changed to the back, a total of n times in the order of the line

Here we can add an optimization, we do each round of exchange when the number of elements can be reduced by 1 each time, why? Because each time a sort, there is an element has been sequenced and placed in the back, and then the number is smaller than it, you do not have to compare again

The time complexity of these three sorting algorithms is O (n^2), but the different data may show different efficiency, they are stable sorting algorithm

Most basic sort: insert Sort, select Sort, and bubble sort implementation

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.