Detailed Explanation of Table Insertion Sort of Direct Insertion Sort Algorithm

1. Algorithm principle

Insertion sort is a stable sort method and is a commonly used sorting algorithm. The direct insertion sorting algorithm can be implemented using a static array, or a static linked list or a singly linked list. This paper presents the static linked list implementation method of the direct insertion algorithm, that is, the table insertion sorting algorithm.
The direct insertion sorting based on the static linked list is to put the array to be sorted into the static linked list, and give the sorting result by modifying the value of the index field in the static linked list. This sorting method does not change the position of the element in the original array, but only gives its position in the sorting table in the index field. Therefore, compared with the direct insertion sorting algorithm, this algorithm does not need to move elements.

The following example demonstrates the process of using the single-table insertion sort algorithm.
Demo:
Assume a set of scattered data: { 40, 30, 50, 10, 20 }
Step 0 : Create a static linked list, element 0 does not store elements, and its "Next subscript" item indicates the subscript of the smallest element in the current static linked list
insert image description here
Step 1: Store element 40 in the position where the subscript of the static linked list is 1. The smallest element in the current static linked list is 40, and its subscript is 1, so the "Next subscript" of element 0 is assigned a value of 1, while the "Next subscript" of element 1 is assigned a value of 1. Subscript" has a value of 0, indicating that it is the last element of the current static array.
insert image description here
Step 2: Store element 30 in the position where the subscript of the static linked list is 2. The smallest element in the current static linked list is 30, so modify the "Next subscript" of element 0 to 2, and modify the "Next subscript" of element 2 to 1 .
insert image description here
Step 3: Store element 50 in the position where the subscript of the static linked list is 3. The smallest element in the current static linked list is 30, and the largest element is 50. Therefore, modify the value of "Next subscript" as shown in the table below.
insert image description here
Step 4: Store element 10 in the position where the subscript of the static linked list is 4. The smallest element in the current static linked list is 10, so modify the "Next subscript" value of element 0 to 4, and the "Next subscript" values ​​of other elements are as follows shown in the table.
insert image description here
Step 5: Store element 20 in the position of subscript 5 in the static linked list, and the "Next subscript" value of each element is shown in the table below.
insert image description here
When inserting element 20 in this step, it is the second smallest element in the current static linked list, so when modifying its subscript and the subscript of the smallest element 10 here, it needs to be processed separately, that is, it needs to record the element smaller than element 20 The position of the adjacent element, and then assign its "Next subscript" value to the second smallest subscript.

2. C program based on table insertion sort algorithm

1. Table insertion sort algorithm function

void TableInsertSort( Table arr[], int length )
{
    
    
	int i, k ,loc;
	arr[0].nextInd = 1;
	arr[1].nextInd = 0;
	for( i = 2; i <= length; i++ )
	{
    
    
		k = loc = 0;
		while( 1 )
		{
    
    
			if( arr[i].data <= arr[ arr[k].nextInd ].data )
			{
    
    
				if( k == 0 )
				{
    
    
					loc = 0;
				}
				arr[i].nextInd = arr[loc].nextInd;
				arr[loc].nextInd = i;
				break;
			}
			else 
			{
    
    
				if( arr[ arr[k].nextInd ].nextInd == 0 )
				{
    
    
					arr[ arr[k].nextInd ].nextInd = i;
					arr[i].nextInd = 0;
					break;
				}
				loc = arr[k].nextInd;//记录比当前元素小的元素下标 
				k = arr[ arr[k].nextInd ].nextInd;
			}
		}
	}
}

2. Test code (for reference only)

//直接插入排序算法之静态链表实现方法 
#include"stdio.h"
typedef struct tbl
{
    
    
	int data;
	int nextInd;
}Table;
void TableInsertSort( Table arr[], int length );
int main()
{
    
    
	int i;
	int a[] = {
    
     4, 3, 5, 1, 2 };
	Table arr[6];
	int length = 5;
	for( i = 1; i <= length; i++ )
	{
    
    
		arr[i].data    = a[i-1];
		arr[i].nextInd = 0;
	}
	arr[0].data = 0;
	TableInsertSort( arr, length );
	printf( "index  :" );
	for( i = 0; i <= length; i++ )
	{
    
    
		printf( "%5d", i );
	} 
	printf( "\n" );
	printf( "data   :" );
	for( i = 0; i <= length; i++ )
	{
    
    
		printf( "%5d", arr[i].data );
	} 
	printf( "\n" );
	printf( "nextInd:" );
	for( i = 0; i <= length; i++ )
	{
    
    
		printf( "%5d", arr[i].nextInd );
	} 
	printf( "\n" );
	return 0;
}

void TableInsertSort( Table arr[], int length )
{
    
    
	int i, k ,loc;
	arr[0].nextInd = 1;
	arr[1].nextInd = 0;
	for( i = 2; i <= length; i++ )
	{
    
    
		k = loc = 0;
		while( 1 )
		{
    
    
			if( arr[i].data <= arr[ arr[k].nextInd ].data )
			{
    
    
				if( k == 0 )
				{
    
    
					loc = 0;
				}
				arr[i].nextInd = arr[loc].nextInd;
				arr[loc].nextInd = i;
				break;
			}
			else 
			{
    
    
				if( arr[ arr[k].nextInd ].nextInd == 0 )
				{
    
    
					arr[ arr[k].nextInd ].nextInd = i;
					arr[i].nextInd = 0;
					break;
				}
				loc = arr[k].nextInd;//记录比当前元素小的元素下标 
				k = arr[ arr[k].nextInd ].nextInd;
			}
		}
	}
}

3. Running results
insert image description here

Guess you like

Origin blog.csdn.net/sunnyoldman001/article/details/127080111