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

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

时间:2020-09-16 12:15:10      阅读:39      评论:0      收藏:0      [点我收藏+]

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

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

#include <iostream>
using namespace std;

//int n;
void show(int a[], int n)
{
    for (int i = 0; i < n; i++)
    {
        cout << " " << a[i];
    }
    cout << endl;
    cout << endl;
}
void siSort(int a[], int n)//直接插入排序
{
    int temp,j;
    for (int i = 1; i < n; i++)
    {
        if (a[i] < a[i - 1])        
        {
            temp = a[i];
            for ( j = i - 1; temp < a[j]; j--)
                //向后比较,从前往后空位准备插入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 < n; i++)
    {
        temp = a[i];
        low = 0, high = i - 1;
        while (low <= high) //二分查找确定插入位置
        {
            mid = (low + high) / 2;
            if (temp > a[mid]) low = mid + 1;
            else high = mid - 1;
            //cout << "a[i]="<<a[i]<<" i="<<i<<" low=" << low << " " << " high="<<high << endl;
        }
        for ( j = i - 1; j>=high+1; j--)//插入temp
            a[j + 1] = a[j];
        a[high + 1] = temp;
        //cout << endl;
    }
}
void bubbleSort(int a[], int n)//冒泡排序
{
    int temp;
    for (int j = 0; j < n; j++)
    {
        for (int i = n - 1; i >= j; --i)
        {
            //cout << "i=" << i << " j=" << j << endl;
            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 < j && a[j] >= temp) j--;
            a[i] = a[j];
            while (i < j && a[i] <= temp) 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 < n-1; i++)
    {
        for (int j = i + 1; j < n; 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 <= high; k++)
        b[k] = a[k];
    for ( i = low, j = mid + 1, k = i; i <= mid && j <= high; k++)
    {
        if (b[i] <= b[j]) a[k] = b[i++];
        else a[k] = b[j++];
    }
    while (i <= mid) a[k++] = b[i++];
    while (j <= high) a[k++] = b[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

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