Common sorting methods in C language

Source: Internet
Author: User

In C language, the common sort methods are bubbling, sorting, inserting and so on. The so-called bubble method, is a set of numbers from the big to small or small to large, a sort of method. The main thing is that the adjacent values are exchanged. Starting with the first value, if the next two values are sorted in a position other than the one we want, the two-digit position is exchanged. If they are consistent, they will not be exchanged. This process is repeated until no values need to be exchanged, and the sort is complete. The following is a sequence of the results of 10 students in a bubbling manner:

1#include <stdio.h>2 intMain ()3 {4     inti;5     intarr[Ten];6      for(i=0;i<Ten; i++)7     {8printf"Please enter the score of%d students: \ n", i+1);9scanf"%d",&arr[i]);Ten      } One  A      intJ; -       for(j=0;j<9; j + +) -      { the          for(i=0;i<9; i++)//Cycle 9 times -           { -               intTemp=arr[i];//Arr[i] is assigned to a variable, arr[i+1] is assigned to Arr[i], at which point the adjacent two values are swapped.  -arr[i]=arr[i+1]; +arr[i+1]=temp; -           } +       } A        for(i=0;i<Ten; i++)     at       { -printf"%d\n",&arr[i]); -     } -  -  - return 0; in}

Selection method:

The selection method is to use a variable to continuously select a small number, pay the value to the variable and then pass the variable to the corresponding position of the array element ...

#include <stdio.h>intMain () {inti; intarr[Ten];  for(i=0;i<Ten; i++) {printf ("Please enter the score of%d students: \ n"); scanf ("%d",&Arr[i]); }    intJ;  for(j=0;j<Ten; j + +)    {        for(i=j;i<Ten; i++)         {            if(Arr[j]>arr[i])//Judge Arr[j]>arr[i], the condition will be arr[j] and Arr[i] Exchange              {                      inttemp=Arr[j]; ARR[J]=Arr[i]; Ar[i]=temp; }          }     }     for(i=0;i<Ten; i++) {printf ("%d",&Arr[i]); }return 0;}

Insert Method:

#include <stdio.h>intMain () {intarr[7]={2,4,5, at, $, +}; intnum; printf ("Please enter a number: \ n", num); scanf ("%d",&num); intI=0;//initialized to 0   intindex=6;  for(;i<6; i++)   {       if(num>=arr[i]&&num<arr[i+1])//if the value of the input is greater than or equal to the previous item and is less than the latter, then the value is inserted into the next item.{Index=i+1;  Break; }       if(num<arr[0])//The value of the input is less than the first item, and the value is set to the first item.{Index=0; Break; }   }    for(i=6; i>index;i--) {Arr[i]=arr[i-1]; } Arr[index]=num;  for(i=0;i<7; i++) {printf ("%d", Arr[i]); }return 0;}

Common sorting methods in C language

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.