Performance Analysis of Internal Sorting Algorithm

[Problem Description]

In Chapter 10 of the textbook, a variety of sorting algorithms are introduced, and the time complexity of each algorithm is analyzed from a theoretical point of view. According to the theoretical knowledge in the book, design a sorting algorithm comparison program, try to compare the number of keyword comparisons and the number of moves (keyword exchange is regarded as 3 moves) of several internal sorting algorithms, so as to obtain the efficiency of various sorting algorithms. Intuitive feeling.

[basic requirements]

( 1) The sequence table is used as the storage structure of the table to be sorted, in which the table length (ie the number of keywords) is not less than 100, the data in the table is randomly generated, and at least 5 sets of different data are used for comparison.

( 2) Implement at least several basic sorting algorithms such as Bubble Sort , Direct Insertion Sort , Simple Selection Sort , Quick Sort , Shell Sort , and Heap Sort.

( 3) Output the comparison result. Output the sequence to be sorted before using various sorting algorithms to sort each group of data and the sorting result sequence after sorting; output the result table corresponding to 5 groups of data and the comparison of various algorithms

***Linear table structure****

typedef struct
{
	ElemType *elem; //Base address of storage space
	int length; //current length
	int listsize; //Currently allocated storage space capacity (in sizeof(ElemType))
}SqList;


【analyze】

Analyze where each sorting algorithm is moving and where data is being compared; for example, take the simplest bubble as an example:

//Bubble sort (Bubble)
void Bubble(SqList &L)
{
	compare=0,move=0;
	int i,j,t;
	for(j=0;j<L.length-1;j++)
		for(i=0;i<L.length-1-j;i++)
		{   compare++;
			if(L.elem[i]>L.elem[i+1])
			{t = L.elem [i]; L.elem [i] = L.elem [i + 1]; L.elem [i + 1] = t; move ++;}
		}
}

int compare=0,move=0; ////Record the number of comparisons and the number of moves

Two of the for loops are comparing to know if (L.elem[i]>L.elem[i+1]) is compared, the loop compares and adds one to know that the condition is satisfied, then the exchange can be performed, and the exchange is moved and one is added. . In this way, the number of comparisons and the number of moves for the sorting of this set of data are recorded.

A simple counting method is used to record the number of comparisons and the number of moves with compare and move, respectively.

Other sorting algorithms are analyzed one by one according to the textbook. . . . . . . .


Direct Insertion Sort (Insert)

void Insert(SqList &L)
{
	int i,j;
	for(i=2;i<L.length;++i)
	{
		compare++;
		if(L.elem[i]<L.elem[i-1])
		{
			L.elem [0] = L.elem [i];
			L.elem [i] = L.elem [i-1];
			for(j=i-2;L.elem[i]<L.elem[i-1];--j)
			{
				L.elem [j + 1] = L.elem [j];
				move++;  
                compare++;
			}
			L.elem [j + 1] = L.elem [0];
            move+=3;
		}
	}
}

Simple selection sort (Select)

/***Simple selection***/
//Simple selection sort (Select)
void Select(SqList &L)
{
	compare=0,move=0;
	int i=0,j=0,t=0;    
    for(i=1;i<=L.length;i++)//Select the i-th smallest record and swap it in place  
    {  
        j=SelectMin(L,i);//Select the record with the smallest key in L.elem[i....L.lengh]  
        if(i!=j)//Exchange with the i-th record  
        {move ++; t = L.elem [i]; L.elem [i] = L.elem [j]; L.elem [j] = t;} // 互换  
    }  
}


int SelectMin(SqList &L,int i)//Select the minimum  
{  
    int j=1,k=1;  
    for(j=i;j<=L.length;j++)  
    {compare++; if(L.elem[j]<=L.elem[i]) k=j;}  
    return k;  
}
/***Simple selection ends***/

Quick Sort (Quick)

/*****Quicksort****/
//Quick sort (Quick)
void Quick(SqList &L,int low,int high)
{
	int pivot=0;  
    if(low<high)//length is greater than 1  
    {  
        pivot=QsortPartion(L,low,high);// Divide L.elem[low...high] into two  
        Quick(L,low,pivot-1);//Recursively sort the low sub-table, pivot is the pivot position  
        Quick(L, pivot+1, high);//Recursively sort the high sub-table  
    }
}

void QuickSort(SqList &L)//Quick Sort  
{  
    Quick(L,1,L.length);  
}
 
int QsortPartion(SqList &L,int low,int high)//Quick sort subfunction  
{   
	
    int key=0;  
    key=L.elem[low];//Pivot record keyword  
    move++;  
    while(low<high)//Alternately scan from both ends of the table to the middle  
    {  
        compare+=2;  
        while(low<high&&L.elem[high]>=key) --high;  
        L.elem[low]=L.elem[high];//Move the record smaller than the pivot record to the low end  
        move++;  
        while(low<high&&L.elem[low]<=key) ++low;  
        L.elem[high]=L.elem[low];//Move records larger than the pivot record to the high end  
        move++;  
    }  
    L.elem[low]=key;//The pivot record is in place  
    move++;
    return low;//Return to pivot position	
}    
/****Quicksort end***/

