#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <sstream>
#include <stack>
#include <map>
#include <ctime>
#include <array>
#include <set>
using namespace std;
vector<int> departString_int(string data)
{
vector<int> back_part;//output type
int i, j;
vector<string> part;
string A_part;
stringstream room;
room.str(data);
while (room >> A_part)
part.push_back(A_part);
for (i = 0; i < part.size(); i++)
{
int num_cahe;
num_cahe = atoi(part[i].c_str());
back_part.push_back(num_cahe);
}
return back_part;
}
vector<double> departString_double(string data)
{
vector<double> back_part;//output type
int i, j;
vector<string> part;
string A_part;
stringstream room;
room.str(data);
while (room >> A_part)
part.push_back(A_part);
for (i = 0; i < part.size(); i++)
{
double num_cahe;
num_cahe = atof(part[i].c_str());
back_part.push_back(num_cahe);
}
return back_part;
}
vector<char> departString_char(string data)
{
vector<char> back_part;//output type
int i, j;
vector<string> part;
string A_part;
stringstream room;
room.str(data);
while (room >> A_part)
part.push_back(A_part);
for (i = 0; i < part.size(); i++)
{
char num_cahe;
num_cahe = part[i].at(0);
back_part.push_back(num_cahe);
}
return back_part;
}
vector<string> departString_string(string data)
{
vector<int> back_part;//output type
int i, j;
vector<string> part;
string A_part;
stringstream room;
room.str(data);
while (room >> A_part)
part.push_back(A_part);
return part;
}
//====================================================
template<class ElemType>
int divide(vector<ElemType>& A, int low, int high)//划分函数(快速排序)
{
ElemType num = A[low];
while (low < high)
{
while (low < high && A[high] > num)
high--;
A[low] = A[high];
while (low < high && A[low] <= num)
low++;
A[high] = A[low];
}
A[low] = num;
return low;
}
template<class ElemType>
void QuickSort_down(vector<ElemType>& A, int low, int high)//快速排序的实现(递归)
{
if (low < high)
{
int q = divide(A, low, high);
int kk;
for (kk = 0; kk < A.size() - 1; kk++)
cout << A[kk] << ",";
cout << A[kk] << endl;
QuickSort_down(A, low, q - 1);
QuickSort_down(A, q + 1, high);
}
}
template<class ElemType>
void QuickSort(vector<ElemType> &A)//快速排序的实现(外壳)
{
if (A.size() == 1)
{
cout << A[0] << endl;
return;
}
QuickSort_down(A, 0, A.size() - 1);
return;
}
int main()
{
vector<int> input_int;
vector<double> input_double;
vector<char> input_char;
vector<string> input_string;
int kinds_num;
int dlta[100], t;
cin >> kinds_num;
if (kinds_num != 0 && kinds_num != 1 && kinds_num != 2 && kinds_num != 3)
{
cout << "err" << endl;
return 0;
}
cin.get();
string s1;
getline(cin, s1);
if (kinds_num == 0)
input_int = departString_int(s1);
if (kinds_num == 1)
input_double = departString_double(s1);
if (kinds_num == 2)
input_char = departString_char(s1);
if (kinds_num == 3)
input_string = departString_string(s1);
if (kinds_num == 0)
{
QuickSort(input_int);
}
if (kinds_num == 1)
{
QuickSort(input_double);
}
if (kinds_num == 2)
{
QuickSort(input_char);
}
if (kinds_num == 3)
{
QuickSort(input_string);
}
return 0;
}
5 数据结构课程设计个人任务5:快速排序
最新推荐文章于 2023-04-15 22:37:01 发布