排序算法--冒泡、插入、归并、快排

整理了一下几种常见的排序算法,包括冒泡、插入、归并、快排。还有另外几种整理:堆排序、希尔排序、桶排序

直接上代码:

#include<iostream>
#include<stdlib.h>
//#include <exception>
using namespace std;

void swap(int* a, int* b)
{
    if(a==NULL || b==NULL)
        cout << "bad in swap" << endl;
    int tmp;
    tmp = *a;
    *a = *b;
    *b = tmp;
     
}

//最简单的冒泡排序,时间复杂度O(n*n)
void BubbleSort(int a[], int length)
{
    int i,j;
    for(i=0; i<length;i++)
    {
        for(j=i; j<length; j++)
        {
            if(a[i]>a[j])
                swap(&a[i],&a[j]);
        }
    }
}

//插入排序,最坏时间复杂度O(n*n)
void InsertSort(int a[], int length)
{
    int i,j;
    int tmp;
    for(j=1; j<length; j++)
    {
        tmp = a[j];
        for(i=j; i>0&&a[i-1]>tmp; i--)
            a[i] = a[i-1];
        a[i] = tmp;
    }
}

//归并排序,时间复杂度O(NlogN)
void mergearray(int a[], int first, int mid, int last, int temp[])
{
    int i = first, j = mid+1;
    int m = mid,   n = last;
    int k = 0;
    
    while(i<=m && j<=n)
    {
        if(a[i] <=a[j])
            temp[k++] = a[i++];
        else
            temp[k++] = a[j++];
    }
    
    while(i<=m)
        temp[k++] = a[i++];
    while(j<=n)
        temp[k++] = a[j++];
    
    for(i=0; i <k; i++)
        a[first+i] = temp[i]; //将排序好的数组放回数组a中
}

void mergesort(int a[], int first, int last, int temp[])
{
    if( first < last )
    {
        int mid = (first + last) >> 1;
        mergesort(a, first, mid, temp);
        mergesort(a, mid+1, last, temp);
        mergearray(a, first, mid, last, temp);
    }
}

void MergeSort(int a[], int length)
{
    int *p = new int[length];
    if (p == NULL)
        return;
    mergesort(a, 0, length-1, p);
    delete [] p;
}

//快速排序,平均复杂度为O(NlogN)
int RandomInRange(int min, int max) //产生随机数
{
    int random = rand()%(max-min+1)+min;
    return random;
}

int Partition(int a[], int length, int start, int end)
{
    if(a==NULL || length <=0 || start <0 || end >=length)
        return -1;
    int index = RandomInRange(start,end);
    swap(&a[index],&a[end]);
    
    int small = start - 1;
    for(index = start; index < end; ++index)
    {
        if(a[index] < a[end])
        {
            ++small;
            if(small != index)
                swap(&a[index], &a[small]);
        }
    }
    ++small;
    swap(&a[small],&a[end]);
    return small;
}

void QuickSort(int a[], int length, int start, int end)
{
    if(start==end)
        return;
    int index = Partition(a, length, start, end);
    if(index > start)
        QuickSort(a, length, start, index-1);
    if(index < end)
        QuickSort(a, length, index+1, end);
}

int main()
{
    int a[10] = {10,9,8,7,6,5,4,3,2,1};
    int *temp1 = a;
    int *temp2 = a;
    int *temp3 = a;
    int temp4[] = {9,4,5,2,3,6,8,9};
    int n = 10;
    //测试冒泡排序
    BubbleSort(temp1,10);
    cout << "BubbleSort result:";
    for(int i = 0; i<10; i++)
        cout << temp1[i] << " " ;
    cout << endl;
    //测试插入排序
    InsertSort(temp2,10);
    cout << "InsertSort result:";
    for(int i = 0; i<10; i++)
        cout << temp2[i] << " " ;
    cout << endl;
    //测试归并排序
    MergeSort(temp3,10);
    cout << "MergeSort result:";
    for(int i = 0; i<10; i++)
        cout << temp3[i] << " " ;
    cout << endl;
    //测试快速排序
    QuickSort(temp4,8,0,7);
    cout << "QuickSort result:";
    for(int i = 0; i<8; i++)
        cout << temp4[i] << " " ;
    cout << endl;
}    


 
运行结果:



猜你喜欢

转载自blog.csdn.net/mmshixing/article/details/51741256