[Algorithm] various sorting algorithms implemented in C ++

We usually refer to sorting algorithm often refers to the internal sorting algorithm that sorts the data recorded in the memory.

  Sorting algorithms can be roughly divided into two types:

    One is the comparison sort , the time complexity of O (nlogn) ~ O (n ^ 2), are: bubble sort , selection sort , insertion sort , merge sort , heap sort , quicksort like.

    Another is the non-sort comparison , the time complexity can be achieved O (n), are: counting sort , radix sort , bucket sort the like.

The following table shows the performance comparison of common sorting algorithm:

 

For convenience of the following description, the following algorithms are all sort object scrambled array int a [n];


 

  1. Bubble Sort (BubbleSort)

Idea: to compare two adjacent elements, so each round of relatively complete current maximum (small) element will be moved to the tail, so to repeat n wheel, you can achieve the sort.

achieve:

 1 void bubbleSort(int a[],int begin,int end)
 2 {
 3   for(int i = begin;i<end;i++)
 4   {
 5     for(int j = i+1;j<end;j++)
 6     {
 7       if(a[i]>a[j])
 8       {
 9         std::swap(a[i],a[j]);
10       }
11     }
12   }
13 }

FIG particular ordering process:

 

to sum up:

Simple sorting algorithm, stability, since the two-cycle so complexity is O (n- 2 ). (But think of when asked about that year for the first time looking for work, the results come out as expected no answer, really made me feel embarrassed algorithm.)


 

Reference article: https://www.cnblogs.com/eniac12/p/5329396.html#s1

Guess you like

Origin www.cnblogs.com/Swetchine/p/11295657.html