1.1插入排序-简单(直接)插入排序

本文介绍了一种稳定的排序算法——简单插入排序,详细解释了其工作原理,并提供了完整的C语言实现代码。该算法的时间复杂度为O(n^2),适用于小规模数据集的排序。

1.1插入排序-简单(直接)插入排序

/* Straight Insertion Sort*/

一.评估

稳定的排序算法

时间复杂度O(n^2)

移动次数 最小值n-1,最大值(n+2)(n-1)/2,平均值(n^2)/4。

二.思想

逐一插入,由尾向前逐一比较。

r[0]作用:哨兵(监视哨)、暂存器。

 

void InsertSort(int *r)
{
	int i,j;

	for(i = 2;i <= N;i ++)
	{
		r[0] = r[i];//r[0]为哨兵、暂存器

		for(j = i-1; r[0] < r[j]; j --)
		{
			r[j+1] = r[j];
		}

		r[j+1] = r[0];
	}
}


三.示例

 

 

/* Straight Insertion Sort*/
//Copyright @wangwei14309 All rights reservd
# include <stdio.h>
void creat(int *r);
void InsertSort(int *r);
void print(int *r);
int N;
int main()
{
	int a[50];
	
	creat(a);

	InsertSort(a);

	print(a);

       return 0;
}

void creat(int *r)
{
	int i;
	printf("sort how many numbers :");
	scanf("%d",&N);
	printf("input %d numbers :",N);
	for(i = 1;i <= N;i ++)
	   {
		scanf("%d",&r[i]);
	   }
}

void InsertSort(int *r)
{
	int i,j;

	for(i = 2;i <= N;i ++)
	{
		r[0] = r[i];//r[0]为哨兵、暂存器

		for(j = i-1; r[0] < r[j]; j --)
		{
			r[j+1] = r[j];
		}

		r[j+1] = r[0];
	}
}

void print(int *r)
{
	int i;

	printf("the result is :");
	for(i = 1; i <= N; i ++)
    {
		printf("%d ",r[i]);
    }
	printf("\n");

}

 

 

 

 

 

搜索没有结果 Building prefix dict from the default dictionary ... 2025-09-21 14:39:40,894 - DEBUG - Building prefix dict from the default dictionary ... Loading model from cache C:\Users\86178\AppData\Local\Temp\jieba.cache 2025-09-21 14:39:40,896 - DEBUG - Loading model from cache C:\Users\86178\AppData\Local\Temp\jieba.cache Loading model cost 0.489 seconds. 2025-09-21 14:39:41,384 - DEBUG - Loading model cost 0.489 seconds. Prefix dict has been built successfully. 2025-09-21 14:39:41,385 - DEBUG - Prefix dict has been built successfully. 2025-09-21 14:39:41,395 - INFO - 数据库表结构创建/更新完成 * Serving Flask app 'search_engine' * Debug mode: off 2025-09-21 14:39:41,448 - INFO - WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on all addresses (0.0.0.0) * Running on http://127.0.0.1:5000 * Running on http://192.168.43.248:5000 2025-09-21 14:39:41,448 - INFO - Press CTRL+C to quit 2025-09-21 14:39:58,620 - INFO - 127.0.0.1 - - [21/Sep/2025 14:39:58] "GET /search?q=是的 HTTP/1.1" 200 - 2025-09-21 14:40:10,978 - INFO - 127.0.0.1 - - [21/Sep/2025 14:40:10] "GET /search?q=是的 HTTP/1.1" 200 - 2025-09-21 14:40:19,513 - INFO - 127.0.0.1 - - [21/Sep/2025 14:40:19] "GET / HTTP/1.1" 200 - 2025-09-21 14:40:24,272 - INFO - 127.0.0.1 - - [21/Sep/2025 14:40:24] "GET /search?q=是的 HTTP/1.1" 200 - 2025-09-21 14:40:37,786 - INFO - 127.0.0.1 - - [21/Sep/2025 14:40:37] "GET /search?q=和的 HTTP/1.1" 200 - 2025-09-21 14:40:41,440 - INFO - 127.0.0.1 - - [21/Sep/2025 14:40:41] "GET /search?q= HTTP/1.1" 200 -
最新发布
09-22
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值