Data Structure Course Set Individual Direct Insert Sorting

Posted by heepofajeep on Sun, 02 Jan 2022 19:12:36 +0100

Description of the problem:

For sequentially stored linear tables (assuming the sequential table is not empty), use vector s or arrays to insert the sorting algorithm directly and output the sorting results for each trip.

Reference function prototype: (vector version)

//Insert Sort directly

template<class ElemType>

void SimpleInsertSort( vector<ElemType> &A );

Input description:

First row: Data type tags for data elements of order table A (0:int, 1:double, 2:char, 3:string)

Second row: Data elements of table A to be sorted (separated by spaces)

Output description:

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

Otherwise:

First Line: Sort results of the first trip (separated by "," between data elements)

Line 3: Sort results of the second trip (separated by "," between data elements)

...

Line n: Final sorting result (separated by "," between data elements)

Input examples:

0
21 25 49 25 16 8

Output examples:

21,25,49,25,16,8
21,25,49,25,16,8
21,25,25,49,16,8
16,21,25,25,49,8
8,16,21,25,25,49

Ideas:

The basic idea of inserting sort directly:
a) Consider the n elements to be sorted as a n ordered table and a n unordered table, starting with a n ordered table containing only one element and a n unordered table containing n-1 elements.
b) Each time the first element is removed from an ordered table during the sorting process, its sorting code is compared with the sorting code of the ordered table elements in turn, and it is inserted into the appropriate place in the ordered table to make it a new ordered table. (Note: Sentry setup)
Where are the new elements inserted?
Linear search in the formed ordered table and insert at the appropriate location to move elements back in their original location.

Time complexity: O(n) ²) Spatial Complexity: O(1) Algorithmic Stability: Stability

Description of the direct insert sort algorithm:


1. Make the first element in the sequence the first element in the ordered sequence after sorting.
2. Compare the next element in the sequence with the last element in the ordered subsequence. If the element is smaller than the last element, look for the position in the ordered sequence where the element should be inserted and insert it in the correct position. If it is greater than the last element in the ordered subsequence, it is directly the last element in the ordered subsequence. Also use elements to be inserted as sentinels to avoid array subscripts crossing bounds when looking for insertion positions.
3. Loop step 2 until all remaining elements of the sequence are inserted into the ordered subsequence.

Solution code:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <stdlib.h>
#include <cmath>
#include <vector>
#Include <sstream> //for ostringstream, istringstream, stringstream
#include<stack>
#include<vector>
#include<queue>
using namespace std;
template<class ElemType>
void createvector( vector<ElemType> &A )
{
    A.push_back(0);//Set A[0] to store sentinels such as direct insertion after convenience
    ElemType tmp;
    string temp;
    getline(cin,temp);
    stringstream input(temp); //Input stream
    while(input>>tmp)
        A.push_back(tmp);
}
template<class ElemType>
void SimpleInsertSort( vector<ElemType> &A )
{
   int length=A.size()-1;//Returns the number of actual data in the vector container, that is, the length of the array, taking care to exclude Sentinel A[0]
   if(length==1){
    cout<<A[length]<<endl;
   }
   else{
        int m=2;//Ordered table has an initial length of 1, starting with the second element
       while(m<length+1){
            A[0]=A[m];//Set Sentinel
            if(A[m]>=A[m-1])
                m++;
            else{
                int location=m-1;
                while(A[location]>A[m])
                    location-=1;
                ElemType t=A[m]; //Record A[m], the next step will cause A[m] to be overwritten
                for(int i=m;i>location+1;i--){
                    A[i]=A[i-1];
                }
                A[location+1]=t;
                m++;
        }
         for(int i=1;i<length;i++)//0 is a Sentry, starting at the end of the first sorting
        cout<<A[i]<<",";
    cout<<A[length]<<endl;
       }
   }

}

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

Topics: data structure