One of the common sorting methods-Bubble sorting method and selection sorting method, and bubble Selection

Source: Internet
Author: User

One of the common sorting methods-Bubble sorting method and selection sorting method, and bubble Selection

Commonly used algorithms include Bubble sorting, fast sorting, insert sorting, selection sorting, Hill sorting, heap sorting, and merge sorting. From this point on, I will summarize these sorting methods respectively.

First, we define the array to be sorted as arr [N], that is, the array arr [] contains N elements.

# Bubblesort )##

The so-called sorting method is to sort a group of unordered sequences in sequence (from large to small or from small to large). So what is the Bubble sorting method, how can we sort arrays in sequence by Bubble sorting.

The specific implementation method of the bubble sort method is as follows, starting from the first element 'Arr [0] 'of the array, and comparing two or two ** ('Arr [n], arr [n + 1] '). If the preceding number is greater than the following number ('Arr [n]> arr [n + 1]'), the positions of the two elements are exchanged, move a large number backwards. After a round of comparison, the maximum number will be switched to the final position (arr [n-1]).

First, let's take a look at how the Bubble sorting method is sorted.
 
Array sorting Top 7 23 12 4 33 21 2 17 13 9
First round sorting 7 12 4 23 21 2 17 13 9 33
Sorting in the second round 7 4 12 21 2 17 13 9 23
Round 3 sorting 4 7 12 2 17 13 9 21
Fourth Round of sorting 4 7 2 12 13 9 17
Fifth Round of sorting 4 2 7 12 9 13
Sorting in the sixth round 2 4 7 9 12
Seventh round of sorting 2 4 7 9
Eighth round of sorting 2 4 7
9-round sorting 2 4

We can see that the maximum number of elements involved in the comparison in each round will be reached the end. The name of the bubble sort also comes from here.

Bubblesort in C language:

1 void bubblesort (int a [], int m) 2 {3 int I, j; 4 int tmp; 5 int flag = 0; // set the flag, if there is no exchange during the first loop comparison, the array is sorted in ascending order, and the loop is terminated in advance. 6 for (I = 0; I <m; I ++) // Number of loops in the outer loop control 7 {8 for (j = 0; j <m-1-i; j ++) // The inner loop controls the number of comparisons in each loop. 9 {10 if (a [j]> a [j + 1]) 11 {12 tmp = a [j]; 13 a [j] = a [j + 1]; 14 a [j + 1] = tmp; 15 flag = 1; 16} 17} 18 19 if (0 = flag) 20 {21 printf ("No Sort \ n"); 22 break; 23} 24} 25}

# Selectionsort )##

What is the meaning of the so-called choice? The choice is to choose one of the thousands of flowers. what is said in the selection sorting method is that in every cycle process, select the ** most value ** you need through comparison **.

The process of selecting the sorting method is to ** compare, select the most valuable element in each round, and then exchange it with the most advanced element in this round **, therefore, the key of this algorithm is to record the results of each comparison, that is, the maximum position (subscript) after each comparison ).

Let's take a look at the process of selecting sorting:

Array sorting Top 7 23 12 4 33 21 2 17 13 9
First round cycle 2 23 12 4 33 21 7 17 13 9
Round 2 cycle 4 12 23 33 21 7 17 13 9
Round 3 cycle 7 23 33 21 12 17 13 9
Round 4 loop 9 33 21 12 17 13 23
Round 5 cycle 12 21 33 17 13 23
Round 6 loop 13 33 17 21 23
Round 7 loop 17 33 21 23
Eighth Cycle 21 33 22
Round 9 Cycle 22 33

Through this process, we can see that the most value element will be found during each round of loop, and this element will not be considered in the next round of sorting.

C language implementation (Selectionsort)

1 void selectionsort (int a [], int m) 2 {3 int I, j; 4 int k; 5 int tmp; 6 7 for (I = 0; I <m-1; I ++) // control the number of loops. n count requires n-1 cycles. 8 {9 k = I; 10 for (j = I + 1; j <m; j ++) 11 {12 if (a [j] <a [k]) 13 k = j; 14} 15 // if I is not k, it means that a [I] is not the smallest, 16 // if I is equal to k, it means that a [I] is the minimum value of 17 if (I! = K) 18 {19 tmp = a [I]; 20 a [I] = a [k]; 21 a [k] = tmp; 22} 23} 24}

 
# Summary ##
The Bubble sorting method compares and exchanges data in sequence. The number of exchanges is large.
The sorting method is used to find the most value in each loop. After the loop ends, the maximum value is adjusted to the appropriate position, and the number of exchanges is small.
 

Related Article

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.