排序_Dir_Insert(直接插入排序)

直接插入排序就是很简单的,逻辑排序,更通俗一点的就是日常生活中我们都是利用这种方法排序的。

贴出代码:

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <string>

using namespace std;

int a[10000];

int N;

void Dir_Insert () //直接插入排序 
{
	for (int i = 1; i < N; i++)
	{
		int j = i - 1;
		int t = a[i]; // 这样赋值是为了让你更能清楚的知道i层的含义,仅仅是作为被插入的对象。 
		while (t < a[j] && j != -1) // 一定注意要加上j != -1 不然的话一比较负数就会死循环的! 
		{
			a[j + 1] = a[j];
			j--; 
		}
		a[j + 1] = t;
	}
}

int main()
{
	cout << "Input the sorting number that you want:  " << endl;
	cin >> N;
	cout << "Please input the numbers: " << endl;
	for (int i = 0; i < N; i++)
	{
		cin >> a[i];
	}
	Dir_Insert();
	for (int i = 0; i < N; i++)
	{
		cout << a[i] << " ";
	}
	cout << endl;
	system ("pause");
	return 0;
}
	


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值