Sorting Algorithm - Summary

There is something wrong, please correct me

Bubble Sort

main idea

If the number n arranged in ascending order, from back to front Ascending adjacent pairwise comparison size (I think it is better understood), if the former than the latter on a large immediately put Talia transposition, direct exchange position, the first pass down the largest in the last one, after the last one without comparison, the second time, the second largest in second place. . . . . . Comparison of the first pass of the n-1 times, exchanged uncertain times, compares the second time n-2 times. . . . .

void bubble_sort(int a[],int n)
{
    int tem=0;
    for(int i=n-1;i>0;--i)
    {
        for(int j=0;j<i;++j)
        {
            if(a[j]>a[j+1])
            {
                tem=a[j];
                a[j]=a[j+1];
                a[j+1]=tem;
            }

        }
    }
}

Selection Sort

main idea

  1. Specific methods are:

    Traversing a recording of the location of the most valued elements, the end of the traversal, the value of this element is adjusted to the most suitable position of
    the first pass, compare times n-1, only one exchange, it will be the proper placement of the value of the most


void sort(int a[],int n){
      for(int i=0;i<n;i++)//外层循环
        {
        int min=a[i];//每次循环把a[i]假设当前最小的数
        int index=i;//相当一个索引,始终指向最小的
        for(int j=i+1;j<n;j++)//内层循环,从剩下的数中找有没有更小的
        {
            if(a[j]<min)
            {
                min=a[j];
                index=j;//跟新索引
            }
        }
          int temp=a[index];//交换
          a[index]=a[i];
          a[i]=temp;
      }
  }

改进
void select_sort(int a[],int n)
{
    int k,temp;
    for(int i=0;i<n-1;i++)
    {
        k=i;//假设最小为a[i],用k标记
        for(int j=i+1;j<n;j++)//内层循环,从剩下的数中找有没有更小的
        {
            if(a[j]<a[k])
            {
              k=j;//找到新的最小的,跟新k的值
            }
        }
        if(i!=k)//比较i和k的值,判断是否交换
        {
            temp=a[i];
            a[i]=a[k];
            a[k]=temp;
        }
    }

}


The difference in the
case where the same data, the number of cycles of the two algorithms is the same, the same number of comparisons, but selection sort only 0-1 times the exchange, and bubble sort exchange only 0 to n times.
The main part of the algorithm affect our performance is the circulation and exchange, obviously, the more the number, the worse the performance. It is complexity of O (n * n). But the efficiency of different
bubble sort:
when the data in the reverse case, the same number of cycles as the exchange (the exchange will be determined in each cycle),
a complexity of O (n * n). When the data is positive, there will be no exchange. Complexity is O (0). Disorder in an intermediate state. It is for this reason, we are usually algorithm to compare the number of cycles.
Selection sort:
Because each outer loop generates only one exchange (only a minimum). Therefore, f (n) <= n
we have f (n) = O (n ). So, more chaos in the data, it can reduce the number of times a certain exchange.

Quick Sort

main idea:

Insertion Sort

The core idea:
insertion sort that every step will be a row of data to be inserted into the appropriate size according to their position already sorted data until all cued.
1, starting from the first element that a [0] may be considered to have been sorted.
2, fetch the next element in the sequence of elements has been ordered in the scanned from the back
3, if the element a [0] (sorted), a new element is greater than a [1], then the elements a [0] shift to the next position
4, 3 repeated until you find the location is less than or equal ordered elements of the new element
5, the new element is inserted into the position
6 was repeated 2

Insertion sort method to sort and divide directly into binary insertion sort are two, here only direct insertion sort, binary insertion sort See another article http://blog.csdn.net/qq_29232943/article/details/52939374
1 presentation charts the four elements of the process directly into the sort, a total of (a), (b), (c) cubic interpolation.Write pictures described here

#include <iostream>

using namespace std;
void insert_sort(int a[],int n);
int main()
{
    int a[]={3,41,369,1,2,4,5,9};
    int n=8;
    insert_sort(a,n);
    for(int i=0;i<n;i++)
    {
        cout<<a[i]<<" ";
    }
    return 0;
}
void insert_sort(int a[],int n)
{
    for (int i=1;i<n;i++)
    {
        for(int j=i;(j>0)&&(a[j]<a[j-1]);j--)
        {
            int tem=a[j];
            a[j]=a[j-1];
            a[j-1]=tem;
        }
    }
}

The best case, the already ascending, only n-1 comparisons, the worst case, is descending, to n (n-1) / comparisons
average complexity of O (n2)

shell sort

Published 175 original articles · won praise 76 · Views 230,000 +

Guess you like

Origin blog.csdn.net/qq_29232943/article/details/52782920