带哨兵的插入排序

插入排序的算法实现


自己在实现带有哨兵的插入排序算法的时候,发现有两种思路:

  • 在内存里面又一次分配一块比输入数组inputArray长度大一的temp数组,空出temp数组的第一个位置。然后把inputArray从第emp数组的第二个位置复制进去。既有temp[i+1]=inputArray[i]。i<arrayLength,再把第一个位置temp[0]赋值一个足够小的数字INT_MIN,以temp[0]为哨兵,进行插入排序。最后再把temp数组从第二个元素(temp[1])開始。又一次拷贝到从第一个元素(inputArray[0])開始的inputArray数组里面,返回。
  • 另外一种方法就是每開始一次输入。就把将要进行插入inputArray[j]的元素赋值给inputArray[0],这样就能够起到一个哨兵的作用,可是这样做须要把inputArray数组第一个元素暂存起来,否则数组的第一个元素会丢失,最后再把inputArray[0]进行插入处理...

以下是两种方案的实现:

第一种实现不是那么灵活
void insertSort(int inputArray[] ,const int arrayLength)
{
	int i,j;
	int tempArray[arrayLength+1];
	//inputArray又一次拷贝到tempArray里面
	for (i = 0; i < arrayLength; ++i)
	{
		tempArray[i+1]=inputArray[i];
	}
	tempArray[0]=INT_MIN;
	//以下開始对带有哨兵的 tempArray进行排序
	for (i = 1; i <= arrayLength; ++i)
	{
		int temp = tempArray[i];
		for (j = i; temp < tempArray[j-1] ; --j)
		{
			tempArray[j]=tempArray[j-1];
		}
		tempArray[j]=temp;
	}
	for (i = 0;i<arrayLength; ++i)
	{
		inputArray[i]=tempArray[i+1];
	}
}

另外一种实现
void insertSort(int inputArray[] , const int arrayLength)
{
	int i,j;
	int tempArray[arrayLength+1];
	//inputArray又一次拷贝到tempArray里面
	for (i = 0; i < arrayLength; ++i)
	{
		tempArray[i+1]=inputArray[i];
	}
	tempArray[0]=INT_MIN;
	//以下開始对带有哨兵的 tempArray进行排序
	for (i = 1; i <= arrayLength; ++i)
	{
		int temp = inputArray[i];
		for (j = i; temp < tempArray[j-1] ; --j)
		{
			tempArray[j]=tempArray[j-1];
		}
		tempArray[j]=temp;
	}
	for (i = 0;i<arrayLength; ++i)
	{
		inputArray[i]=tempArray[i+1];
	}
}

对两个插入排序进行实验:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
void insertSort(int inputArray[],const int arrayLength )
{
	/*...*/
}
void printArray(const int inputArray[],const int arrayLength)
{
	int i;
	for(i=0;i<arrayLength;++i)
	{
		printf("%d	",inputArray[i] );
	}
}

int main(int argc, char const *argv[])
{
	int i=0;
	srand((int)time(NULL));
	int array[10];
	for(i=0;i<sizeof(array)/4;++i)
	{
		array[i]=rand()%100;
	}
	printArray(array,sizeof(array)/4);
	insertSort(array,sizeof(array)/4);
	printArray(array,sizeof(array)/4);
	return 0;
}

实验结果:




原文地址:https://www.cnblogs.com/mengfanrong/p/5062447.html