C Language Choice sorting algorithm and implementation code _c language

Source: Internet
Author: User

Selection sorting is a sort of algorithm, here from small to large sorting as an example to explain.

Basic ideas and illustrative examples

The basic idea of choosing a sort (from small to large) is to first select the smallest number and place it in the first position; then, pick the second small number and put it in the second position, and so on until all the numbers are sorted from small to large.

In implementation, we usually first determine the position of the small number I, and then exchange it with the number of I.

Next, select the sort procedure by selecting 3 2 4 1, using Min_index to record the location of the current smallest number.

1th-round sorting process (looking for the location of the 1th small number)

3 2 4 1 (originally, Min_index=1)
3 2 4 1 (3 > 2, so min_index=2)
3 2 4 1 (2 < 4, so min_index=2)
3 2 4 1 (2 > 1, so min_index=4, this time determined the 1th small number at position 4)
1 2 4 3 (1th round results, Exchange 3 and 1, i.e. position 1 and position 4 Exchange)

2nd-round sorting process (looking for the location of the 2nd small number)

1 2 4 3 (1th round results, min_index=2, only need to start looking from position 2)
1 2 4 3 (4 > 2, so min_index=2)
1 2 4 3 (3 > 2, so min_index=2)
1 2 4 3 (2nd round result, because the min_index position is just in the 2nd position, without swapping)

3rd-round sorting process (looking for the location of the 3rd small number)

1 2 4 3 (2nd round results, min_index=3, only need to start looking from position 2)
1 2 4 3 (4 > 3, so min_index=4)
1 2 3 4 (3rd round results, Exchange 3 and 4, i.e. position 4 and position 3 Exchange)

This completes the sorting.

Summary and implementation

#include <stdio.h>
#include <stdlib.h>
#define N 8
void select_sort (int a[],int n);
Select Sort implementation
void Select_sort (int a[],int N)//n number of elements in array a
{
  //N-1 wheel selection for
  (int i=0; i<n-1; i++)
  {
    int min_index = i; 
    Find out where the small number i is in for
    (int j=i+1; j<n; j + +)
    {
      if (A[J) < A[min_index])
      {
        min_index = j;
      }
    }
    Place the small number of I in the first position, and if it happens, do not exchange if
    (I!= min_index)
    {
      int temp = a[i];
      A[i] = A[min_index];
      A[min_index] = temp;
    }
}} int main ()
{
  int num[n] = {m, M, M, M, N, MB;
  Select_sort (num, N);
  for (int i=0; i<n; i++)
    printf ("%d", Num[i]);
  printf ("\ n");
  System ("pause");
  return 0;
}

Select sort to sort unordered array r[n with size N for N-1 wheel selection process. I select the small number of the first wheel and place it in the first position. When the first N-1 is completed, the number of the nth (i.e. the largest) is naturally in the final position.

The following is a C-language implementation of the Select sort.

Note: Choosing a sort is an unstable sort algorithm that may disrupt the original order of two identical digits.

For example, the sequence 5 8 5 2 9, sorted from small to large, the first round will exchange the 1th digit 5 and 2, then the relative order of 2 5 in the original sequence is destroyed, so the selection order is an unstable sort algorithm.

The above is the bubble sort algorithm simple example, follow-up to continue to collate relevant information, thank you for the support of this site!

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.