#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;
}
}
快排与二分(C++)
最新推荐文章于 2022-11-05 19:15:04 发布