Data structure course design: quick sorting

Posted by brmcdani on Sat, 01 Jan 2022 10:16:20 +0100

Problem Description:

For the linear table stored in order (assuming that the order table is not empty), use vector or array to realize the quick sorting algorithm and output the sorting results of each trip.

Reference function prototype: (vector version)

(1) / / implementation of quick sort (shell)

template<class ElemType>

void QuickSort( vector<ElemType> &A );

(2) / / implementation of quick sort (recursion)

template<class ElemType>

void QuickSort( vector<ElemType> &A, int low, int high); 

(3) / / partition function (quick sort)

template<class ElemType>

int divide( vector<ElemType> &A, int low, int high );

Input Description:

The first row: the data type mark of the data element of sequence table A (0: int, 1: double, 2: char, 3: string)

The second row: the data elements of the order table A to be sorted (the data elements are separated by spaces)

Output Description:

If the input values in the first line are values other than 0, 1, 2 and 3, directly output "err"

Otherwise:

First row: pivot element position pivot element

Intermediate sorting results (data elements are separated by ",")

Line 2: pivot element position pivot element

Intermediate sorting results (data elements are separated by ",")

...

Line n: pivot element position pivot element

Final sorting result (data elements are separated by ",")

Note: there is a blank line after the final output result

Input example:

0
256 301 751 129 937 863 742 694 076 438

Output example:

2 256
76,129,256,751,937,863,742,694,301,438
0 76
76,129,256,751,937,863,742,694,301,438
7 751
76,129,256,438,301,694,742,751,863,937
4 438
76,129,256,301,438,694,742,751,863,937
5 694
76,129,256,301,438,694,742,751,863,937
8 863
76,129,256,301,438,694,742,751,863,937
 

Idea:

Basic idea:
Take any element (for example, the first one) from the sequence to be arranged as the center. All elements smaller than it are placed in front and all elements larger than it are placed in back to form the left and right sub tables;

——- > low pointing to the first element of the sequence and high pointing to the last element of the sequence approach the middle alternately / oscillatory, starting from high and moving forward from back. If the pointing element is larger than the selected intermediate element, then high continues to point forward. If the pointing element is smaller than the specified intermediate element, Put the element in front and point back from changing to low open. Similarly, if it is less than, it will continue to point back. If it is greater than, it will move the element to the back and start with {high. Until the last space remains, place the selected center element.
Then re select the central element for the left and right sub tables and adjust according to this rule until there is only one element left in each sub table. This is an ordered sequence. (recursive)
Advantages: because the position of more than one element can be determined in each trip and increases exponentially, it is particularly fast!
Premise: sequential storage structure
Time efficiency: O(nlog2n) - because the elements determined in each trip increase exponentially
Space efficiency: O(log2n) - because recursion uses stack (low, high and pivot in each layer)
Stability: unstable - because there is a jump exchange.

*The average sorting efficiency of quick sort is O(nlog) ² n); But in the worst case (e.g. natural order), it is still O(n ²)

Code implementation:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <stdlib.h>
#include <cmath>
#include <vector>
#Include < ssstream > / / used for ostringstream, istringstream and stringstream
#include<stack>
#include<vector>
#include<queue>
#include<list>
using namespace std;
template<class ElemType>
void createvector( vector<ElemType> &A )
{
    ElemType tmp;
    string temp;
    getline(cin,temp);
    stringstream input(temp); //Input stream
    while(input>>tmp)
        A.push_back(tmp);
}
//Partition function (one quick sort)
template<class ElemType>
int divide( vector<ElemType> &A, int low, int high )
{
    ElemType temp=A[low];   //Take the first element as the pivot. You can also use A[0] as a sentry to store it. The effect is the same
    while(low<high){
        while(low<high&&A[high]>temp) --high; //Start with high, and go high as long as you don't encounter an element smaller than temp that needs to be moved
        A[low]=A[high]; //The initial A[low] is stored in temp and is not afraid of coverage. After the A[high] is moved to the front, the original position is equivalent to a vacancy.
        while(low<high&&A[low]<temp) ++low;//Go low as long as you don't encounter an element larger than temp that needs to be moved
        A[high]=A[low];//Similarly, after performing this step, the original low position is equivalent to an empty position
    }
    A[high]=temp;//You can also use A[low], because the condition for exiting while is high=low
    cout<<low<<" "<<A[low]<<endl;
    for(int k=0;k<A.size()-1;k++)
                        cout<<A[k]<<',';
            cout<<A[A.size()-1]<<endl;
    return high;//Return to final pivot position
}
//Implementation of quick sort (recursion)
template<class ElemType>
void QuickSort( vector<ElemType> &A, int low, int high)
{

    if(low<high){
           int location=divide(A,  low,  high );
    QuickSort(A,  low,  location-1 );   //The following two are QuickSort functions, not divide functions, otherwise recursion cannot be realized!!!
    QuickSort(A,  location+1,  high );
    }
}
//Implementation of quick sort (shell)
template<class ElemType>
void QuickSort( vector<ElemType> &A )
{
    int low=0,high=A.size()-1;  //If A[0] is used as the sentry, low starts from the beginning
    if(low==high)//There is only one element
        {
            cout<<0<<" "<<A[low]<<endl;
            cout<<A[low]<<endl;
        }
        
        
    else
    QuickSort( A,  low, high);
}

int main()
{
    int kd;
    cin>>kd;
    cin.ignore();
    if(kd==0){
        vector<int> A;
        createvector( A );
        QuickSort( A );
    }
    else if(kd==1){
        vector<double>A;
        createvector( A );
        QuickSort( A );
    }
    else if(kd==2){
         vector<char>A;
         createvector( A );
           QuickSort( A );
    }
     else if(kd==3){
        vector<string >A;
        createvector( A );
        QuickSort( A );
     }
    else
        cout<<"err"<<endl;
    return 0;
}

Topics: Algorithm data structure