#include<iostream>
#include<vector>
using namespace std;
template<class key_type>
class Record
{
private:
key_type key;
public:
Record(){}
void set_key(int k) { this->key = k; }
key_type get_key() { return this->key; }
};
class Sort : private Record<int>
{
private:
void swap_ele(Record *arr, int first, int second);
int partition(Record *arr, int low, int high);
public:
Sort(){}
void Menu();
void Insert_Sort(Record *arr, int num);
void Quick_Sort(Record *arr, int low, int high);
void Bubble_Sort(Record *arr, int num);
};
void Sort::Menu()
{
cout << "**************************" << endl
<< "**************************" << endl
<< "1.直接插入排序" << endl
<< "2.快速排序" << endl
<< "3.冒泡排序" << endl
<< "4.显示姓名" << endl
<< "**************************" << endl;
}
void Sort::Insert_Sort(Record *arr, int num)
{
Record *temp = new Record;
int i = 0, j = 0;
for (i = 1; i < num; i++)
{
if (arr[i].get_key() < arr[i - 1].get_key())
{
*temp = arr[i];
for (j = i - 1; j >= 0 && arr[j].get_key() > temp->get_key(); j--)
{
arr[j + 1] = arr[j];
}
arr[j + 1] = *temp;
}
}
}
void Sort::swap_ele(Record *arr, int first, int second)
{
Record *temp = new Record;
*temp = arr[first];
arr[first] = arr[second];
arr[second] = *temp;
}
int Sort::partition(Record *arr, int low, int high)
{
int mid = high / 2;
if (arr[low].get_key() > arr[high].get_key())
{
this->swap_ele(arr, low, high);
}
if (arr[mid].get_key() > arr[high].get_key())
{
this->swap_ele(arr, mid, high);
}
if (arr[mid].get_key() > arr[low].get_key())
{
this->swap_ele(arr, mid, low);
}
int pivot_key = arr[low].get_key();
Record* pivot_temp = new Record;
pivot_temp->set_key(pivot_key);
while (low < high)
{
while (low < high && arr[high].get_key() >= pivot_key)
{
high--;
}
arr[low] = arr[high];
while (low < high && arr[low].get_key() <= pivot_key)
{
low++;
}
arr[high] = arr[low];
}
arr[low] = *pivot_temp;
return low;
}
void Sort::Quick_Sort(Record *arr, int low, int high)
{
int pivot = 0;
if (low < high)
{
pivot = this->partition(arr, low, high);
this->Quick_Sort(arr, low, pivot - 1);
this->Quick_Sort(arr, pivot + 1, high);
}
}
void Sort::Bubble_Sort(Record *arr, int num)
{
Record *temp = new Record;
int i = 0, j = 0;
bool flag = 1;
for (i = 0; i < num - 1 && flag; i++)
{
flag = 0;
for (j = num - 2; j >= i; j--)
{
if (arr[j].get_key() > arr[j + 1].get_key())
{
*temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = *temp;
flag = 1;
}
}
}
}
int main()
{
int num;
cout << "请输入要排序的序列个数:" << endl;
cin >> num;
Record<int> *arr = new Record<int>[num];
int input;
cout << "请输入序列:" << endl;
for (int i = 0; i < num; i++)
{
cin >> input;
arr[i].set_key(input);
}
int choice = 0;
Sort *s = new Sort;
s->Menu();
while (1)
{
cout << "**************************" << endl;
cout << "请输入你的选择(输入选项外其他数字退出):" << endl;
cin >> choice;
switch (choice)
{
case 1:
s->Insert_Sort(arr, num);
cout << "直接插入排序的结果:" << endl;
for (int i = 0; i < num; i++)
{
cout << arr[i].get_key() << "\t";
}
cout << endl;
break;
case 2:
s->Quick_Sort(arr, 0, num - 1);
cout << "快速排序的结果:" << endl;
for (int i = 0; i < num; i++)
{
cout << arr[i].get_key() << "\t";
}
cout << endl;
break;
case 3:
s->Bubble_Sort(arr, num);
cout << "冒泡排序的结果:" << endl;
for (int i = 0; i < num; i++)
{
cout << arr[i].get_key() << "\t";
}
cout << endl;
break;
case 4:
cout << "计科6班——罗炳培" << endl;
break;
default:
exit(0);
break;
}
}
return 0;
}