Dark Horse programmer--bubble sort and selection sort--familiar and unfamiliar sort method

Source: Internet
Author: User

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

Yesterday to do the basic test questions, encountered a sort of problem, after writing the brain suddenly jumped out of the "bubble sort method" of the term. "Bubble sorting" and "choice of sorting" is a beginner will learn the two sorting method, but a lot of people on the two sorting methods are both familiar and unfamiliar, familiar is because generally encountered in the sort of general will think of these two methods, unfamiliar is because of the two methods of the principle and the difference is not very clear. Not really good, beginners a serious is easy to get dizzy by these two methods, I was also the two methods to get dizzy, and then seriously step-by-step analysis of the two methods, and a summary. Okay, no more nonsense, first on the code:

Arrange the 1,2,3,4,5,6,7,8,9 according to the size from the big to the small.

1. Bubble sort

1#include <stdio.h>2 3 intMain ()4 {5     intarry[9] = {1,2,3,4,5,6,7,8,9};6     intTEM =0;7 8      for(intI=0; i<8; i++)//control the number of times when sorting9     {Ten          for(intj=0; j<8-I.; J + +)//control the subscript for each trip sort One         { A             if(arry[j]<arry[j+1]) -             { -TEM =Arry[j]; theARRY[J] = arry[j+1]; -arry[j+1] =tem; -             } -         } +     } -  +printf"array elements are arranged from large to small: \ n"); A      for(intI=0; i<9; i++) at     { -printf"%d\n", Arry[i]); -     } -     return 0; -}


2. Select sort

1#include <stdio.h>2 3 intMain ()4 {5     intarry[9] = {1,2,3,4,5,6,7,8,9};6     intTEM =0;7 8      for(intI=0; i<8; i++)//control number of trips9     {Ten          for(intj=i+1; j<9; J + +)//control the number of times per trip One         { A             if(arry[i]<Arry[j]) -             { -TEM =Arry[i]; theArry[i] =Arry[j]; -ARRY[J] =tem; -             } -         } +     } -  +printf"array elements are arranged from large to small: \ n"); A      for(intI=0; i<9; i++) at     { -printf"%d\n", Arry[i]); -     } -     return 0; -}

Summarize:
1. Bubble sort: As shown in the example above,

The 0th trip, the No. 0 and 1th position of the comparison, if No. 0 is less than 1th, then exchange their position, and then the 1th and 2nd position to compare, if the 1th is less than 2nd, then exchange their position; Compare the 7th to the 8th position, if 7th is less than 8th, the position is exchanged, and the 0th is completed, and the smallest digit in all the numbers is already at the lowest point. So the next trip doesn't have to compare the value of the last position.

The first trip, the No. 0 and 1th position of the comparison, if No. 0 is less than 1th, then exchange their position, and then the 1th and 2nd position to compare, if the 1th is less than 2nd, then exchange their position; Comparing the 6th and 7th positions, if 6th is less than 7th, the position is exchanged, and the first trip is completed, and the second-lowest number in all numbers is already in the second-to-last position. So the next trip doesn't have to compare the values of the last two positions.

......

Seventh trip, the No. 0 and the first position of the comparison, if No. 0 is less than 1th, then exchange their position, at this time the value of the back 7 positions have been arranged from large to small, so do not compare.

2. Select sort: As shown in the example above,

When i=0, the value of the No. 0 position is compared with the value of each position behind it, and if the value of the No. 0 position is less than the value at that position, the position is exchanged; when compared to the last position, the largest value in all values is already in the No. 0 position.

Similarly, when I=1 is done, the second largest value is already in the 1th position.

......

When the i=7 is finished, all the values have been sorted from large to small.

3. The difference between bubble sorting and selection sorting is:

The bubbling sort is 22 compared and then a trip through the loop until the order is sorted (the lowest value in the example is determined by the position, arranged sequentially until the maximum value is finally determined);

The selection sort is compared from the first position of the number to the following number (in the example, the maximum value is determined by the position, arranged sequentially until the minimum value is finally determined).

------Java Training, Android training, iOS training,. NET training, look forward to communicating with you! -------

Dark Horse programmer--bubble sort and selection sort--familiar and unfamiliar sort method

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.