5 数据结构课程设计个人任务5:快速排序

#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;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值