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个元素:
加载数据>>打印未排序>>快速排序>>打印排序。这是用于枢轴第一个元素。
然后对于枢轴随机元素,我需要再次加载相同的100元素数据,然后再次执行相同操作,加载数据>>打印未排序>>快速排序>>打印排序。
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 elementsn 2.500 elementsn 3.1000 elementsn 4.Quitn";
cout<< " ---------------------------------"<<endl;
cout<<" Enter your choice : ";
cin>>ch;
cout<<endl;
switch(ch)
{
case 1 : cout << "n 3.Pivot : First Element n 4.Pivot: Random elementn";
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 elementn";
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方法,首先引入两个变量:
1-输入的数量
2-枢轴选择
cout<<endl<<endl;
cout << "n QUICK SORT(RANDOM NUMBER)n";
cout << " ---------------------------------n";
cout << "n 1.100 elementsn 2.500 elementsn 3.1000 elementsn 4.Quitn";
cout<< " ---------------------------------"<<endl;
cout<<" Enter your choice : ";
cin>>ch;
cout<<endl;
cout << "n 3.Pivot : First Element n 4.Pivot: Random elementn";
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];
相关文章:
- 如果没有malloc,链表实现将失败
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 代码在main()中运行,但在函数中出现错误
- 如何在c++中实现处理器调度模拟器
- 如何在c++中使用引用实现类似python的行为
- 实现无开销push_back的最佳方法是什么
- 使用简单类型列表实现的指数编译时间.为什么
- 如何在BST的这个简单递归实现中消除警告
- 实现一个在集合上迭代的模板函数
- 在int main()[c++]中实现一个递归函数
- 有人能告诉我这是如何在main()中实现的吗?(关于无效指针的问题)
- 我有一个构造函数,但我需要将输出返回到 main.cpp.我无法使用获取字符串功能!!任何想法如何实现这一目标?
- 测试环形缓冲区实现的"cache not flushed to main memory"
- C++编译器在 main 之后实现动态初始化
- C++ 从实现文件中的 main 调用函数
- 如何将.h文件中的代码实现到main.cpp文件中
- 如何在main.cpp文件中实现文件
- Quick Sort c++,代码在int main()中实现
- 实现自定义Main函数
- main()的链接是由实现定义的,这意味着什么