Shell sort (Shell)

/****Hill Sort****/
//Shell sort (Shell)
void Shell(SqList &L,int dk )//Do a Hill insertion sort on the sequence table L
{  
    int j;  
    for(int i=dk+1;i<=L.length;i++)  
    {  
        compare++;  
        if(L.elem[i]<L.elem[i-dk])//L.elem[i] needs to be inserted into the ordered increment subtable  
        {  
            L.elem[0]=L.elem[i];//Temporarily stored in L.elem[0]  
            move++;  
            for( j=i-dk;j>0&&(L.elem[0]<L.elem[j]);j-=dk)  
            {  
                L.elem[j+dk]=L.elem[j];//The record is moved back to find the insertion position  
                move++;  
                compare++;  
            }  
            L.elem[j+dk]=L.elem[0];//Insert  
            move++;  
        }  
    }  
}  
  
void Shellsort(SqList &L,int dlta[],int t)//According to the incremental sequence dlta[0...t-1] Hill sort the sequence table L  
{  
    int k;  
    compare=0,move=0;  
    for(k=0;k<t;k++)  
        Shell(L,dlta[k]);//An insertion sort with an increment of dlta[k]
}  
/****Hill sort end****/

Heap Sort (Heap)

void Heap(SqList &L)//Heap sort
{
	int i=1,t;  
    compare=0,move=0;    
    for(i=L.length/2;i>0;i--)  
        HeapAdjust(L,i,L.length);  
    for(i=L.length;i>1;i--)  
    {  
        move++;  
        {t = L.elem [1]; L.elem [1] = L.elem [i]; L.elem [i] = t;}  
        HeapAdjust(L,1,i-1);  
    }
}

void HeapAdjust(SqList &L,int s,int m)//Heap build function  
{  
    int j,rc;  
    rc=L.elem[s];  
    for(j=2*s;j<=m;j*=2)//Filter down along the child node with the smaller key  
    {  
        compare+=2;  
        if(j<m&&(L.elem[j]<L.elem[j+1])) ++j; //j is the subscript of the record with the smaller key  
        if(!(rc<L.elem[j])) break;//End of heap adjustment  
        L.elem[s]=L.elem[j]; s=j;//The smaller child node value is changed to the parent node position  
        move++;  
    }  
    L.elem[s]=rc;//The position where rc should be inserted is at s  
    move++;  	
}  
  
/****Heapsort end****/

The main sorting algorithms are given, and others are designed and written by themselves. Data input and output are implemented according to the basic operations of the sequence table.

Main function:

void main()
{

	int i,j;
	int dlta[3]={5,3,1},t=3;//Hill function increment array
	SqList L1;
	printf("-----|---------------Number of comparisons-------------------||---- -------------Number of moves-------------------|\n");
	printf(" 分组|Bubble|Insert|Select|Quick |Shell | Heap ||Bubble|Insert|Select|Quick |Shell | Heap |\n");
	for(j=0;j<5;j++)
	{	
	InitList_Sq(L1);
	for(i = 1; i <=100; i++)
	{
			L1.elem[i]=rand()%100;
            ListInsert_Sq(L1,i,L1.elem[i]);
	}
	printf("\n");
    Bubble(L1);
	Insert(L1);
	Select(L1);
	QuickSort(L1);
    Shellsort(L1,dlta,t);
	Heap(L1);
	printf("  %d |  %d  |  %d  |  %d  |  %d  |  %d  |  %d  ||  %d  |  %d  |  %d  |  %d  |  %d  |  %d  |\n",
		j+1,compare1,compare2,compare3,compare4,compare5,compare6,move1,move2,move3,move4,move5,move6);
	}
}


The final popular science random number representation 

Main function rand()

(1) If you just need to generate random numbers and don't need to set the range, you just need to use rand(): rand() will return a random number, ranging from 0 to RAND_MAX. RAND_MAX is defined in stdlib.h, and its value is 2147483647.
E.g:

#include<stdio.h>
#include<stdlib.h>
void main()
{
       for(int i=0;i<10;i+)
             printf("%d/n",rand());
}

(2) If you want to randomly generate a number in a certain range, you can define a random(int number) function in the macro definition, and then call the random() function directly in main():

For example: randomly generate 10 numbers from 0 to 100:
#include<stdio.h>
#include<stdlib.h>
#define random(x) (rand()%x)

void main()
{
     for(int x=0 ;x<10;x++)
           printf("%d/n",random(100));
}

Result display: ( the format is not adjusted well, it is not beautiful, and the window width is limited )


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325728132&siteId=291194637