我嘗試開發C++程序,快速排序。在int main()
,第一菜單:快速排序C++,代碼實現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個元素:
負載數據>>打印未排序>> >>快速排序打印排序。這是用於支點 的第一個元素。
然後,對於pivot pivot元素,我需要再次加載相同的100 元素數據,並再次做同樣的事情,加載數據>> 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;
}
嘗試使用函數和一些變量,使其更清潔,生病舉個例子,等一下。 – ivan