【数据结构】6-1内部排序(选择、插入、快排)
dataList类定义:
class dataList { private: int number; int *data, *bdata;//data数组是随机生成的,排序更改的都是这个数组,bdata就是用来存放排序前的 void reset();//重置数组为初始生成的乱序状态 public: dataList(int num); void Select_sort(); void Insert_sort(); void Qick_sort(int start, int end); void print(int kind); };
构造函数
dataList::dataList(int num) { data = new int[num]; bdata = new int[num]; number = num; for (int i = 0; i < number; i++) { data[i] = rand() % 100; bdata[i] = data[i]; } }
选择排序
void dataList::Select_sort()//最简单的选择排序 { for (int i = 0; i < number; i++) { for (int j = i; j < number; j++) { if (data[i] > data[j]) { int temp = data[j]; data[j] = data[i]; data[i] = temp; } } } }
插入排序
void dataList::Insert_sort() { for (int i = 0; i <number; ++i) { for (int j = i; j > 0; --j) { if (data[j] < data[j - 1]) { int temp = data[j]; data[j] = data[j - 1]; data[j - 1] = temp; } } } }
快速排序
void dataList::Qick_sort(int start, int end) { int i = start; int j = end; int temp = data[i]; if (i < j) { while (i < j) { while (i < j && data[j] >= temp) j--; if (i < j) { data[i] = data[j]; i++; } while (i < j && temp > data[i]) i++; if (i < j) { data[j] = data[i]; j--; } } data[i] = temp; Qick_sort(start, i - 1); Qick_sort(i + 1, end); } }
菜单
void menu()//模拟菜单选项 { cout << "---------------------排序小程序1.0@paul------------------" << endl; cout << "| |" << endl; cout << "| 1____________简单选择排序 |" << endl; cout << "| 2____________直接插入排序 |" << endl; cout << "| 3____________快速排序 |" << endl; cout << "| 4____________退出系统 |" << endl; cout << "| |" << endl; cout << "---------------------------------------------------------" << endl; }
额,复制过来格式就乱了,自己对齐吧
测试函数
int main() { srand(time(NULL)); dataList L(maxsize); menu(); while (1) { int select; cout << "请输入您的选择:"; cin >> select; switch (select) { case 1://简单选择排序 { L.Select_sort(); L.print(select); break; } case 2://直接插入排序 L.Insert_sort(); L.print(select); break; case 3://快速排序 L.Qick_sort(0,maxsize-1); L.print(select); break; case 4: system("pause"); return 0; default: cout << "您输入的选项有误,请重新输入:"; } } }
完整代码
#include<iostream> #include<ctime> const int maxsize = 10; using namespace std; class dataList { private: int number; int *data, *bdata;//data数组是随机生成的,排序更改的都是这个数组,bdata就是用来存放排序前的 void reset();//重置数组为初始生成的乱序状态 public: dataList(int num); void Select_sort(); void Insert_sort(); void Qick_sort(int start, int end); void print(int kind); }; dataList::dataList(int num) { data = new int[num]; bdata = new int[num]; number = num; for (int i = 0; i < number; i++) { data[i] = rand() % 100; bdata[i] = data[i]; } } void dataList::Select_sort()//最简单的选择排序 { for (int i = 0; i < number; i++) { for (int j = i; j < number; j++) { if (data[i] > data[j]) { int temp = data[j]; data[j] = data[i]; data[i] = temp; } } } } void dataList::reset() { for (int i = 0; i < number; i++) { data[i] = bdata[i]; } } void dataList::Insert_sort() { for (int i = 0; i <number; ++i) { for (int j = i; j > 0; --j) { if (data[j] < data[j - 1]) { int temp = data[j]; data[j] = data[j - 1]; data[j - 1] = temp; } } } } void dataList::Qick_sort(int start, int end) { int i = start; int j = end; int temp = data[i]; if (i < j) { while (i < j) { while (i < j && data[j] >= temp) j--; if (i < j) { data[i] = data[j]; i++; } while (i < j && temp > data[i]) i++; if (i < j) { data[j] = data[i]; j--; } } data[i] = temp; Qick_sort(start, i - 1); Qick_sort(i + 1, end); } } void dataList::print(int kind) { cout << "原始数据: "; for (int i = 0; i < number; i++) { cout << bdata[i] << " "; } cout << endl; switch (kind) { case 1: cout << "简单选择排序后:"; break; case 2: cout << "直接插入排序后:"; break; case 3: cout << "快速排序后: "; break; } for (int i = 0; i < number; i++) { cout << data[i] << " "; } reset();//打印完就需要重置待排序数组啦 cout << endl; } void menu()//模拟菜单选项 { cout << "---------------------排序小程序1.0@paul------------------" << endl; cout << "| |" << endl; cout << "| 1____________简单选择排序 |" << endl; cout << "| 2____________直接插入排序 |" << endl; cout << "| 3____________快速排序 |" << endl; cout << "| 4____________退出系统 |" << endl; cout << "| |" << endl; cout << "---------------------------------------------------------" << endl; } int main() { srand(time(NULL)); dataList L(maxsize); menu(); while (1) { int select; cout << "请输入您的选择:"; cin >> select; switch (select) { case 1://简单选择排序 { L.Select_sort(); L.print(select); break; } case 2://直接插入排序 L.Insert_sort(); L.print(select); break; case 3://快速排序 L.Qick_sort(0,maxsize-1); L.print(select); break; case 4: system("pause"); return 0; default: cout << "您输入的选项有误,请重新输入:"; } } }
测试结果