Selection and sequencing of data structure sorting algorithms

Source: Internet
Author: User

Today we continue to introduce a sort algorithm: select sort.

The basic idea of selecting a sort is to select the smallest one from the sequence to be sorted, and then swap the position of the selected element with the first element of the sequence (currently the default is ascending), then the first element is the smallest element of the entire sequence when the interchange is complete, and the end of the order is selected once. Then we select the smallest one from the remaining subsequence and then swap the selected element with the first element of the subsequence (that is, the second element of the entire sequence), then the second element of the current entire sequence is the minor minimum in the current sequence, and the second selection is the end of the sort. And so on, until only one element is left in the pending sequence, the entire sequence is ordered.

The exact process is as follows:

Here is not much to say, directly on the code, there are almost every word in the comments:

1#include <stdio.h>2 3 /*4 Simple Selection Sort (assumed ascending order):5 The simple selection sort principle is to take a minimum value from the current sequence and swap the currently selected minimum value with the first value. Then the minimum value is then selected from the remaining sequence, and the sequence6 the second interchange until the current sequence is left with only one value, and the entire sequence is an ordered sequence. 7 */8 9 voidEasyselectsort (int*arr[],intlen);Ten intSelectminpos (intArr[],intLeninti); One  A  - intMainintargcChar*argv[]) - { the      -     intarr[5]={ -         3, +, +,1,8 -     }; +Easyselectsort (arr,5); -     inti; +      for(i=0;i<5; i++){ Aprintf"%d", Arr[i]); at     } -      -     return 0; - } -  - /* in Sorting Algorithms - */ to voidEasyselectsort (int*arr[],intLen) { +     //since it is a matter of decreasing the minimum value from left to right, it is necessary to traverse the entire sequence -     intJ//defines an shaping variable J, which stores the subscript of the selected minimum value the     intTemp//Define a TEMP variable temp is used to store temporary variables when swapping *     inti; $      for(i=0; i<len;i++){      Panax NotoginsengJ=selectminpos (Arr,len,i);//The current method represents the operation to select the minimum value for the sequence of arr array from I to arr.length-1, and assigns the subscript to J -      the         if(I!=J) {//It is only when I and J are different that the position of the smallest value chosen is not the position at which the position of the element is to be interchanged. +temp=Arr[j]; Aarr[j]=Arr[i]; thearr[i]=temp; +         } -     }  $ } $  - /* - method of selecting the minimum element position the arr: Selected Array - I: From what subscript start to the selected sequence of Arr.length-1Wuyi */ the intSelectminpos (intArr[],intLeninti) { -     intPos=i;//define a POS position variable, and assign the default value to I Wu      for(i;i<len;i++){ -         /** About We compare from POS (that is, I) to pos=i when the corresponding array value is greater than the value of the variable i corresponds to; this is equivalent to assuming that the POS (i.e. i) subscript $ the corresponding array value is the smallest, and then each take the back (i++) value and the current POS subscript corresponding value for comparison, found that the next I assigned to the POS, so traverse over - then we can find the final minimum value corresponding to the subscript -         */  -         if(arr[pos]>Arr[i]) {   Apos=i; +         } the     } -     returnPos; $}

The above is the selection of the principle of sorting algorithm, diagram and code implementation, this is only my own understanding, if there is a wrong place also ask you to point out.

Above blog is the main original, reproduced please indicate the source, thank you.

Selection and sequencing of data structure sorting algorithms

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.