八大排序算法自我实现

来源:互联网 发布:windows 3.0 编辑:程序博客网 时间:2024/06/04 18:00

下面是的代码都是C++ 实现的
1.插入排序—直接插入排序(Straight Insertion Sort)

#pragma once#include "JudgmentCondition.h"void InsertionSort(int *arr, int arrsize, bool sortMethod){    int i = 0;    functionPointer fun = retfun(sortMethod);    for (int i = 1; i < arrsize; ++i)    {        int sentryPost = arr[i];        for (int j = 0; j < i; ++j)        {            if (fun(arr[j], sentryPost))            {                exchange(sentryPost, arr[j]);            }        }        exchange(arr[i], sentryPost);    }}
  1. 插入排序—希尔排序(Shell`s Sort)
  2. 选择排序—简单选择排序(Simple Selection Sort)
  3. 选择排序—堆排序(Heap Sort)
  4. 交换排序—冒泡排序(Bubble Sort)
#pragma once#include "JudgmentCondition.h"void BubbleSort(int *arr, int arrsize, bool sortMethod){    bool isBreak;    int i = 0;    functionPointer fun = retfun(sortMethod);    while (true)    {        if (i >= arrsize - 1)        {            if (isBreak)                break;            i = 0;            isBreak = true;        }        if (fun(arr[i], arr[i + 1]))        {            exchange(arr[i], arr[i + 1]);            isBreak = false;        }        ++i;    }}
  1. 交换排序—快速排序(Quick Sort)
  2. 归并排序(Merge Sort)
  3. 桶排序/基数排序(Radix Sort)

没写完。。。待完善。。

0 0