Quick Sort c++,代码在int main()中实现

Quick Sort c++, code implementation in int main()

本文关键字:main 实现 int Sort c++ 代码 Quick      更新时间:2023-10-16

我尝试开发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];