Bubble sorting, selection sorting and insertion algorithms, and Bubble Sorting insertion Algorithms

Source: Internet
Author: User

Bubble sorting, selection sorting and insertion algorithms, and Bubble Sorting insertion Algorithms

I. Bubble Arrangement

The principle of Bubble Sorting is as follows. The data is stored in array a [8], with 8 numbers arranged in ascending order as an example.

Assume that the 8 numbers are 4, 9, 10, 3, 2, 14, 11, and 5.

A [0] <a [1] is 4 <9, so the switch location is 9, 4, 10, 3, 2, 14, 11, 5

A [1] <a [2] is 4 <10, so the switch location is 9, 10, 4, 3, 2, 14, 11, 5

A [2]> a [3], that is, 4> 3. The position remains unchanged and the comparison continues.

A [3]> a [4], that is, 3> 2. The position remains unchanged and the comparison continues.

A [4 <a [5] is 2 <14, so the switch location is 9, 10, 4, 3, 14, 2, 11, 5

A [5] <a [6] is 2 <11, so the switch location is 9, 10, 4, 3, 14, 11, 2, 5

A [6] <a [7] is 2 <5, so the switch location is 9, 10, 4, 3, 14, 11, 5, 2

The above comparison process shows that through 7 comparisons, we can determine the minimum number 2 and put it at the end.

In the same way, this process can be determined after 6 comparisons. 3. The sequence is changed :......... 3, 2

After five comparisons, we can determine 4, and the sequence is changed :......... 4, 3, 2

After four comparisons, we can determine 5, and the sequence is changed :......... 5, 4, 3, 2

After three comparisons, we can determine 9, and the number of columns becomes :......... 9, 5, 4, 3, 2

After two comparisons, we can determine 10, and the number of columns is changed :......... 10, 9, 5, 4, 3, 2

After one comparison, we can determine 11, and the number of columns becomes :............ 11, 10, 9, 5, 4, 3, 2 at the same time the maximum number is determined, the number of columns becomes 14, 11, 10, 9, 5, 4, 3, 2

To sum up, we need to compare the eight numbers in the order of 7 cycles. The comparison times of each loop are 7, 6, 5, 4, 3, 2, and 1, respectively.

The following code lists the eight numbers from large to small:

1 # include <stdio. h> 2 int main () 3 {4 int arr [8]; 5 int I, j, temp; 6 for (I = 0; I <8; I ++) // use the for loop to manually enter 8 numbers and place them in the array arr [I] 7 {8 printf ("Enter the % d Data: \ n", I + 1 ); 9 scanf ("% d", & arr [I]); 10} 11 for (I = 0; I <7; I ++) // I from 0 ~ 6. The sequence 12 {13 for (j = 0; j <7-i; j ++) can be discharged only after 7 cycles. // for 7 cycles, comparison times 7, 6, 5, 4, 3, 2, 1 14 {15 if (arr [j] <arr [j + 1]) 16 {17 temp = arr [j]; // Switch location 18 arr [j] = arr [j + 1]; 19 arr [j + 1] = temp; 20} 21} 22} 23 printf ("arranged from big to small: \ n"); // outputs the array 24 for (I = 0; I <8; I ++) 25 {26 printf ("% d", arr [I]); 27} 28 return 0; 29}

Note: To arrange a set of numbers from small to large, change the condition statement in if. arr [j]> arr [j + 1].

 

Ii. Select sorting

The sorting principle is as follows. The 8 numbers are arranged in ascending order as an example. The data is stored in array a [8.

Assume that the 8 numbers are 4, 9, 10, 3, 2, 14, 11, and 5. The first comparison starts with a [0.

A [0] <a [1] is 4 <9, so the switching location is 9, 4, 10, 3, 2, 14, 11, and 5.

A [0] <a [2] is 9 <10, so the switching position is 10, 4, 9, 3, 2, 14, 11, and 5.

A [0]> a [3] is 10> 3, and the position remains unchanged.

A [0]> a [4] is 10> 2, and the position remains unchanged.

A [0] <a [5], that is, 10 <14. Therefore, the switching positions are: 14, 4, 9, 3, 2, 10, 11, and 5.

A [0]> a [6], that is, 14> 11. The position remains unchanged.

A [0]> a [7] is 14> 5, and the position remains unchanged.

From the above comparison process, we can determine the maximum number of 2 by 7 comparisons and put it first.

Similarly, in this loop process, we can determine 11 after 6 comparisons with a [1] As the point, and the number of columns will change to: 14, 11 .........

Taking a [2] As the point, after five comparisons, we can determine 10, and the number of columns will change to: 14, 11, 10 .........

Taking a [3] as the fixed point, after four comparisons, we can determine 9, and the number of columns will change to: 14, 11, 10, 9 .........

Take a [4] as the fixed point. After three comparisons, we can determine 5. The number of columns is changed to: 14, 11, 10, 9, 5 .........

Take a [5] as the fixed point. After two comparisons, we can determine 4. The number of columns is changed to: 14, 11, 10, 9, 5, 4 .........

Take a [6] as the fixed point. After 1 comparison, we can determine 3. The number of columns is changed to: 14, 11, 10, 9, 5, 4, 3 ............ At the same time, the smallest number 2 is determined, and the number of columns becomes 14, 11, 10, 9, 5, 4, 3, 2.

Conclusion: To compare the Order of 8 numbers, we need to cycle 7 times before doing so. The comparison times of each loop are 7, 6, 5, 4, 3, 2, and 1, respectively.

1 # include <stdio. h> 2 int main () 3 {4 int arr [8]; 5 int I, j, temp; 6 for (I = 0; I <8; I ++) // use the for loop to manually enter 8 numbers and coexist in the array arr [I] 7 {8 printf ("Enter the % d Data: \ n ", I + 1); 9 scanf ("% d", & arr [I]); 10} 11 for (I = 0; I <7; I ++) // I ranges from 0 ~ After 6 or 7 cycles, the 8 numbers can be arranged in 12 {13 for (j = I; j <8; j ++) // 7 cycles, number of times of comparison in each loop 7, 6, 5, 4, 3, 2, 114 {15 if (arr [I] <arr [j]) 16 {17 temp = arr [I]; 18 arr [I] = arr [j]; 19 arr [j] = temp; 20} 21} 22} 23 printf ("in ascending order: \ n"); 24 for (I = 0; I <8; I ++) 25 {26 printf ("% d", arr [I]); 27} 28 return 0; 29}

Note: To arrange a set of numbers from small to large, change the condition statement in if. arr [I]> arr [j].

 

3. Insert Algorithms

Insert a number in a sequence (ascending to smallest or ascending) to keep the original sequence. The program code is as follows:

1 # include <stdio. h> 2 int main () 3 {4 int arr [11] = {3, 5, 6, 8, 15, 17, 20, 21, 45, 50, 0}; 5 int num; 6 int I; 7 int biaoji = 0; // Save the table found. The default minimum subscript is 8 printf ("enter a data: \ n"); 9 scanf ("% d ", & num); 10 for (I = 0; I <= 8; I ++) // cyclically locate the position to insert the subscript 11 {12 if (num> = arr [I] & num <= arr [I + 1]) 13 {14 biaoji = I + 1; 15 break; 16} 17 if (num> arr [9]) 18 {19 biaoji = 10; 20 break; 21} 22} 23 for (I = 10; I> biaoji; I --) // shift the number following one by one, null subscript position 24 {25 arr [I] = arr [I-1]; 26} 27 arr [biaoji] = num; // assign the num value to 28 for (I = 0; I <11; I ++) // display result 29 {30 printf ("% d", arr [I]); 31} 32 33 return 0; 34}

 

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.