排序算法实现分析

来源:互联网 发布:软件研发公司 编辑:程序博客网 时间:2024/06/07 16:12

排序算法分析


选择排序、快速排序、希尔排序、堆排序不是稳定的排序算法。
冒泡排序、插入排序、归并排序和基数排序是稳定的排序算法。

冒泡法:这是最原始,也是众所周知的最慢的算法了。他的名字的由来因为它的工作看来象是冒泡:复杂度为O(n*n)。当数据为正序,将不会有交换。复杂度为O(0)。

直接插入排序:O(n2)

选择排序:O(n2)

快速排序:平均时间复杂度nlog2(n),所有内部排序方法中最高好的,大多数情况下总是最好的。

归并排序:nlog2(n)

堆排序:nlog2(n)

希尔排序:算法的复杂度为n的1.2次幂

#include<iostream>#include<cassert>#include <algorithm> using namespace std;void insertion_sort(int a[], int n)//insertion sort{    assert(a != NULL );    int temp;    int j = 0;    for (int p = 1; p < n; ++p)    {        temp = a[ p];        for ( j = p; j > 0 && a[j - 1 ]>temp; --j)        {            a [j] = a[j - 1 ];        }        a [j] = temp;    }}// bubble sort 的改进版,一旦没发生交换则排序结束, flag去掉则为原始版本。void bubble_sort(int a[], int n){    assert(a != NULL );    int i, j ,flag= 1;    for (i = 0 ; i < n - 1 && flag; ++i)    {        flag = 0 ;        for ( j = 0; j < n - 1 - i;++j )            if ( a[j]>a [j + 1 ])            {                swap (a[ j], a[j + 1 ]);                flag = 1 ;            }    }}void selection_sort(int a[], int n)//selection sort{    assert(a != NULL );    int i, j , min;    for (i = 0 ; i < n; ++i )    {        min = i;        for ( j = i + 1; j < n;++ j)            if ( a[j] < a[ min])                min = j;            if(min != i)               swap (a[ min], a [i]);    }}int partition(int a[], int start, int end) //partition 算法{    assert(a != NULL );    int middle = ( start + end ) / 2 ;    int pivotIndex ;    if (a[ start] < a[ middle])        if ( a[middle] < a[ end])            pivotIndex = middle;        else if (a[start ] < a[end])            pivotIndex = end;        else            pivotIndex = start;    else if ( a[start] <a[ end])        pivotIndex = start;    else if ( a[middle]<a [end])        pivotIndex = end;    else        pivotIndex = middle;    int pivot = a[ pivotIndex];    swap(a [pivotIndex], a[end ]);    int storeIndex = start;    for (int i = start ; i < end; ++i)    {        if ( a[i]<pivot )        {            swap (a[ i], a[storeIndex ]);            ++storeIndex;        }    }    swap(a [storeIndex], a[end ]);    cout << pivot << endl;    return storeIndex ;}void quick_sort(int a[], int left, int right) //quick sort{    if (left == right)        return;    int index = partition(a, left , right);    if (index > left)        quick_sort (a, left, index - 1 );    if (index < right)        quick_sort (a, index+1 , right);}//归并排序的merge相当于两个已排序链表或者数组的合并void merge( int arr[], int temp_arr[], int start_index, int mid_index , int end_index){    int i = start_index , j = mid_index + 1;    int k = 0;    while (i < mid_index + 1 && j < end_index + 1)        temp_arr [k++] = arr[i ] > arr[j ] ? arr[j ++] : arr[i ++];    while (i < mid_index+1 )        temp_arr [k++] = arr[i ++];    while (j < end_index+1 )        temp_arr [k++] = arr[j ++];    for (i = 0 , j = start_index; j < end_index+1; i ++, j++)        arr [j] = temp_arr[i ];}void merge_sort(int a[], int temp_a[], int start_index, int end_index ){    if (start_index == end_index)        return;    int mid_index = ( start_index + end_index ) / 2 ;    merge_sort(a , temp_a, start_index, mid_index );    merge_sort(a , temp_a, mid_index + 1, end_index );    merge(a , temp_a, start_index, mid_index , end_index);}void heap_adjust(int a[], int i, int len)//heap sort{    int child ;    int temp;    for (; 2 * i + 1 < len; i = child )    {        child = 2 * i + 1;        if(child <len - 1 && a[child + 1 ] > a[child])            ++child;        if ( a[i] < a[ child])        {            temp = a[ i];            a [i] = a[child ];            a [child] = temp;        }        else            break;    }}void heap_sort(int a[], int len){    assert(a != NULL );    int i;    for ( i = len / 2; i >= 0 ; -- i)        heap_adjust (a, i,len);    for (i = len - 1; i > 0 ; -- i)    {        int tmp = a[0];        a [0] = a[i ];        a [i] = tmp;        heap_adjust (a, 0 , i);    }}//shell sort,中间两个循环的思想和插入排序一样,每次相当于对 incur个子数组进行排序void shell_sort(int a[], int len){    int i, j , incr;    int temp;    for (incr = len / 2; incr > 0 ; incr /= 2 )    {        for ( i = incr; i < len; i++)        {            temp = a[ i];            for ( j = i; j >= incr && a[j - incr ]>temp; j -= incr )                a [j] = a[j - incr];            a [j] = temp;        }    }}int main() {    int arr[] = { 61 , 17 , 29 , 22 , 34 , 60 , 72 , 21 , 50 , 1 , 62 };    int len = (int) sizeof(arr) / sizeof(*arr);    cout << len << endl;    int *temp_a = new int [len];    merge_sort(arr ,temp_a, 0,len -1);    delete[]temp_a;    for (int i = 0; i < len; i++)        cout << arr[ i] << ' ';    cout << endl ;    return 0;}
1 0
原创粉丝点击