快排与二分(C++)

#include<iostream>
using namespace std;
#include<algorithm>
#include<string>

#include<Windows.h>  
template<typename ElemType>
class Tool{
	protected:
		ElemType* Data;
		int Data_count;
		int At_present_index;
	public:
		Tool();
		Tool(int);
		~Tool();
	public:
		void Sort();
		int find();
		void expand_size();
		void add_data();
		void Show_data();
		void sort_by_algorithm();
	private:
		void quicksort(int,int);
		int partition(int,int);
		int binary_find(ElemType);
		ElemType* expand();
		void add(int);
};

int main(){
	Tool<int> t(9);
	t.add_data();
	t.Show_data();
//	t.sort_by_algorithm();
//	DWORD start_time = GetTickCount(); 
//	t.sort();
//	DWORD end_time = GetTickCount();
//	cout << "The run time is:" << (end_time - start_time) << "ms!" << endl; 
	t.find();
	t.Show_data();
	return 0;
} 

template<typename ElemType>
Tool<ElemType>::Tool():At_present_index(0)
{
	const int Default=10;
	Data_count=Default;
	Data = new ElemType[Default];
}

template<typename ElemType>
Tool<ElemType>::Tool(int data_count)
	:Data_count(data_count),At_present_index(0)
{
	Data = new ElemType[Data_count];
}

template<typename ElemType>
Tool<ElemType>::~Tool()
{
	delete[]Data;
}

template<typename ElemType>
void Tool<ElemType>::sort_by_algorithm(){
	sort(Data,Data+this->At_present_index);
}

template<typename ElemType>
void Tool<ElemType>::Show_data(){
	cout<<"数据:[";
	for(int i=0;i<this->At_present_index;i++){
		cout<<Data[i];
		if(i<this->At_present_index-1)
			cout<<",";
		else
			cout<<"]"<<endl;
	}
}

template<typename ElemType>
void Tool<ElemType>::add_data(){
	cout<<"输入增加数据的个数:";
	int count;
	cin>>count;
	while(count>this->Data_count)
		expand_size();
	add(count);
}

template<typename ElemType>
void Tool<ElemType>::add(int count){
	for(;this->At_present_index<count;this->At_present_index++){
		cout<<"输入第"<<this->At_present_index+1<<"个数据:";
		cin>>Data[this->At_present_index];
	}
	cout<<"添加数据成功!"<<endl;
}

template<typename ElemType>
void Tool<ElemType>::Sort(){
	quicksort(0,this->At_present_index-1);
}
	
template<typename ElemType>
int Tool<ElemType>::find(){
	ElemType x;
	cout<<"输入你要查找的值:";
	cin>>x;
	binary_find(x);
}
	
template<typename ElemType>
void Tool<ElemType>::expand_size(){
	this->Data=expand();
}

template<typename ElemType>
ElemType* Tool<ElemType>::expand(){
	ElemType* NewData = new ElemType[this->Data_count+(this->Data_count>>1)];
	for(int i=0;i<this->At_present_index;i++)
		NewData[i]=this->Data[i];
	return NewData;
}

template<typename ElemType>
int Tool<ElemType>::partition(int low,int high){
	int i=low,j=high;
	ElemType x=Data[low];
	while(i<j){
		while(i<j&&Data[j]>=x) j--;
		if(i<j){
			Data[i] = Data[j];
			i++;
		}
		while(i<j&&Data[i]<x) i++;
		if(i<j){
			Data[j] = Data[i];
			j--;
		}
	}
	Data[i]=x;
	return i;
}
	
template<typename ElemType>
void Tool<ElemType>::quicksort(int low,int high){
	if(low<high){
		int index=partition(low,high);
		quicksort(low,index-1);
		quicksort(index+1,high);
	}
}
	
template<typename ElemType>
int Tool<ElemType>::binary_find(ElemType x){
	quicksort(0,this->At_present_index-1);
	int i=0,j=this->At_present_index-1,mid;
	bool flag=false;
	while(i<=j){
		mid = j + ((i-j)>>1);
		if(Data[mid]>x)
			j=mid-1;
		else if(Data[mid]<x)
			i=mid+1;
		else{
			cout<<"数据:"<<x<<"在有序递增数组的位置为:"<<mid<<endl;
			flag=true;
			return mid;
		}
	}
	if(!flag){
		cout<<"未找到数据:"<<x<<endl;
		return -1;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值