Internal sort: Bubble sort and select sort

Source: Internet
Author: User
Tags comparison continue min sort

Objective

The reason to put the bubble sort and select sort together is that the implementation code of the two is very similar, and is the most basic sort, very easy to understand and implement. Of course, if it's just to tell the two ways of sorting, there's no need to write this blog post. As with the previous blog post, I will give some improvement and optimization on the basis of bubble sort and selection of original code, which is the focus of this article.

Raw Bubble sort

The idea of bubble sort is simple, and if you want the elements in the sequence to be sorted in order from small to large, the steps for bubbling are as follows:

1, in order to compare the sequence of adjacent two elements, will be larger in the back, such a trip after comparison, the largest element is placed in the last position;

2, and then compare the adjacent two elements, the second largest element will eventually be placed in the penultimate position;

3, in turn, loop until the smallest element is placed in the first position, sorting completed.

Based on the above ideas, it is easy to write code:

 
/* Bubble sort order is from small to large
/void bubble_sort (int *arr,int len)  
{  
    int i,j,exchange;  
    for (i=0;i<len-1;i++) for (  
        j=0;j<len-i-1;j++)  
            if (Arr[j] > arr[j+1])  
            {  
                exchange = Arr[j];  
                ARR[J] = arr[j+1];  
                ARR[J+1] = Exchange;  
            }  
}

Improved bubble sort

Let's go back and look at the idea of bubble sort, regardless of the order of the original sequence (even if it is already from a small to large row), it has to carry out n-1 trip comparison, each comparison to be n-i-1 next to the comparison between the elements (I for the first time from 0 comparison), and in fact, It is likely that no n-1 comparison has been made, has completed the sorting, this time after the several times is redundant, based on this, we can do the following improvements: Set a flag bit, if a trip has elements to exchange, true, continue the next comparison, otherwise, the description of the order has been completed, The flag bit is false, exiting the loop. The code implementation is as follows:

 
/* Bubble sort order is from small to large
/void bubble_sort (int *arr,int len)  
{  
    int i,j,exchange;  
    BOOL flag = TRUE;   Add a flag bit to determine whether the sort is completed for  
    (i=0; i<len-1 && flag; i++)  
    {  
        flag = false;  
        for (j=0;j<len-i-1;j++) if  
            (Arr[j] > arr[j+1])  
            {   //If there is no data exchange for this trip, the description of the order has been completed  
                // The flag is always false, which exits the loop, and no longer makes the next trip to compare  
                exchange = Arr[j];  
                ARR[J] = arr[j+1];  
                ARR[J+1] = Exchange;  
                Flag = true;  
            }  
    }  

Direct Selection Sort

The idea of direct selection sorting is also very simple to sort from small to large as an example, as follows:

1, starting from the first element, select a smallest element and the first element interchange;

2, continue to start from the second element, select the smallest element backwards, and the second element interchange;

3, in turn, loop execution until the largest element is placed in the last position, sorting completed.

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

We can compare the first element to the following elements, respectively, encounter smaller, on the exchange, such a trip compared down, the first element to save is the smallest value, and then from the second element to open it seems, in turn, with the elements of the back, and then encountered a smaller, on the exchange, so that the second trip compared down, The second element holds the second small value ... Loop through the execution until the sort is finished. According to this idea, the implementation code is as follows:

/* The 
first form of the selection sorted 
by the order of choice for small to large
/void select_sort1 (int *arr,int len)  
{  
    int i,j;  
    for (i=0;i<len;i++) for (  
        j=i+1;j<len;j++)  
            if (Arr[i] > Arr[j])  
            {  
                int exchange = Arr[i];  
                Arr[i] = arr[j];  
                ARR[J] = Exchange;  
            }  
}

Improved selection sorting

But as we mentioned in the previous article, we should try to avoid more and element swaps in the sort, and here each time, if you encounter a smaller one, you need to swap the elements. In order to reduce the interchange of elements, we can not exchange directly after each comparison, record the position number of the smaller element, so that after a comparison, you get the position of the smallest element, and if the position of the lowest value changes, then swap the element of that position with the first element, and so on ... This allows you to perform at most one element interchange operation after each comparison completes. The implementation code is as follows:

/* The 
second form of the selection sort, reduces the element interchange operation by 
Selecting the order of the sort after the small to large
/void select_sort2 (int *arr,int len)  
{  
    int i,j, min;  
    for (i=0;i<len;i++)  
    {  
        min = i;    The position used to record the minimum value of each trip for  
        (j=i+1;j<len;j++)  
            if (Arr[min] > Arr[j])  
                min = j;     Record only the minimum position  
        //If the position of the lowest value has changed,  
        //The last operation to perform the element interchange if  
        (min!= i)  
        {  
            int exchange = Arr[i];  
            Arr[i] = arr[min];  
            Arr[min] = Exchange;  
        }  
    }  

Summarize

Bubble sort and select sorting are the most basic sorting algorithm, the average time complexity is O (n*n), the number of sorting elements is small, suitable for use, when encountering large data, it is best to choose other sorting algorithm.

Complete source

Complete C Language Implementation code download address: http://download.csdn.net/detail/mmc_maodun/6970951

Author: csdn Blog Lan pavilion Wind and Rain

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.