#include <iostream>
#include <vector>
#include <algorithm>
#include <fstream>
using namespace std;
int bubble(); //冒泡排序
int quick(); //快速排序
int quicksort(double table[],size_t left,size_t right); //快速排序的实现
int shell(); //希尔排序
int inser(); //插入排序
int selection(); //选择排序
int radix(); //基数排序
int stringsort(); //对一个字符串里的字符进行排序
int stringsortinput(); //输入排序的标准
int stringsortchoose(); //从文件读取排序的标准
int main()
{
bool toD = true;
while (toD)
{
cout << "1,bubble sort \t\t\t";
cout << "2,quick sort " << endl;
cout << "3,shell sort \t\t\t";
cout << "4,inser sort " << endl;
cout << "5,selection sort \t\t\t";
cout << "6,radix sort " << endl;
cout << "7,string sort \t\t\t";
cout << "other exit" << endl;
int r;
cin >> r;
switch (r)
{
case 1:
bubble();
break;
case 2:
quick();
break;
case 3:
shell();
break;
case 4:
inser();
break;
case 5:
selection();
break;
case 6:
radix();
break;
case 7:
stringsort();
break;
default:
toD = false;
break;
}
}
return 0;
}
int bubble() //冒泡排序
{
cout << "enter data:" << endl;
double num;
vector<double> table;
while (cin >> num)
table.push_back(num);
typedef vector<double>::size_type size;
size s = table.size();
for (size i=0; i<s; i++)
{
for(size j=0; j<s-1-i; j++)
{
if (table[j] > table[j+1])
{
double p;
p = table[j+1];
table[j+1] = table[j];
table[j] = p;
}
}
}
for (size k=0;k<s;k++)
cout << table[k] << "\t";
cout << endl;
cin.clear();
return 0;
}
int quick() //快速排序
{
cout << "enter data:" <<endl;
int s = 0;
double num;
double table[1000000];
while (cin >> num)
{
table[s] = num;
s += 1;
}
quicksort(table,0,s-1);
for (int k=0;k<s;k++)
cout << table[k] << "\t";
cout << endl;
cin.clear();
return 0;
}
int quicksort(double table[],size_t left,size_t right) //快速排序的实现
{
size_t p = (left + right) / 2;
double tp;
size_t i = left,j = right,k;
while(i<j)
{
while(i < p)
{
if(table[p] > table[i])
i++;
else
{
tp = table[i];
for(k = i;k < p;k++)
table[k] = table[k+1];
table[p] = tp;
p = p - 1;
}
}
while(j > p)
{
if(table[p] < table[j])
j--;
else
{
tp = table[j];
for(k = j;k > p;k--)
table[k] = table[k-1];
table[p] = tp;
p = p + 1;
}
}
}
if (p - left > 1)
quicksort(table,left,p - 1);
if (right - p > 1)
quicksort(table,p + 1,right);
return 0;
}
int shell() //希尔排序
{
cout << "enter data:" << endl;
double num,p;
vector<double> table;
while (cin >> num)
table.push_back(num);
typedef vector<double>::size_type size;
size s = table.size();
for(size i = s / 2;i > 0;i /= 2)
{
for (size j = i; j < s; j++)
{
p = table[j];
int k;
for (k = j - i; k >= 0 && table[k] > p; k -= i)
table[k+i] = table[k];
table[k+i] = p;
}
}
for (size k=0;k<s;k++)
cout << table[k] << "\t";
cout << endl;
cin.clear();
return 0;
}
int inser() //插入排序
{
cout << "enter data:" << endl;
double num,p;
vector<double> table;
while (cin >> num)
table.push_back(num);
typedef vector<double>::size_type size;
size s = table.size();
for (size i = 1; i < s; i++)
{
p = table[i];
int j = i - 1;
for (; table[j] > p && j >=0;)
{
table[j+1] = table[j];
j--;
}
table[j+1] = p;
}
for (size k=0;k<s;k++)
cout << table[k] << "\t";
cout << endl;
cin.clear();
return 0;
}
int selection() //选择排序
{
cout << "enter data:" << endl;
double num,p;
vector<double> table;
while (cin >> num)
table.push_back(num);
typedef vector<double>::size_type size;
size s = table.size();
for (size i = 0; i < s; i++)
{
int m = i;
for (size j = i +1; j < s; j++)
{
if (table[m] > table[j])
{
p = table[j];
table[j] = table[m];
table[m] = p;
}
}
}
for (size k=0;k<s;k++)
cout << table[k] << "\t";
cout << endl;
cin.clear();
return 0;
}
int radix() //基数排序
{
cout << "enter data:" << endl;
int s = 0;
int num;
int table[1000000];
while (cin >> num)
{
table[s] = num;
s += 1;
}
int b[1000];
int e = 1;
int p = table[0];
for (int i = 0; i < s; i++)
if (p < table[i])
p = table[i];
while(p / e > 0)
{
int box[10] = {0};
for (int i = 0; i < s; i++)
box[(table[i] / e) % 10]++;
for (int i = 1; i < 10; i++)
box[i] += box[i - 1];
for (int j = s - 1; j >= 0; j--)
{
b[box[(table[j] / e) % 10] - 1] = table[j];
box[(table[j] / e) % 10]--;
}
for (int i = 0; i < s; i++)
table[i] = b[i];
e *= 10;
}
for (int k=0;k<s;k++)
cout << table[k] << "\t";
cout << endl;
cin.clear();
return 0;
}
int stringsort() //对一个字符串里的字符进行排序
{
bool toD = true;
while(toD)
{
cout << "1,input template \t\t\t" << "2,choose txt" << endl;
cout << "other back" << endl;
int r;
cin >> r;
switch(r)
{
case 1:
stringsortinput();
break;
case 2:
stringsortchoose();
break;
default:
toD = false;
break;
}
}
return 0;
}
int stringsortinput() //输入排序的标准
{
char ch;
int ts = 0; //标准字符数组的长度
int ss = 0; //待排序字符数组的长度
int news = 0; //已排序字符数组的长度
char t[1000000]; //标准字符数值
char s[1000000]; //待排序的字符数组
char ne[1000000]; //已排序的字符数组
cout << "enter template string" << endl;
while (cin >> ch && ch != '#')
{
t[ts] = ch;
ts += 1;
}
cout << "enter to the sort string" << endl;
while (cin >> ch && ch != '#')
{
s[ss] = ch;
ss += 1;
}
cout <<"The template string is : ";
for(int i = 0; i < ts; i++)
cout << t[i] << " ";
cout << endl;
cout <<"The input string is : ";
for(int i = 0; i < ss; i++)
cout << s[i] << " ";
cout << endl;
for(int i = 0; i < ts; i++)
{
for(int j = 0; j < ss; j++)
{
if(s[j] == t[i])
{
ne[news] = s[j];
news++;
}
}
}
cout <<"The new string is : ";
for(int i = 0; i < news; i++)
cout << ne[i] << " ";
cout << endl;
cin.clear();
return 0;
}
int stringsortchoose() //从文件读取排序的标准
{
ifstream Temp;
Temp.open("Temp.txt");
char ch;
int ts = 0;
int ss = 0;
int news = 0;
char t[10000];
char s[1000000];
char ne[1000000];
while( Temp.get(t[ts]) && ('\n' != t[ts]) )
ts++;
Temp.close();
cout << "enter to the sort string" << endl;
while (cin >> ch && ch != '#')
{
s[ss] = ch;
ss += 1;
}
cout <<"The template string is : ";
for(int i = 0; i < ts; i++)
cout << t[i] << " ";
cout << endl;
cout <<"The input string is : ";
for(int i = 0; i < ss; i++)
cout << s[i] << " ";
cout << endl;
for(int i = 0; i < ts; i++)
{
for(int j = 0; j < ss; j++)
{
if(s[j] == t[i])
{
ne[news] = s[j];
news++;
}
}
}
cout <<"The new string is : ";
for(int i = 0; i < news; i++)
cout << ne[i] << " ";
cout << endl;
cin.clear();
return 0;
}