常见排序算法

未完待续


1.冒泡排序

void Bubble_sort(int a[],int n)  //冒泡排序
{
    int temp,flag;
    for(int i=0;i<n;i++)
    {
        flag=0;
        for(int j=0;j<n-i-1;j++)
        {
            if(a[j]>a[j+1])
            {
                flag=1;
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
        }
        if(flag==0) //内层for循环全程无交换,所以数组已有序
            break;
    }
}

2.插入排序

void Insert_sort(int a[],int n)  //插入排序
{
    int temp;
    int i,j;
    for(i=1;i<n;i++)
    {
        temp=a[i];
        for(j=i-1; j>=0 && a[j]>temp; j--)
        {
            a[j+1]=a[j];  //移出空位
        }
        a[j+1]=temp;  //新牌落位
    }
}

3.折半插入排序

void BinaryInsert_sort(int a[],int n)  //折半插入排序
{
    int temp,i,j;
    int low,high,mid;
    for(i=1; i<n; i++)
    {
        temp=a[i];
        low=0;high=i-1;

        //折半查找,找到插入位置
        while(low<=high)
        {
            mid=(low+high)/2;
            if(temp<a[mid])  //插入点在前一子表
                high=mid-1;
            else             //插入点在后一子表
                low=mid+1;
        }
        for(j=i-1;j>=high+1;j--)
            a[j+1]=a[j];  //移出空位

        a[j+1]=temp;  //新牌落位
    }
}

4.希尔排序

void Shell_insert(int a[],int n,int dk)  //dk为增量插入排序 (希尔排序的一部分)
{
    int temp;
    int i,j;
    for(i=dk; i<n; i++)
    {
        temp=a[i];
        for(j=i-dk; j>=0 && a[j]>temp; j-=dk)
            a[j+dk]=a[j];
        a[j+dk]=temp;
    }
}

void Shell_sort(int a[],int n,int dk[],int k)  //希尔排序
{
        for(int i=0;i<k;i++)
            Shell_insert(a,n,dk[i]);
}


5.快速排序

int partition(int arr[],int low,int high)  //使大于pivot的在其后,小于pivot的在其前 (快速排序的一部分)
{
    int pivot=arr[low];
    while(low<high)
    {
        while(low<high && arr[high]>=pivot)
            high--;
        arr[low]=arr[high];

        while(low<high && arr[low]<=pivot)
            low++;
        arr[high]=arr[low];
    }

    arr[low]=pivot;
    return low;
}

void Quick_sort(int a[],int low,int high)  //快速排序
{
    if(low<high)
    {
        int pi=partition(a,low,high);
        Quick_sort(a,low,pi-1);
        Quick_sort(a,pi+1,high);
    }

}



cpp文件:

#include <iostream>

using namespace std;

void Bubble_sort(int a[],int n)  //冒泡排序
{
    int temp,flag;
    for(int i=0;i<n;i++)
    {
        flag=0;
        for(int j=0;j<n-i-1;j++)
        {
            if(a[j]>a[j+1])
            {
                flag=1;
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
        }
        if(flag==0) //内层for循环全程无交换,所以数组已有序
            break;
    }
}

void Insert_sort(int a[],int n)  //插入排序
{
    int temp;
    int i,j;
    for(i=1;i<n;i++)
    {
        temp=a[i];
        for(j=i-1; j>=0 && a[j]>temp; j--)
        {
            a[j+1]=a[j];  //移出空位
        }
        a[j+1]=temp;  //新牌落位
    }
}

void BinaryInsert_sort(int a[],int n)  //折半插入排序
{
    int temp,i,j;
    int low,high,mid;
    for(i=1; i<n; i++)
    {
        temp=a[i];
        low=0;high=i-1;

        //折半查找,找到插入位置
        while(low<=high)
        {
            mid=(low+high)/2;
            if(temp<a[mid])  //插入点在前一子表
                high=mid-1;
            else             //插入点在后一子表
                low=mid+1;
        }
        for(j=i-1;j>=high+1;j--)
            a[j+1]=a[j];  //移出空位

        a[j+1]=temp;  //新牌落位
    }
}

void Shell_insert(int a[],int n,int dk)  //dk为增量插入排序 (希尔排序的一部分)
{
    int temp;
    int i,j;
    for(i=dk; i<n; i++)
    {
        temp=a[i];
        for(j=i-dk; j>=0 && a[j]>temp; j-=dk)
            a[j+dk]=a[j];
        a[j+dk]=temp;
    }
}

void Shell_sort(int a[],int n,int dk[],int k)  //希尔排序
{
        for(int i=0;i<k;i++)
            Shell_insert(a,n,dk[i]);
}

int partition(int arr[],int low,int high)  //使大于pivot的在其后,小于pivot的在其前 (快速排序的一部分)
{
    int pivot=arr[low];
    while(low<high)
    {
        while(low<high && arr[high]>=pivot)
            high--;
        arr[low]=arr[high];

        while(low<high && arr[low]<=pivot)
            low++;
        arr[high]=arr[low];
    }

    arr[low]=pivot;
    return low;
}

void Quick_sort(int a[],int low,int high)  //快速排序
{
    if(low<high)
    {
        int pi=partition(a,low,high);
        Quick_sort(a,low,pi-1);
        Quick_sort(a,pi+1,high);
    }

}

int main()
{
    int a[10]={10,80,60,100,30,20,90,40,50,70}; //待排序数组
    int dk[3]={5,3,1}; //希尔排序的增量数组

    //Bubble_sort(a,10);  //冒泡排序
    Insert_sort(a,10);  //插入排序
    //Shell_sort(a,10,dk,3);  //希尔排序
    //Quick_sort(a,0,9);
    //BinaryInsert_sort(a,10);
    cout<<"10,80,60,100,30,20,90,40,50,70\n"<<"排序后:\n";
    for(int i=0;i<10;i++)
        cout<<a[i]<<" ";
    return 0;
}


posted @ 2017-12-31 11:16  詹晔晔(๑>؂<๑)  阅读(103)  评论(0编辑  收藏  举报