Summary of sorting algorithms

Source: Internet
Author: User

Summary: 1, bubble sort bubble sort is a simple sorting method, the algorithm is as follows: 1. First put all the numbers you want to sort into your work list. 2. From the first number of the list to the second-to-last number, check each: If the number on one is greater than the next, then swap it with the next one.

1. Bubble sort

Bubble sort is a simple sort method, with the following algorithm:
1. First put all the numbers you want to sort into your work list.
2. From the first number of the list to the second-to-last number, check each: If the number on one is greater than the next, then swap it with the next one.
3. Repeat step 2nd (the countdown number plus 1. For example: the first to the penultimate number, the second to the last third number, and so on ...), until it can no longer be exchanged.
The following are implemented in the C language:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

intBubbleSort(int *a, intn)    //a是待排序的整型数组,n是待排序数组的元素个数

{

   inti,j,temp;

    for(j=0;j<n-1;j++)

        for(i=0;i<n-1-j;i++)

        {

            if(a[i]>a[i+1])        //数组元素大小按升序排列

            {

                temp=a[i];

                a[i]=a[i+1];

                a[i+1]=temp;

            }

        }


2. Insert Sort

The insertion sort is also a simple sort method, with the following algorithm:
1. Starting with the first element, it is assumed that the element is already sequenced.
2. Remove an element and scan from the back forward in the sequence of elements that are already sequenced.
3. If the element in the ordered sequence is greater than the new element, move the element to the right one position.
4. Repeat step 3 until the ordered element is less than or equal to the new element.
5. Insert the new element at the current location.
6. Repeat step 2.
The following are implemented in C:

???????

Original link

Summary of 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.