直接插入排序 二分排序 希尔排序 冒泡排序 快速排序 简单选择排序 堆排序
代码如下
#include<math.h>
#include<stdlib.h>
#include<string>
#include<stdbool.h>
#include<iostream>
using namespace std;
const int N = 666;
typedef struct student
{
int data;
}stu;
typedef struct
{
stu* elem;
int length;//当前表长
}Sqlist;
void InitSqlist(Sqlist& S)//初始化顺序表
{
S.elem = new stu[N + 1];
if (!S.elem)
{
cout << "初始化失败" << endl;
return;
}
S.length = 0;
int data;
cout << "请输入表中元素的值 输入10086停止" << endl;
cin >> data;
for (int i = 1; i <= N && data != 10086; i++)
{
S.elem[i].data = data;
S.length++;
cin >> data;
}
return;
}
void Traversal_List(Sqlist& L)//遍历表
{
for (int i = 1; i <= L.length; i++)
{
cout << L.elem[i].data << "->";
}
cout << "NULL" << endl << "遍历结束" << endl;
}
void SIsort(Sqlist& S)//直接插入排序
{
int i, j;
for (i = 2; i <= S.length; i++)
{
if (S.elem[i].data < S.elem[i - 1].data)
{
S.elem[0] = S.elem[i];
S.elem[i] = S.elem[i - 1];//S.elem[i-1]后移
for (j = i - 2; S.elem[j].data > S.elem[0].data; j--)
{
S.elem[j + 1] = S.elem[j];//后移
}
S.elem[j + 1] = S.elem[0];
}
}
}
void EFsort(Sqlist& S)//二分排序
{
int i, j, l, r, mid;
for (i = 2; i <= S.length; i++)
{
S.elem[0] = S.elem[i];
l = 1; r = i - 1;
while (l <= r)
{
mid = (l + r) / 2;
if (S.elem[0].data < S.elem[mid].data)
r = mid - 1;
else
l = mid + 1;
}
for (j = i - 1; j >= r + 1; j--)
{
S.elem[j + 1] = S.elem[j];
}
S.elem[j + 1] = S.elem[0];
}
}
int dt[5] = { 5,3,1 };//增量数组
void ShellInsert(Sqlist& S, int dk)//希尔插入
{
int i, j;
for (i = dk + 1; i <= S.length; i++)
{
if (S.elem[i].data < S.elem[i - dk].data)
{
S.elem[0] = S.elem[i];
for (j = i - dk; j > 0 && S.elem[j].data > S.elem[0].data; j -= dk)
{
S.elem[j + dk] = S.elem[j];
}
S.elem[j + dk] = S.elem[0];
}
}
}
void ShellSort(Sqlist& S, int dt[], int t)//希尔排序
{
for (int k = 1; k <= t; k++)
{
ShellInsert(S, dt[k]);
}
}
void BubbleSort(Sqlist& S)//冒泡排序
{
int m = S.length - 1;
int flag = 1; //标志作用 如果在一次循环中发生交换则继续下一轮 没有发生交换则说明已经排序完成
while (m > 0 && flag == 1)
{
flag = 0;
for (int j = 1; j <= m; j++)
{
if (S.elem[j].data > S.elem[j + 1].data)
{
flag = 1;
int data = S.elem[j].data;
S.elem[j] = S.elem[j + 1];
S.elem[j + 1].data = data;
}
}
m--;
}
}
int Partition(Sqlist& S, int low, int high)//寻找枢纽
{
S.elem[0] = S.elem[low];
while (low < high)
{
while (low < high && S.elem[high].data >= S.elem[0].data)
{
high--;
}
S.elem[low] = S.elem[high];
while (low < high && S.elem[low].data <= S.elem[0].data)
{
low++;
}
S.elem[high] = S.elem[low];
}
S.elem[low] = S.elem[0];
return low;
}
void QuickSort(Sqlist& S, int low, int high)//快排
{
if (low < high)
{
int pivot = Partition(S, low, high);
QuickSort(S, low, pivot - 1);
QuickSort(S, pivot + 1, S.length);
}
}
void SelectSort(Sqlist& S)//简单选择排序
{
for (int i = 1; i < S.length; i++)
{
int k = i;
for (int j = i + 1; j <= S.length; j++)
{
if (S.elem[j].data < S.elem[k].data)
{
k = j; //K为本次循环中最小值
}
}
int data = S.elem[i].data;
S.elem[i] = S.elem[k];
S.elem[k].data = data;
}
}
void HeapAdjust(Sqlist& S, int s, int m)//堆调整
{
int data = S.elem[s].data;
int j=0;
for ( j = s * 2; j <= m; j *= 2)
{
if (j<m&&S.elem[j].data < S.elem[j + 1].data)
j++;
if (data >= S.elem[j].data)
break;
S.elem[s].data = S.elem[j].data;
s = j;
}
S.elem[s].data = data;
}
void HeapCreat(Sqlist& S)//创建堆
{
int n = S.length;
for (int i = n / 2; i >= 1; i--)
{
HeapAdjust(S, i, n);
}
}
void HeapSort(Sqlist& S)//堆排序
{
HeapCreat(S);
for (int i = S.length; i > 1; i--)
{
int x = S.elem[1].data;
S.elem[1].data = S.elem[i].data;
S.elem[i].data = x;
HeapAdjust(S, 1, i - 1);
}
}
int main()
{
Sqlist S;
InitSqlist(S);
Traversal_List(S);
HeapSort(S);
Traversal_List(S);
}