七大排序算法代码实现-C语言[插入、冒泡、选择、希尔、快排、归并、堆]

目录

前言

1 插入排序

2 冒泡排序

3 选择排序

4 希尔排序

5 快速排序

6 归并排序

7 堆排序


前言

本文章是笔者前些时间作为考试复习使用,现分享给大家。

均为经典排序算法,其实现策略不一,且我能力有限,短时间内赶出来的代码多少存在一些小问题,还请见谅。


1 插入排序

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>

void InsertSort(int *arr, int len)
{
    int i, j;
    int temp;
    for (i = 0; i < len; i++) // 执行len次
    {
        temp = arr[i];                               // 摸下一张牌
        for (j = i; j > 0 && arr[j - 1] > temp; j--) // 往前找合适的位置
            arr[j] = arr[j - 1];                     // 移出空位
        arr[j] = temp;                               // 新牌落位
    }
}

int main()
{
    int i;
    int arr[] = {70, 25, 23, 55, 31, 20, 22, 13, 62, 81, 50, 74, 65, 84, 80, 66, 64, 42, 12, 40};
    InsertSort(arr, 20);
    for (i = 0; i < 20; i++)
        printf("%d ", arr[i]);
    system("pause");
    return 0;
}


2 冒泡排序

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>

void BubbleSort(int *arr, int len)
{
    int i, j;
    int temp;
    int flag;

    for (i = 0; i < len; i++)
    {
        flag = 0;
        for (j = 0; j < len - i - 1; j++)
        {
            if (arr[j] > arr[j + 1])
            {
                temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
                flag = 1;
            }
        }
        if (!flag)
            break;
    }
}

int main()
{
    int i;
    int arr[] = {70, 25, 23, 55, 31, 20, 22, 13, 62, 81, 50, 74, 65, 84, 80, 66, 64, 42, 12, 40};
    BubbleSort(arr, 20);
    for (i = 0; i < 20; i++)
        printf("%d ", arr[i]);
    system("pause");
    return 0;
}


3 选择排序

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>

void SelectSort(int *arr, int n)
{
    int i, j;
    int min;
    int temp;
    for (i = 0; i < n; i++) // 执行n次
    {
        // 从当前元素往后找出最小的
        min = i;
        for (j = i + 1; j < n; j++)
        {
            if (arr[j] < arr[min])
                min = j;
        }
        // 将最小的与当前交换
        temp = arr[i];
        arr[i] = arr[min];
        arr[min] = temp;
    }
}

int main()
{
    int i;
    int arr[] = {70, 25, 23, 55, 31, 20, 22, 13, 62, 81, 50, 74, 65, 84, 80, 66, 64, 42, 12, 40};
    SelectSort(arr, 20);
    for (i = 0; i < 20; i++)
        printf("%d ", arr[i]);
    system("pause");
    return 0;
}


4 希尔排序

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>

void ShellSort(int *arr, int len)
{
    int i, j;
    int d; // gap
    int temp;

    for (d = len / 2; d > 0; d /= 2) // 希尔增量序列
    {
        for (i = d; i < len; i++) // 插入排序
        {
            temp = arr[i];
            for (j = i; j >= d && arr[j - d] > temp; j -= d)
                arr[j] = arr[j - d];
            arr[j] = temp;
        }
    }
}

int main()
{
    int i;
    int arr[] = {70, 25, 23, 55, 31, 20, 22, 13, 62, 81, 50, 74, 65, 84, 80, 66, 64, 42, 12, 40};
    ShellSort(arr, 20);
    for (i = 0; i < 20; i++)
        printf("%d ", arr[i]);
    system("pause");
    return 0;
}


5 快速排序

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <time.h>

void QuickSort(int *arr, int low, int high);
int QuickSort_Partition(int *arr, int low, int high);
void QuickSort_RandomPivot(int *arr, int low, int high);

int main()
{
    int i;
    int arr[] = {70, 25, 23, 55, 31, 20, 22, 13, 62, 81, 50, 74, 65, 84, 80, 66, 64, 42, 12, 40};
    QuickSort(arr, 0, 19);
    for (i = 0; i < 20; i++)
        printf("%d ", arr[i]);
    system("pause");
    return 0;
}

void QuickSort(int *arr, int low, int high)
{
    if (low < high)
    {
        int pivot = QuickSort_Partition(arr, low, high);
        QuickSort(arr, low, pivot - 1);
        QuickSort(arr, pivot + 1, high);
    }
}

int QuickSort_Partition(int *arr, int low, int high)
{
    int i, j; // i作为分水岭,比主元小的在i左侧,比主元大的在右侧,i+1为第一个比主元大的
    int temp;
    QuickSort_RandomPivot(arr, low, high); // 随机选取主元
    int pivot = high;                      // 选取主元
    i = low - 1;                 // 初始化i为low-1,j为low
    for (j = low; j < high; j++) // 遍历所有元素
    {
        if (arr[j] <= arr[pivot]) // 当前元素比主元小,和i+1交换下
        {
            temp = arr[j];
            arr[j] = arr[i + 1];
            arr[i + 1] = temp;
            i++; // i右移
        }
    }
    temp = arr[pivot]; // 把主元放在中间做分界线
    arr[pivot] = arr[i + 1];
    arr[i + 1] = temp;
    return i + 1;
}

