排序:C/C++实现插入,选择,交换,归并6大排序算法

2021-03-29 15:26

阅读:607

标签:数据   merge   比较   iostream   插入   out   amp   span   nbsp   

最近复习完了第二轮的数据结构,感觉排序这一块的代码很多,平时编程就用一个sort()函数草草了事,现在参考数据结构课本把几个常考的排序算法编写出来,方便查阅

#include using namespace std;

//int n;
void show(int a[], int n)
{
    for (int i = 0; i )
    {
        cout " "  a[i];
    }
    cout  endl;
    cout  endl;
}
void siSort(int a[], int n)//直接插入排序
{
    int temp,j;
    for (int i = 1; i )
    {
        if (a[i] 1])        
        {
            temp = a[i];
            for ( j = i - 1; temp )
                //向后比较,从前往后空位准备插入temp到合适位置
                a[j+1] = a[j];
            a[j+1] = temp;
        }
    }
}
void biSort(int a[], int n)//折半插入排序
{
    int temp,j;
    int low, high,mid;
    for (int i = 1; i )
    {
        temp = a[i];
        low = 0, high = i - 1;
        while (low //二分查找确定插入位置
        {
            mid = (low + high) / 2;
            if (temp > a[mid]) low = mid + 1;
            else high = mid - 1;
            //cout 
        }
        for ( j = i - 1; j>=high+1; j--)//插入temp
            a[j + 1] = a[j];
        a[high + 1] = temp;
        //cout 
    }
}
void bubbleSort(int a[], int n)//冒泡排序
{
    int temp;
    for (int j = 0; j )
    {
        for (int i = n - 1; i >= j; --i)
        {
            //cout 
            if (a[i-1] > a[i])
            {
                temp = a[i-1];
                a[i-1] = a[i];
                a[i] = temp;
            }
        }
    }
}
void quickSort(int a[], int n,int low,int high)//快速排序
{
    int temp,i,j;
    if (low  high)
    {
        temp = a[low];
        i = low, j = high;
        while (i  j)
        {
            while (i = temp) j--;
            a[i] = a[j];
            while (i ;
            a[j] = a[i];
        }
        a[i] = temp;
        quickSort(a, n,low, i - 1);
        quickSort(a, n,i + 1, high);
    }
}
void selectSort(int a[], int n)//简单选择排序
{
    int min = a[0],pos,temp;
    for (int i = 0; i 1; i++)
    {
        for (int j = i + 1; j )
        {
            if (min > a[j])
            {
                min = a[j];
                pos = j;
            }
        }
        if (a[i] > min)
        {

            temp = a[i];
            a[i] = min;
            a[pos] = temp;
        }
        min = a[i + 1];
    }
    //show(a, n);
}
int* b = (int*)malloc((10+1) * sizeof(int));
void merge(int a[], int n,int low, int mid, int high)
{
    int i, j, k;
    for (int k = low; k )
        b[k] = a[k];
    for ( i = low, j = mid + 1, k = i; i )
    {
        if (b[i] ];
        else a[k] = b[j++];
    }
    while (i ];
    while (j ];
}
void mergeSort(int a[], int n, int low,int high)//归并排序
{
    if (low  high) {
        int mid = (low + high) / 2;
        mergeSort(a,n, low, mid);
        mergeSort(a, n, mid + 1, high);
        merge(a,n, low, mid, high);
    }
}

int main(void)
{
    int a[] = { 5,7,4,0,2,6,9,8,3,1 };
    int n = sizeof(a) / sizeof(int);

    //siSort(a, n);//直接插入排序
    //biSort(a,n);//折半插入排序
    //bubbleSort(a, n);//冒泡排序
    //quickSort(a,n, 0, n-1);//快速排序
    //selectSort(a, n);//简单选择排序
    //mergeSort(a,n,0,n-1);//归并排序
    show(a, n);
}

 

排序:C/C++实现插入,选择,交换,归并6大排序算法

标签:数据   merge   比较   iostream   插入   out   amp   span   nbsp   

原文地址:https://www.cnblogs.com/murenma/p/13603149.html


评论


亲,登录后才可以留言!