Quick Sort c++,int main()中的代码实现
Quick Sort c++, code implementation in int main()
我尝试开发c++程序,Quick Sort。在 int main()
中,第一个菜单是:
1. 100 elements
2. 500 elements
3. 1000 elements.
4. Quit
用户选择或输入1/2/3后,用户需要选择:
3. Pivot: First Element
4. Pivot: Random element.
问题是我的代码太长了,因为我没有让它变得简单,而且我不知道该怎么做。我用同样的方法。
用于对 100 个元素进行排序:
Load data>>print unsorted>>quicksort>>print sorted. This is for pivot
first element.
Then for pivot random element, I need to load again the same 100
elements data and do the same again,Load data>>print
unsorted>>quicksort>>print sorted.
同样适用于 500 和 1000 个元素。看到我这样的代码好累
int main(){
int ch,ch2;
while(1)
{
cout<<endl<<endl;
cout << "\n QUICK SORT(RANDOM NUMBER)\n";
cout << " ---------------------------------\n";
cout << "\n 1.100 elements\n 2.500 elements\n 3.1000 elements\n 4.Quit\n";
cout<< " ---------------------------------"<<endl;
cout<<" Enter your choice : ";
cin>>ch;
cout<<endl;
switch(ch)
{
case 1 : cout << "\n 3.Pivot : First Element \n 4.Pivot: Random element\n";
cout<< " ---------------------------------"<<endl;
cout<<" Enter your choice : ";
cin>>ch2;
cout<<endl;
if(ch2==3){
ifstream file;
file.open("100.txt");
if(!file) {
cout<<" Error opening file. " << endl;
}
int input1[100];
for(int i = 0; i < 100; i++)
{ file>>input1[i];
}
file.close();
cout << " UNSORTED DATA (100 elements) \n";
cout<< " ------------------------------------------------"<<endl;
print(input1,99);
//Quick sort Pivot: first element
cout << " SORTED DATA (100 elements) Pivot: First Element: \n";
cout<< " ------------------------------------------------"<<endl;
quicksort1(input1, 0, 99);
cout<<endl;
print(input1,99);
cout << " Number of comparison: \n";
cout << " Number of moves required: \n";
cout<<endl;
break;
}
else{
ifstream file2;
file2.open("100.txt");
if(!file2) {
cout<<" Error opening file. " << endl;
}
int input2[100];
for(int i = 0; i < 100; i++)
{ file2>>input2[i];
}
file2.close();
cout << " UNSORTED DATA (100 elements) \n";
cout<< " ------------------------------------------------"<<endl;
print(input2,99);
//Quick sort Pivot: random element
cout << " SORTED DATA (100 elements) Pivot: Random Element: \n";
cout << " ------------------------------------------------"<<endl;
print(input2,99);
quicksort2(input2, 0, 99);
cout<<endl;
print(input2,99);
cout << " Number of comparison: \n";
cout << " Number of moves required: \n";
cin.get();
//if windows suddenly close
break;
}
break;
case 2 :cout << "\n 3.Pivot : First Element \n 4.Pivot: Random element\n";
cout<< " ---------------------------------"<<endl;
cout<<" Enter your choice : ";
cin>>ch2;
cout<<endl;
if(ch2==3){
ifstream file;
file.open("500.txt");
if(!file) {
cout<<" Error opening file. " << endl;
}
int input1[500];
for(int i = 0; i < 500; i++)
{ file>>input1[i];
}
file.close();
cout << " UNSORTED DATA (500 elements) \n";
cout<< " ------------------------------------------------"<<endl;
print(input1,499);
//Quick sort Pivot: first element
cout << " SORTED DATA (500 elements) Pivot: First Element: \n";
cout<< " ------------------------------------------------"<<endl;
quicksort1(input1, 0, 499);
cout<<endl;
print(input1,499);
cout << " Number of comparison: \n";
cout << " Number of moves required: \n";
cout<<endl;
break;
}
else{
ifstream file2;
file2.open("500.txt");
if(!file2) {
cout<<" Error opening file. " << endl;
}
int input2[500];
for(int i = 0; i < 500; i++)
{ file2>>input2[i];
}
file2.close();
cout << " UNSORTED DATA (500 elements) \n";
cout<< " ------------------------------------------------"<<endl;
print(input2,499);
//Quick sort Pivot: random element
cout << " SORTED DATA (100 elements) Pivot: Random Element: \n";
cout << " ------------------------------------------------"<<endl;
print(input2,499);
quicksort2(input2, 0, 499);
cout<<endl;
print(input2,499);
cout << " Number of comparison: \n";
cout << " Number of moves required: \n";
cin.get();
//if windows suddenly close
break;
}
break;
case 3 :
break;
case 4 :
break;
}
我建议您先采用 DRY 方法,首先考虑 2 个变量:
1- 输入数
2- 枢轴选择
cout<<endl<<endl;
cout << "\n QUICK SORT(RANDOM NUMBER)\n";
cout << " ---------------------------------\n";
cout << "\n 1.100 elements\n 2.500 elements\n 3.1000 elements\n 4.Quit\n";
cout<< " ---------------------------------"<<endl;
cout<<" Enter your choice : ";
cin>>ch;
cout<<endl;
cout << "\n 3.Pivot : First Element \n 4.Pivot: Random element\n";
cout<< " ---------------------------------"<<endl;
cout<<" Enter your choice : ";
cin>>ch2;
cout<<endl;
string inputSize;
switch(ch)
{
case 1: inputSize="100";
...
}
string pivotSelection;
switch(ch2)
{
case 1: pivotSelection="FirstElement";
...
}
然后重写您的代码块以使用这些变量而不是对所有内容进行硬编码。
例如,您可以 file.open( inputSize + ".txt");
而不是 file.open("500.txt");
但是,使用此方法时,您必须注意,在 C++ 中,您不能创建可变大小的堆栈分配数组。例如,您需要 int * inputs = malloc(sizeof(int) * sizeOfInput);
而不是 int inputs[sizeOfInput];
我尝试开发c++程序,Quick Sort。在 int main()
中,第一个菜单是:
1. 100 elements
2. 500 elements
3. 1000 elements.
4. Quit
用户选择或输入1/2/3后,用户需要选择:
3. Pivot: First Element
4. Pivot: Random element.
问题是我的代码太长了,因为我没有让它变得简单,而且我不知道该怎么做。我用同样的方法。
用于对 100 个元素进行排序:
Load data>>print unsorted>>quicksort>>print sorted. This is for pivot first element.
Then for pivot random element, I need to load again the same 100 elements data and do the same again,Load data>>print unsorted>>quicksort>>print sorted.
同样适用于 500 和 1000 个元素。看到我这样的代码好累
int main(){
int ch,ch2;
while(1)
{
cout<<endl<<endl;
cout << "\n QUICK SORT(RANDOM NUMBER)\n";
cout << " ---------------------------------\n";
cout << "\n 1.100 elements\n 2.500 elements\n 3.1000 elements\n 4.Quit\n";
cout<< " ---------------------------------"<<endl;
cout<<" Enter your choice : ";
cin>>ch;
cout<<endl;
switch(ch)
{
case 1 : cout << "\n 3.Pivot : First Element \n 4.Pivot: Random element\n";
cout<< " ---------------------------------"<<endl;
cout<<" Enter your choice : ";
cin>>ch2;
cout<<endl;
if(ch2==3){
ifstream file;
file.open("100.txt");
if(!file) {
cout<<" Error opening file. " << endl;
}
int input1[100];
for(int i = 0; i < 100; i++)
{ file>>input1[i];
}
file.close();
cout << " UNSORTED DATA (100 elements) \n";
cout<< " ------------------------------------------------"<<endl;
print(input1,99);
//Quick sort Pivot: first element
cout << " SORTED DATA (100 elements) Pivot: First Element: \n";
cout<< " ------------------------------------------------"<<endl;
quicksort1(input1, 0, 99);
cout<<endl;
print(input1,99);
cout << " Number of comparison: \n";
cout << " Number of moves required: \n";
cout<<endl;
break;
}
else{
ifstream file2;
file2.open("100.txt");
if(!file2) {
cout<<" Error opening file. " << endl;
}
int input2[100];
for(int i = 0; i < 100; i++)
{ file2>>input2[i];
}
file2.close();
cout << " UNSORTED DATA (100 elements) \n";
cout<< " ------------------------------------------------"<<endl;
print(input2,99);
//Quick sort Pivot: random element
cout << " SORTED DATA (100 elements) Pivot: Random Element: \n";
cout << " ------------------------------------------------"<<endl;
print(input2,99);
quicksort2(input2, 0, 99);
cout<<endl;
print(input2,99);
cout << " Number of comparison: \n";
cout << " Number of moves required: \n";
cin.get();
//if windows suddenly close
break;
}
break;
case 2 :cout << "\n 3.Pivot : First Element \n 4.Pivot: Random element\n";
cout<< " ---------------------------------"<<endl;
cout<<" Enter your choice : ";
cin>>ch2;
cout<<endl;
if(ch2==3){
ifstream file;
file.open("500.txt");
if(!file) {
cout<<" Error opening file. " << endl;
}
int input1[500];
for(int i = 0; i < 500; i++)
{ file>>input1[i];
}
file.close();
cout << " UNSORTED DATA (500 elements) \n";
cout<< " ------------------------------------------------"<<endl;
print(input1,499);
//Quick sort Pivot: first element
cout << " SORTED DATA (500 elements) Pivot: First Element: \n";
cout<< " ------------------------------------------------"<<endl;
quicksort1(input1, 0, 499);
cout<<endl;
print(input1,499);
cout << " Number of comparison: \n";
cout << " Number of moves required: \n";
cout<<endl;
break;
}
else{
ifstream file2;
file2.open("500.txt");
if(!file2) {
cout<<" Error opening file. " << endl;
}
int input2[500];
for(int i = 0; i < 500; i++)
{ file2>>input2[i];
}
file2.close();
cout << " UNSORTED DATA (500 elements) \n";
cout<< " ------------------------------------------------"<<endl;
print(input2,499);
//Quick sort Pivot: random element
cout << " SORTED DATA (100 elements) Pivot: Random Element: \n";
cout << " ------------------------------------------------"<<endl;
print(input2,499);
quicksort2(input2, 0, 499);
cout<<endl;
print(input2,499);
cout << " Number of comparison: \n";
cout << " Number of moves required: \n";
cin.get();
//if windows suddenly close
break;
}
break;
case 3 :
break;
case 4 :
break;
}
我建议您先采用 DRY 方法,首先考虑 2 个变量:
1- 输入数
2- 枢轴选择
cout<<endl<<endl;
cout << "\n QUICK SORT(RANDOM NUMBER)\n";
cout << " ---------------------------------\n";
cout << "\n 1.100 elements\n 2.500 elements\n 3.1000 elements\n 4.Quit\n";
cout<< " ---------------------------------"<<endl;
cout<<" Enter your choice : ";
cin>>ch;
cout<<endl;
cout << "\n 3.Pivot : First Element \n 4.Pivot: Random element\n";
cout<< " ---------------------------------"<<endl;
cout<<" Enter your choice : ";
cin>>ch2;
cout<<endl;
string inputSize;
switch(ch)
{
case 1: inputSize="100";
...
}
string pivotSelection;
switch(ch2)
{
case 1: pivotSelection="FirstElement";
...
}
然后重写您的代码块以使用这些变量而不是对所有内容进行硬编码。
例如,您可以 file.open( inputSize + ".txt");
file.open("500.txt");
但是,使用此方法时,您必须注意,在 C++ 中,您不能创建可变大小的堆栈分配数组。例如,您需要 int * inputs = malloc(sizeof(int) * sizeOfInput);
int inputs[sizeOfInput];