Inserting Sort, Shell Sort, Heap Sort and Quick Sort

来源:互联网 发布:孤岛危机2优化补丁 编辑:程序博客网 时间:2024/06/06 14:18

Inserting Sort, Shell Sort, Heap Sort and Quick Sort

This passage contains the application of four Sorting Algorithms in the title Inserting Sort, Shell Sort, Heap Sort and Quick Sort . Sorting algorithms are fundamental in Computer Science and these four algorithms are basic for new hand.

Code

Below is the C code

#include<stdio.h>#include<stdlib.h>void InsertingSort(int A[], int N);void ShellSort(int A[], int N);void Swap(int A[] ,int a, int b);void PrecDown(int A[], int i, int N);void HeapSort(int A[], int N);int Median3(int A[], int left, int right);void Qsort(int A[], int left, int right);void QuickSort(int A[], int N);int main(){    int size;    scanf("%d", &size);    int* A = (int*)malloc(sizeof(int)*size);    for(int i=0; i<size;i++){        scanf("%d", &A[i]);    }    InsertingSort(A, size);    ShellSort(A, size);    HeapSort(A, size);    QuickSort(A, size);    for(int i=0; i<size; i++){        printf("%d ", A[i]);    }}void InsertingSort(int A[], int N){    int Tmp;    int P, j;    for(P=1; P<N; P++){        Tmp = A[P];        for(j=P; j>0&&A[j-1]>Tmp; j--){        //important 1.cmpare with Tmp; start just from P        //remember j is the place to fit in            A[j] = A[j-1];          }        A[j] = Tmp;    }}void ShellSort(int A[], int N){    int Tmp;    int P, j;    int Increment = N/2;    for(; Increment>0; Increment/=2){    //stop after Increment become 1        for(P=Increment; P<N; P++){            Tmp = A[P];            for(j=P; j>=Increment&&A[j-Increment]>Tmp; j-=Increment){                A[j] = A[j-Increment];              }            A[j] = Tmp;        }    }}void Swap(int A[] ,int a, int b){    int Tmp = A[a];    A[a] = A[b];    A[b] = Tmp;    return;}void PrecDown(int A[], int i, int N){    //be careful that generally when we apply a heap, we start from 1 instead of 0    int Tmp = A[i];    int child;    child = 2*i+1;     for(; child<N; child = 2*i+1){    //notice that left child is not naively i*2        if(child!=N-1&&A[child+1]>A[child])            child++;        if(A[child]>Tmp){            A[i] = A[child];            i = child;        }        else            break;    }    A[i] = Tmp;}void HeapSort(int A[], int N){    //pay attention that it's a Max heap    for(int i = N/2-1; i>=0; i--){        PrecDown(A, i, N);    }    int size = N;    //intitializing the heap    for(int i=0; i<size; i++){        Swap(A, 0, N-1);        PrecDown(A, 0, N-1);        N--;        }    return;}int Median3(int A[], int left, int right){    int mid = (left+right)/2;    if(A[left]>A[mid])        Swap(A, left, mid);    if(A[left]>A[right])        Swap(A, left, right);    if(A[mid]>A[right])        Swap(A, mid, right);    Swap(A, mid, right-1);    //the right item > mid, so hide mid just bufore right, instead of at right    return A[right-1];    //simple while complex  }void Qsort(int A[], int left, int right){    //Not as easy as first sight    if(right - left >10){        int mid = Median3(A, left, right);        int i=left;        int j = right-1;        for(;;){            while(A[++i]<mid);            //actually i start from left+1, because after running Median3 the item at left must<mid            while(A[--j]>mid);            //j start from right-2            if(i>j)                break;            Swap(A, i, j);        }        Swap(A, i, right-1);        Qsort(A, left, i-1);        Qsort(A, i+1, right);    }    else        InsertingSort(A+left,  right-left+1);}void QuickSort(int A[], int N){    Qsort(A, 0, N-1);} 
阅读全文
'); })();
0 0
原创粉丝点击
热门IT博客
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 福斯柴机油 长城柴机油价格表 叉车柴机油 柴机油的作用 柴机油种类 柴机油和汽机油的区别 汽机油和柴机油的区别 埃尔夫柴机油 柴机油15w-40是什么意思 潍柴机油 柴油机油级别 小型柴油机 柴油机价格 双缸柴油机 柴油机配件 柴松岩挂号 柴松岩网上挂号 柴松岩在哪里坐诊 柴松岩 柴松岳 拔根芦柴花二胡曲谱 拔根芦柴花二胡 拔根芦柴花二胡独奏 一根柴 九江柴桑 柴桑春天 九江柴桑区 柴棺龟 柴正荣 柴沟堡 柴沟堡长途汽车站 宣化到柴沟堡的汽车 大同到柴沟堡的火车时刻表 乌拉特前旗到柴沟堡的火车 0号柴油多少度才会冻住 柴油滤芯器 柴油油罐 柴油热水锅炉 世界柴油发动机排名 柴油车暖风机 拖拉机柴油发动机