Data structure - sorting

Posted by ted_chou12 on Tue, 18 Jan 2022 21:27:46 +0100

Experimental preparation:

1. Understand and master the basic concepts and methods of direct insertion sort, half insertion sort, 2-way insertion sort, bubble sort and quick sort.
2. Master the basic idea of sorting algorithm.

1, Experimental purpose

  1. Master the implementation of the basic idea of sorting algorithm.
  2. Through experiments, master the specific implementation of direct insertion sorting, half insertion sorting and 2-way insertion sorting.
  3. Master the implementation of the basic idea of exchange sorting algorithm.
  4. Master the specific implementation of bubble sorting and quick sorting through experiments.

2, Experimental requirements

[item 1 – sorting algorithm verification]

Use the sequence {40, 57, 38, 11, 13, 34, 48, 75, 6, 19, 9, 7} as the test data, run the corresponding algorithm program, observe the operation results, and deeply understand the idea and implementation method of the algorithm:

(1) Direct insertion sorting; 8-1.cpp
(2) Hill sort; 8-2.cpp
(3) Bubble sorting; 8-3.cpp
(4) Quick sort; 8-4.cpp
(5) Heap sorting; 8-6.cpp
(6) Merge and sort; 8-7.cpp
(7) Cardinality sorting; 8-8.cpp

Please create your own sorting algorithm library.

[item 2 – experience of sorting algorithm performance on large data sets]

Please use the given test data to test the running time of the sorting algorithm in the given reference algorithm. Write a main program, call each sorting function, view the results of each sorting, and give the comparison of sorting time of various sorting methods. Observe the output. The perceptual knowledge of running time of various algorithms with different complexity is obtained. This project aims to obtain perceptual knowledge of algorithms with different complexity. Due to different data distribution characteristics and computer operation status, the results can not completely replace the theoretical analysis of algorithm complexity;

According to your own situation, you can choose the following two schemes for data test:

  1. Please use the given test data to test the running time of the sorting algorithm in the given reference algorithm. Write a main program, call each sorting function, view the results of each sorting, and give the comparison of sorting time of various sorting methods.
  2. Design a function to generate a data set of at least 100000 records. Each sort method is written separately as a sub function, and then called by the main function.

code:
Insert sort directly:
main.cpp:

#include <iostream>
#include "head.h"
using namespace std;

int main()
{
    int n=0,i=0;
    double t1,t2;
    RecType R[MaxSize];
    FILE *fp1;
    fp1=fopen("numbers.txt","r");
    if(fp1==NULL)
    {
        printf("Error opening file! Please download the file and copy it to the same folder as the source program file.\n");
        exit(1);
    }
    while(fscanf(fp1,"%d",&R[n].key)!=EOF)
        n++;
    printf("Data volume: %d,Start sorting....",n);
    t1=time(0);
    InsertSort(R,n);
    t2=time(0);
    printf("Direct insert sort time %d second",(int)(t2-t1));
    printf("\n After sorting:");
    for (i=0; i<n; i++)
        printf("%d ",R[i].key);
    printf("\n");
    fclose(fp1);

    return 0;
}

Hill sort:
main.cpp:

#include <iostream>
#include "head.h"
using namespace std;

int main()
{
    int n=0,i=0;
    double t1,t2;
    RecType R[MaxSize];
    FILE *fp1;
    fp1=fopen("numbers.txt","r");
    if(fp1==NULL)
    {
        printf("Error opening file! Please download the file and copy it to the same folder as the source program file.\n");
        exit(1);
    }
    while(fscanf(fp1,"%d",&R[n].key)!=EOF)
        n++;
    printf("Data volume: %d,Start sorting....",n);
    t1=time(0);
    ShellSort(R,n);
    t2=time(0);
    printf("Hill sort time %d second",(int)(t2-t1));
    fclose(fp1);

    return 0;
}

Bubble sort:
main.cpp:

#include <iostream>
#include "head.h"
using namespace std;

int main()
{
    int n=0,i=0;
    double t1,t2;
    RecType R[MaxSize];
    FILE *fp1;
    fp1=fopen("numbers.txt","r");
    if(fp1==NULL)
    {
        printf("Error opening file! Please download the file and copy it to the same folder as the source program file.\n");
        exit(1);
    }
    while(fscanf(fp1,"%d",&R[n].key)!=EOF)
        n++;
    printf("Data volume: %d,Start sorting....",n);
    t1=time(0);
    BubbleSort1(R,n);
    t2=time(0);
    printf("Bubble sorting time %d second",(int)(t2-t1));
    fclose(fp1);

    return 0;
}

Quick sort:
main.cpp:

#include <iostream>
#include "head.h"
using namespace std;

int main()
{
    int n=0,i=0;
    double t1,t2;
    RecType R[MaxSize];
    FILE *fp1;
    fp1=fopen("numbers.txt","r");
    if(fp1==NULL)
    {
        printf("Error opening file! Please download the file and copy it to the same folder as the source program file.\n");
        exit(1);
    }
    while(fscanf(fp1,"%d",&R[n].key)!=EOF)
        n++;
    printf("Data volume: %d,Start sorting....",n);
    t1=time(0);
    QuickSort(R,0,n-1);
    t2=time(0);
    printf("Quick sort time %d second",(int)(t2-t1));
    fclose(fp1);

    return 0;
}

Heap sort:
main.cpp:

#include <iostream>
#include "head.h"
using namespace std;

int main()
{
    int n=0,i=0;
    double t1,t2;
    RecType R[MaxSize];
    FILE *fp1;
    fp1=fopen("numbers.txt","r");
    if(fp1==NULL)
    {
        printf("Error opening file! Please download the file and copy it to the same folder as the source program file.\n");
        exit(1);
    }
    while(fscanf(fp1,"%d",&R[n].key)!=EOF)
        n++;
    printf("Data volume: %d,Start sorting....",n);
    t1=time(0);
    HeapSort(R,n);
    t2=time(0);
    printf("Heap sort time %d second",(int)(t2-t1));
    fclose(fp1);

    return 0;
}

Merge sort:
main.cpp:

#include <iostream>
#include "head.h"
using namespace std;

int main()
{
    int n=0,i=0;
    double t1,t2;
    RecType R[MaxSize];
    FILE *fp1;
    fp1=fopen("numbers.txt","r");
    if(fp1==NULL)
    {
        printf("Error opening file! Please download the file and copy it to the same folder as the source program file.\n");
        exit(1);
    }
    while(fscanf(fp1,"%d",&R[n].key)!=EOF)
        n++;
    printf("Data volume: %d,Start sorting....",n);
    t1=time(0);
    MergeSort(R,n);
    t2=time(0);
    printf("Merge sort time %d second",(int)(t2-t1));
    fclose(fp1);

    return 0;
}

Operation result diagram:
Insert sort directly:

Hill sort:

Bubble sort:

Quick sort:

Heap sort:

Merge sort:

Topics: C++ Algorithm data structure