//project.h
#pragma once
#include
#include
using namespace std;
class charusort
{
public:
void printf_array(int a[], int n) {
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << endl<= 0 && a[j] > key) {
a[j + 1] = a[j];
j--;
}
a[j + 1] = key;
cout << " 第" << i << "趟排序:";
printf_array(a, n);
}
cout << " 若有n个元素,插入排序的比较次数为:1+2+...+(n-2)+(n-1)=n(n-1)/2 nn";
}
};
class kuaisusort
{
public:
int p;
void ListShow(int list[], int n) {
cout << " 第" << p++ << "次排序:";
for (int i = 0; i < n; i++) {
cout << list[i] << " ";
}
cout << endl << endl;
}
int Partition(int list[], int low, int high,int n) {
int key = list[low];//选取low指针指向的数据为关键字
while (low < high) {
while (list[high] >= key && low < high) {
high--;//high指针往前移
}
if (low < high) {
int temp = list[low];
list[low] = list[high];
list[high] = temp;
}
while (list[low] <= key && low < high) {
low++;//high指针往前移
}
if (low < high) {
int temp1 = list[low];
list[low] = list[high];
list[high] = temp1;
}
}
ListShow(list, n);
return low;
}
void QuickSort(int list[], int low, int high,int n) {
if (low >= high) {
return;
}
else {
int par = Partition(list, low, high,n);
QuickSort(list, low, par - 1,n);//快速排序左子序列
QuickSort(list, par + 1, high,n);//快速排序右子序列
}
}
};
class maopaosort
{
public:
maopaosort(int s[],int n) {
cout << " 冒泡排序:nn 时间复杂度为:nn 最坏情况:O(N^2),最好情况:O(N)nn 空间复杂度为O(1),稳定排序nn";
cout << " 思路:左边大于右边交换一趟排下来最大的在右边nn";
for (int j = 0; j < n; j++) {
for (int y = 0; y < n - j - 1;y++) {
if (s[y]> s[y + 1]) {
int m = s[y];
s[y] = s[y + 1];
s[y + 1] = m;
}
}
if (j == n - 1) continue;
cout << " 第" << j+1<< "趟的排序为:";
for (int u = 0; u < n; u++)
cout << s[u] << " ";
cout << endl<= 1) {
for (int i = gap; i < len; i++) {
//对arr[i], arr[i-h], arr[i-2h], ... 使用插入排序
int e = arr[i];
int j;
for (j = i; j >= gap && e < arr[j - gap]; j -= gap) {
arr[j] = arr[j - gap];
}
arr[j] = e;
int u;
cout << " 第" << t << "趟排序:"; t++;
for (u = 0; u < len; u++)
cout << arr[u] << " ";
cout << endl << endl;
}
gap /= 3;
}
}
};
main.cpp
#include "project.h"
#include
#include
using namespace std;
int s[100];//数组
int n;
void startsystem() {//启动系统
cout <<"n=================================================================" << endl<> t;
s[i] = t;
cout << endl;
cout << " 是否继续输入?(0表示结束,其他数字表示继续),请输入:";
cin >> h; cout << endl;
if (h == 0) break;
n++; i++;
}
cout << "=================================================================" << endl << endl;
cout << " 初始数据为:n" << endl << " ";
for (int j = 0; j < n; j++)
cout << s[j] << " ";
cout << endl << endl;
}
int main() {
while(1){
int a;
startsystem();
choisesort();
cin >> a;
if (a == 5) break;
cout << "nn=================================================================" << endl << endl;
systemui();
switch (a)
{
case 1:
charusort(s, n);
break;
case 2:
cout << " 快速排序:时间复杂度为nlog2n,空间复杂度为nlog2n,不稳定排序nn";
kuaisusort k;
k.p = 1;
k.QuickSort(s, 0, n - 1,n);
break;
case 3:
maopaosort(s,n);
break;
case 4:
shellSort(s, n);
break;
}
cout << "n=================================================================" << endl << endl;
cout << "n 1.继续 2.退出 请选择:";
int d; cin >> d;
if (d == 2)
break;
system("cls");
}
cout << endl<