Summary of C-language sorting algorithm

Source: Internet
Author: User

Learning computer programming should know that the algorithm is the soul of the program. The so-called algorithm is the solution to the problem and the limited implementation steps. The features of the algorithm are poor, deterministic, and valid, with 0 or more inputs, one or more outputs. Let's take a brief summary of the three classic sorting algorithms in the C language.

First, the bubbling algorithm.

The so-called bubble sorting method, is a set of numbers from large to small or from the beginning to the large sort of an algorithm. The exact method is that the adjacent number 22 is exchanged. Starting with the first value, if the next two numbers are arranged in a different order than our expectations, the position of the two digits is swapped (swapped), and the position is not swapped if the rest of us expect the same. Repeat this process until there is no value at the end that requires an input exchange, then the sort is complete.

Instance:

#include <stdio.h>
int main ()
{
Defines an array to store the input numbers
int arr[10];
Define two variables to control a loop
int i,j;
Define an intermediate variable
int temp;
Prompt for input
for (i=0;i<10;i++)
{
printf ("Please enter number%d: \ n", i+1);
scanf ("%d", &arr[i]);
GetChar ();
}
Bubble sort
for (j=0;j<9;j++)
{
for (i=0;i<9;i++)
{
To exchange numeric values
if (arr[i]>arr[i+1])
{
TEMP=ARR[I+1];
Arr[i+1]=arr[i];
Arr[i]=temp;
}
}
}
Loop output Results
for (i=0;i<10;i++)
{
printf ("%d", arr[i]);
}
return 0;
}

Second, choose the sort.

The basic idea of sorting is to select the smallest number in the set of numbers to be sorted and the number of the first position to exchange, then find the smallest number in the remaining number and the number of the second position to exchange, so loop until the penultimate number is compared to the last number.

Instance:

#include <stdio.h>
int main ()
{
Defines an array that stores the number of inputs
int arr[10];
Define two variable control loops
int i,j;
Define an intermediate variable
int temp;
for (i=0;i<10;i++)
{
Prompt for input
printf ("Please enter number%d: \ n", i+1);
scanf ("%d", &arr[i]);
GetChar ();
}
Select sort
for (j=0;j<10;j++)
{
for (i=j;i<10;i++)
{
Swap location
if (Arr[j]>=arr[i])
{
Temp=arr[i];
ARR[I]=ARR[J];
Arr[j]=temp;
}
}
}
Cyclic output sorting results
for (i=0;i<10;i++)
{
printf ("%d", arr[i]);
}
return 0;
}

Third, insert sort.

It is important to ensure that the number of inserted groups is ordered before inserting the order (for example from small to large), and the basic idea is to compare the number of insertions to each number in the inserted sequence until the number of inserts is greater than the previous number in the sequence and less than or equal to the last number in the sequence, and note this position, This position is then moved back one at a time, the number of points to be inserted, and the last number to be inserted in the vacated position to complete the sorting.

Instance:

#include <stdio.h>
int main ()
{
To define an ordered array
int arr[10]={12,34,67,89,123,145,167,213,223};
Defines a variable that stores the number of inputs
int num;
Define two variable control loops
int i,j;
Defining a Variable Store index
int index=9;
Prompt for input
printf ("Please enter a number: \ n");
scanf ("%d", &num);
Insert Sort
for (i=0;i<9;i++)
{
if (Arr[i]<=num && num<arr[i+1])
{
index=i+1;
Break
}
else if (Num<=arr[i])
{
index=0;
Break
}
}
Move position
for (j=9;j>index;j--)
{
ARR[J]=ARR[J-1];
}
Put the number you want to insert in the vacated position.
Arr[index]=num;
Cyclic output sorting results
for (i=0;i<10;i++)
{
printf ("%d", arr[i]);
}
return 0;
}

The above is my C language commonly used in the summary of three classical algorithms, I hope you learn the C-language sorting algorithm of small partners to help.

C language Sorting algorithm summary

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.