void QuickSort_RandomPivot(int *arr, int low, int high)
{
    srand((unsigned)time(NULL));
    int num = rand();
    int pivot = low + num % (high - low + 1);
    int temp;
    temp = arr[high];
    arr[high] = arr[pivot];
    arr[pivot] = temp;
}


6 归并排序

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>

void MergeSort(int *arr, int low, int high);
void Merge(int *arr, int low, int mid, int high);

int main()
{
    int i;
    int arr[] = {70, 25, 23, 55, 31, 20, 22, 13, 62, 81, 50, 74, 65, 84, 80, 66, 64, 42, 12, 40};
    MergeSort(arr, 0, 19);
    for (i = 0; i < 20; i++)
        printf("%d ", arr[i]);
    system("pause");
    return 0;
}

void MergeSort(int *arr, int low, int high)
{
    if (low >= high)
        return;
    int mid = (low + high) / 2;
    MergeSort(arr, low, mid);
    MergeSort(arr, mid + 1, high);
    Merge(arr, low, mid, high);
}

void Merge(int *arr, int low, int mid, int high)
{
    int i, j, k;
    int *assist = (int *)malloc(sizeof(int) * (high + 1)); // 辅助数组,将两部分copy过来
    for (k = low; k <= high; k++)
        assist[k] = arr[k];
    k = low;
    i = low;
    j = mid + 1;
    while (i <= mid && j <= high)
    {
        if (assist[i] <= assist[j])
            arr[k] = assist[i++];
        else
            arr[k] = assist[j++];
        k++;
    }
    while (i <= mid)
        arr[k++] = assist[i++];
    while (j <= high)
        arr[k++] = assist[j++];
}


7 堆排序

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>

void HeapSort_Max(int *arr, int n);
void Heap_Max_Adjust(int *arr, int n, int index);

int main()
{
    int i;
    int arr[] = {70, 25, 23, 55, 31, 20, 22, 13, 62, 81, 50, 74, 65, 84, 80, 66, 64, 42, 12, 40};
    HeapSort_Max(arr, 20);
    for (i = 0; i < 20; i++)
        printf("%d ", arr[i]);
    system("pause");
    return 0;
}

void HeapSort_Max(int *arr, int n)
{
    int i;
    int temp;
    // 建堆
    for (i = n / 2 - 1; i >= 0; i--)
        Heap_Max_Adjust(arr, n, i);

    // 排序
    for (i = n - 1; i > 0; i--)
    {
        temp = arr[0];
        arr[0] = arr[i];
        arr[i] = temp;
        Heap_Max_Adjust(arr, i, 0);
    }
}

void Heap_Max_Adjust(int *arr, int n, int index)
{
    int max = index; // 需调整结点与其孩子中最大的那个数
    int lchild, rchild;
    lchild = 2 * index + 1;
    rchild = 2 * index + 2;
    if (lchild < n && arr[max] < arr[lchild])
        max = lchild;
    if (rchild < n && arr[max] < arr[rchild])
        max = rchild;

    if (max != index) // 需调整堆
    {
        int temp;
        temp = arr[max];
        arr[max] = arr[index];
        arr[index] = temp;
        Heap_Max_Adjust(arr, n, max); // 可以非递归
    }
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
C语言中常见的排序算法包括归并排序、选择排序、直接插入排序、希尔排序、冒泡排序、快速排序、排序以及顺序查找和二分查找。这些排序算法各有特点,在不同情况下有着不同的应用场景和性能表现。 归并排序(Merge Sort):采用分治法,将待排序数组递归地分成两部分,分别进行排序,然后合并两个有序数组。 选择排序(Selection Sort):每次从未排序的部分选出最小(或最大)的元素,放到已排序部分的末尾。 直接插入排序(Insertion Sort):将未排序的元素逐个插入到已排序的数组中,直到全部元素有序。 希尔排序(Shell Sort):插入排序的改进版,通过间隔序列对数组进行分组,分组内部进行插入排序,逐步减小间隔直至为1。 冒泡排序(Bubble Sort):依次比较相邻的两个元素,将较大的元素交换到右侧,通过多次遍历,将最大的元素逐步冒泡到数组末尾。 快速排序(Quick Sort):通过选择一个基准元素,将数组分成两部分,左边部分小于基准,右边部分大于基准,然后递归地对左右子数组进行快速排序。 排序(Heap Sort):利用的性质进行排序,将数组构建成最大或最小,然后依次将顶元素与最后一个元素交换并重新调整,直到整个数组有序。 顺序查找(Sequential Search):逐个遍历数组元素,查找目标元素是否存在。 二分查找(Binary Search):针对有序数组,通过比较目标值与中间值的大小关系,缩小查找范围,直到找到目标值或确定不存在。 这些排序算法和查找方法在实际编程中都有广泛的应用,选择合适的算法可以提高程序的效率和性能。对于排序算法而言,需要考虑数据规模、数据分布、稳定性等因素,而对于查找算法,则需要考虑数据的有序性和查找频率等因素。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

友人帐_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值