插入排序(Insertion Sort)

C++自学精简教程 目录(必读)

插入排序

每次选择未排序子数组中的第一个元素,从后往前,插入放到已排序子数组中,保持子数组有序。

打扑克牌,起牌。

输入数据

42 20 17 13 28 14 23 15

执行过程

完整代码

cpp 复制代码
#include <iostream>
#include <cassert>
#include <vector>
using namespace std;

void print_array(const char* msg, int* arr, int n)
{
	cout << msg << " ";
	for (int i = 0; i < n; i++)
	{
		cout << arr[i] << " ";
	}
	cout << endl;
}
//swap two number
void Swap(int& a, int& b)
{
	int tmp = a; a = b; b = tmp;
}

//将newValue插入到子数组arr中,arr的长度为length
void Insert(int* arr, int newValueindex, int newValue)
{
	// newValueindex=1     {arr[0]}    <== arr[1]           
	// newValueindex=2     {arr[0], arr[1]}    <== arr[2]
	// ......
	// newValueindex=n-1   {arr[0], arr[1],... arr[n-2]}    <== arr[n-1]

	// [a,   b,   c,   d]    e
	//  0              j     length   

	int j = newValueindex - 1;
	for (; j >= 0; j--)
	{
		if (arr[j] > newValue)
		{
			//move arr[j] to the next position,for newVaule
			arr[j + 1] = arr[j];
		}
		else
		{
			break;//break 发生时, j 的值可能是 0
		}
	}
	//发生了移动,j会停止在最后一个需要被移动的位置的前面一个位置
	if (j != newValueindex - 1)
	{
		arr[j+1] = newValue;
	}
}

void InsertSort(int* arr, int n)
{
	if (n <= 1)
	{
		return;
	}
	//将下标为1的元素插入到{arr[0]}中
	//将下标为2的元素插入到{arr[0], arr[1]}中
	//......
	//将下标为n-1的元素插入到{arr[0], arr[1], arr[n-2]}中
	for (int i = 1; i < n; i++) 
	{
		//将未排序序列中的第一个元素插入到已排序的序列中
		Insert(arr, i, arr[i]);//insert first element in unsorted list to the sorted list
		print_array("one trip", arr, n);
	}
}

void test(vector<int> arr)
{
    //输出原始序列
	print_array("original array:", arr.data(), arr.size());
	//执行排序,并输出排序过程
	InsertSort(arr.data(), arr.size());
	//输出排序后的列表
	print_array("after sorted:",arr.data(), arr.size());
	cout << endl;
}

int main()
{
	test({ 1 });
	test({ 1 , 2 });
	test({ 2 , 1 });
	test( { 2 , 2 });
	test({ 42, 20, 17, 13, 28, 14, 23, 15 });
	test({ 1, 8, 3, 6, 5, 4, 7, 2 , 9 });
	test( { 8, 8, 6, 6, 7, 5, 5, 7, 9 , 9});
	return 0;
}

执行结果

cpp 复制代码
original array: 1
after sorted: 1

original array: 1 2
one trip 1 2
after sorted: 1 2

original array: 2 1
one trip 1 2
after sorted: 1 2

original array: 2 2
one trip 2 2
after sorted: 2 2

original array: 42 20 17 13 28 14 23 15
one trip 20 42 17 13 28 14 23 15
one trip 17 20 42 13 28 14 23 15
one trip 13 17 20 42 28 14 23 15
one trip 13 17 20 28 42 14 23 15
one trip 13 14 17 20 28 42 23 15
one trip 13 14 17 20 23 28 42 15
one trip 13 14 15 17 20 23 28 42
after sorted: 13 14 15 17 20 23 28 42

original array: 1 8 3 6 5 4 7 2 9
one trip 1 8 3 6 5 4 7 2 9
one trip 1 3 8 6 5 4 7 2 9
one trip 1 3 6 8 5 4 7 2 9
one trip 1 3 5 6 8 4 7 2 9
one trip 1 3 4 5 6 8 7 2 9
one trip 1 3 4 5 6 7 8 2 9
one trip 1 2 3 4 5 6 7 8 9
one trip 1 2 3 4 5 6 7 8 9
after sorted: 1 2 3 4 5 6 7 8 9

original array: 8 8 6 6 7 5 5 7 9 9
one trip 8 8 6 6 7 5 5 7 9 9
one trip 6 8 8 6 7 5 5 7 9 9
one trip 6 6 8 8 7 5 5 7 9 9
one trip 6 6 7 8 8 5 5 7 9 9
one trip 5 6 6 7 8 8 5 7 9 9
one trip 5 5 6 6 7 8 8 7 9 9
one trip 5 5 6 6 7 7 8 8 9 9
one trip 5 5 6 6 7 7 8 8 9 9
one trip 5 5 6 6 7 7 8 8 9 9
after sorted: 5 5 6 6 7 7 8 8 9 9
相关推荐
晨非辰29 分钟前
#C语言——刷题攻略:牛客编程入门训练(十一):攻克 循环控制(三),轻松拿捏!
c语言·开发语言·经验分享·学习·visual studio
励志码农2 小时前
JavaWeb 30 天入门:第二十三天 —— 监听器(Listener)
java·开发语言·spring boot·学习·servlet
天高云淡ylz2 小时前
子网掩码的隐形陷阱:为何能ping通却无法HTTPS访问
开发语言·php
奔跑吧 android5 小时前
【linux kernel 常用数据结构和设计模式】【数据结构 2】【通过一个案例属性list、hlist、rbtree、xarray数据结构使用】
linux·数据结构·list·kernel·rbtree·hlist·xarray
汉克老师5 小时前
第十四届蓝桥杯青少组C++选拔赛[2023.2.12]第二部分编程题(5、机甲战士)
c++·算法·蓝桥杯·01背包·蓝桥杯c++·c++蓝桥杯
希望20175 小时前
Golang Panic & Throw & Map/Channel 并发笔记
开发语言·golang
朗迹 - 张伟5 小时前
Golang安装笔记
开发语言·笔记·golang
yzx9910135 小时前
生活在数字世界:一份人人都能看懂的网络安全生存指南
运维·开发语言·网络·人工智能·自动化
小周同学@5 小时前
谈谈对this的理解
开发语言·前端·javascript
Mr_Xuhhh6 小时前
项目需求分析(2)
c++·算法·leetcode·log4j