码迷,mamicode.com
首页 > 编程语言 > 详细

排序算法

时间:2016-09-04 15:50:46      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:

//BubbleSort
void BubbleSort(int* pData, int Count){
    int iTemp;
    for(int i = 0; i < Count; i++){
        for(int j = Count-1; j >= i; j--){
            if(pData[j] < pData[j-1]){
                iTemp = pData[j];
                pData[j] = pData[j-1];
                pData[j-1] = iTemp;
            }
        }
    }
}

//SelectSort
void SelectSort(int* pData, int Count){
    int iTemp;
    int iPos;
    for(int i = 0; i < Count-1; i++){
        iTemp = pData[i];
        iPos = i;
        for(int j = i+1; j < Count; j++){
            if(pData[j] < iTemp){
                iTemp = pData[j];
                iPos = j;
            }
        }
        pData[iPos] = pData[i];
        pData[i] = iTemp;
    }
}

//InsertSort
void InsertSort(int* pData, int Count){
    int iTemp;
    int iPos;
    for(int i = 1; i < Count; i++){
        iTemp = pData[i];
        iPos = i-1;
        while(iPos >= 0 && iTemp < pData[iPos]){
            pData[iPos+1] = pData[iPos];
            iPos--;
        }
        pData[iPos+1] = iTemp;
    }
}

//MergeSort
void merge(int array[], int p, int q, int r){
    int i, k;
    int begin1, end1, begin2, end2;
    int *temp = (int*)malloc((r-p+1)*sizeof(int));
    begin1 = p; end1 = q;
    begin2 = q+1; end2 = r;
    k = 0;
    while(begin1 <= end1 && begin2 <= end2){
        if(array[begin1] < array[begin2]){
            temp[k] = array[begin1]; begin1++;
        }
        else{
            temp[k] = array[begin2]; begin2++;
        }
        k++;
    }
    while(begin1 <= end1)
        temp[k++] = array[begin1++];
    while(begin2 <= end2)
        temp[k++] = array[begin2++];
    for(i = 0; i < r-p+1; i++)
        array[p+i] = temp[i];
    free(temp);
}

void merge_sort(int array[], unsigned int first, unsigned int last){
    int mid = 0;
    if(first < last){
        mid = (first + last)/2;
        merge_sort(array, first, mid);
        merge_sort(array, mid+1, last);
        merge(array, first, mid, last);
    }
}

//qsort
inline void swap(int v[], int k, int j){
    int temp;
    temp = v[k];
    v[k] = v[j];
    v[j] = temp;
}

void qsort(int v[], int left, int right){
    int j, last;
    if(left >= right) return;
    swap(v, left, (left+right)/2);
    last = left;
    for(j = left+1; j <= right; j++){
        if(v[j] < v[left]) swap(v, ++last, j);
    }
    swap(v, left, last);
    qsort(v, left, last-1);
    qsort(v, last+1, right);
}

//ShellSort
int ShellPass(int *array, int d){
    int temp;
    int k=0;
    for(int i = d+1; i < 13; i++){
        if(array[i] < array[i-d]){
            temp = array[i];
            int j = i-d;
            do{
                array[j+d] = array[j];
                j = j-d;
                k++;
            }
            while(j > 0 && temp < array[j]);
            array[j+d] = temp;
        }
        k++;
    }
    return k;
}

void ShellSort(int *array){
    int count = 0;
    int ShellCount = 0;
    int d = 12;//一般增量设置为数组元素个数,不断除以2以取小
    do{
        d = d/2;
        ShellCount = ShellPass(array, d);
        count += ShellCount;
    }
    while(d>1);
    cout << "希尔排序关键字移动次数:" << count << endl;
}

//HeapSort
void HeapAdjust(int a[], int i, int n){
    if(n == 1 || i > (n-2)/2) return;
    int lc = 2*i+1, rc = 2*i+2;
    if(rc <= n-1){
        int pos;
        if(a[lc] >= a[rc])
            pos = lc;
        else
            pos = rc;
        if(a[i] < a[pos]){
            int temp = a[i];
            a[i] = a[pos];
            a[pos] = temp;
            HeapAdjust(a, pos, n);
        }
    }
    else{
        if(a[i] < a[lc]){
            int temp = a[lc];
            a[lc] = a[i];
            a[i] = temp;
            HeapAdjust(a, lc, n);
        }
    }
}

void HeapCreate(int a[], int n){
    int first = (n-1)/2;
    for(; first>=0; first--)
        HeapAdjust(a, first, n);
}

void HeapSort(int a[], int n){
    HeapCreate(a, n);
    int temp;
    for(int i = 0; i < n; i++){
        temp = a[n-1-i];
        a[n-1-i] = a[0];
        a[0] = temp;
        HeapAdjust(a, 0, n-1-i);
    }
}

时间复杂度:

冒泡、选择、插入排序:O(n2)

快速、归并、堆排序:O(nlog2n)

希尔排序:O(n1+ξ) 0<ξ<1

排序算法

标签:

原文地址:http://www.cnblogs.com/yingl/p/5839216.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!