[Algorithm] Simple selection of sorting C language implementation, algorithm sorting C Language

Source: Internet
Author: User

[Algorithm] Simple selection of sorting C language implementation, algorithm sorting C Language

In the previous article, we talked about the Bubble sorting and implemented the Bubble Sorting in two versions. Do you have a summary of the characteristics of the Bubble sorting? In fact, Bubble Sorting is quite violent because it frequently exchanges data. In this case, our computer computing frequency will be very high, so it is very inefficient in general, so can we find a method with fewer exchanges? This introduces the simple selection and sorting algorithm.


The basic idea of Simple selection sorting is to select a keyword minimum record from N-I + 1 record by comparing the keywords of N-1 times, and (1 <= I <= nIn fact, simple sorting and Bubble sorting are almost the same in terms of thinking, but the exchange of Bubble Sorting is changed to comparison by simple sorting, we need to assign values three times for the exchange of two values, and the comparison only needs one time. This reduces the number of operations, so it is more efficient (compared with Bubble sorting, in fact, both of them are very inefficient ).


Okay, let's get the code first.


Void simple_selection_sort (int array [], int length) {int I; int j; int min; int temp; for (I = 0; I <length; I ++) {min = I; // by default, the elements of the current base are defined as the minimum value for (j = I + 1; j <length; j ++) {if (array [min]> array [j]) {// This is a process for finding the minimum value min = j ;}} if (min! = I) {// if the minimum value changes and is no longer the I-th element, switch their location temp = array [I]; array [I] = array [min]; array [min] = temp ;}}}


Here, the min value indicates the minimum value. For example, at the beginning, we set the min value to 0, which means that the element whose subscript is 0 is the minimum value, then traverse the system backward and compare it with the min value. If it is smaller than the min value, change the min value to the subscript of the current element. After the loop ends, compare the values of min with the subscript at the beginning of the loop. If the values are not equal, exchange them. next traversal is performed until the sorting ends.


The following describes how to simply select the sorting execution process by combining an example.

Suppose that the sequence to be sorted is {9, 1, 5, 8, 3, 7, 4, 6, 2}, We need to round the I from 0 to 7, when I = 0, the value of array [I] = 9, min is 0, and then j = 1 ~ The size of array [min] and array [j] between 8. Because min is the minimum value when it is equal to 1, min = 1, finally, the positions of array [0] and array [1] are switched. it is worth noting that only one exchange is conducted here.



The subsequent cycles are almost the same as this one, but the actual position is only behind the back.


In fact, the core of simple sorting is to mark two words. It uses the min tag to replace the repeated exchange of Bubble sorting, so as to improve efficiency. Finally, its time complexity is still O (n ^ 2 ). OK. Thank you.

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.