Я пытаюсь разработать программу на C++, Quick Sort. В 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.
Проблема мой код слишком долго из-за I не сделал это простым, и я не знаю, как это сделать. Я использую тот же метод.
Для сортировки 100 элементов:
Загрузка данных >> печать несортированный >> >> печать быстрой сортировки сортировки. Это для первого элемента .
Затем для случайного элемента поворота мне необходимо снова загрузить те же 100 данных элементов и сделать то же самое снова, Загрузить данные >> распечатать unsorted >> quicksort >> распечатать отсортировано.
Он же относится к 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