【插入排序】直接,折半,二路,希尔

插入排序 给出一下四种方法:

直接插入排序,折半插入排序,二路插入排序,希尔插入排序

代码实现:

#include<iostream>
using namespace std;
#define size 21

typedef int Sqlist[size];

void SInsertSort(Sqlist &L, int n)  //直接插入
{
	cout << "直接插入排序" << endl;

	for (int i = 2; i < n; ++i)
	{
		if (L[i] < L[i - 1])		//判断i与i-1位置的大小
		{
			L[0] = L[i];			//将i位置赋值给哨兵位

			int j;
			for (j = i - 1; L[0] < L[j]; --j)
			{
				L[j + 1] = L[j];		//循环后移
			}
			L[j + 1] = L[0];
		}
	}
}

void BInsertSort(Sqlist &L, int n)
{
	cout << "折半插入排序" << endl;
	for (int i = 2; i < n; ++i)
	{
		L[0] = L[i];

		int low = 1;
		int high = i - 1;
		int mid = 0;

		while (low <= high)
		{
			mid = (low + high) / 2;

			if (L[0] < L[mid])
			{
				high = mid - 1;
			}
			else
			{
				low = mid + 1;
			}
		}

		for (int j = i - 1; j >= high + 1; --j)
		{
			L[j + 1] = L[j];				//循环后移
		}
		L[high + 1] = L[0];
	}
}

void TWInsertSort(Sqlist &L, int n)			//2—路插入排序
{
	cout << "二路插入排序" << endl;

	Sqlist T;
	T[0] = L[0];
	int first;
	int last;
	first = last = 0;
	for (int i = 1; i < n; ++i)
	{
		if (L[i] < T[first])
		{
			first = (first - 1 + n) % n;
			T[first] = L[i];
		}
		else if (L[i] > T[last])
		{
			last++;
			T[last] = L[i];
		}
		else
		{
			last++;
			T[last] = T[last - 1];

			int j = last - 1;
			for (; L[i] < T[(j - 1 + n) % n]; j = (j - 1 + n) % n)
			{
				T[j] = T[(j - 1 + n) % n];
			}
			T[j] = L[i];
		}
	}
	for (int i = 0; i < n; ++i)
	{
		L[i] = T[first];
		first = (first + 1) % n;
	}
}

void ShellInsert(Sqlist &L, int n, int dk)//希尔插入
{
	int t;
	for (int i = dk + 1; i <= n; ++i)         //按增量变化
	{
		if (L[i] < L[i - dk])              //比较大小
		{

			t = L[i];
			int j = i - dk;
			for (; j>0 && t<L[j]; j -= dk)
			{
				L[j + dk] = L[j];
			}
			L[j + dk] = t;             //赋值
		}
	}
}

void ShellSort(Sqlist &L, int n, int dlta[], int t)//希尔排序
{
	for (int k = 0; k < t; ++k)                    //按照增量数组值重复插入排序
	{
		ShellInsert(L, n, dlta[k]);
	}
}

void main()
{
	Sqlist sq = { 0, 49, 38, 65, 97, 76, 13, 27, 49 };//0为哨兵位

	cout << "原数组为:" << endl;
	for (int i = 1; i < 9; ++i)
	{
		cout << sq[i]<<' ';
	}
	cout << endl;

	SInsertSort(sq,9);

	for (int i = 1; i < 9; ++i)
	{
		cout << sq[i] << ' ';
	}
	cout << endl;

	BInsertSort(sq, 9);

	for (int i = 1; i < 9; ++i)
	{
		cout << sq[i] << ' ';
	}
	cout << endl;

	Sqlist Sq = { 49, 38, 65, 97, 76, 13, 27, 49 };

	cout << "原数组为:" << endl;
	for (int i = 0; i < 8; ++i)
	{
		cout << Sq[i]<<' ';
	}
	cout << endl;

	TWInsertSort(Sq, 8);

	for (int i = 0; i < 8; ++i)
	{
		cout << Sq[i] << ' ';
	}
	cout << endl;

	Sqlist sQ = { 0, 49, 38, 65, 97, 76, 13, 27, 49 };//哨兵位
	cout << "原数组为:"<<endl;
	for (int i = 1; i <= 8; ++i)            //打印sQ
	{
		cout << sQ[i] << " ";
	}
	cout << endl;

	int dlta[] = { 5, 3, 1 };             //增量数组  

	ShellSort(sQ, 8, dlta, sizeof(dlta) / sizeof(int));//希尔排序

	cout << "希尔排序:" << endl;
	for (int j = 1; j <= 8; ++j)
	{
		cout << sQ[j] << " ";
	}
	cout << endl;
}

输出结果:

时间: 2024-08-29 16:30:32

【插入排序】直接,折半,二路,希尔的相关文章

算法学习之排序算法:插入排序(直接插入排序、折半插入排序、2-路插入排序)

引言: 插入排序作为最简单易于理解的排序算法,基本实现比较简单.本文详细介绍直接插入排序,并给出实现,简单的介绍折半插入排序,并给出2-路插入排序和表插入排序两种插入排序,但并未给出具体实现. 一.直接插入排序 直接插入排序的基本操作是将一个记录插入到已排好序的有序表中,从而得到一个新的.记录数增1的有序表. 算法描述: 步骤1.将待排序的一组记录中的第1个记录拿出来作为一组有序的记录(当然此时该组记录仅有1个记录). 步骤2.依次将待排序的一组记录中的记录拿出来插入到前面已排好序的记录中. 步

直接插入排序与折半插入排序——Java实现

1.直接插入排序 1)原理说明:直接插入排序是一种最简单的插入排序方法,它的基本思想是,仅有一个元素的序列总是有序的,因此,对n 个记录的序列,可从第二个元素开始直到第n 个元素,逐个向有序序列中执行插入操作,从而得到n 个元素按关键字有序的序列. 2)代码实现如下: package com.test.sort; public class InsertSort { private static void sort(int[] data) { for (int i = 1; i < data.le

直接插入排序、折半插入排序、Shell排序、冒泡排序,选择排序

一.直接插入排序 稳定,时间复杂度:最好O(n).最差O(n^2).平均O(n^2).空间复杂度O(1) void InsertSort(int L[], int n) { int i, j,key; for (i = 1; i<n; i++) if(L[i] < L[i-1])//须要将L[i]插入到有序表L[0...i-1] { key = L[i]; for(j = i-1; j >= 0 && key < L[j]; j--)//后移 L[j+1] = L[

插入排序(直接插入排序、折半插入排序)

一.直接插入排序 package algorithm.sort.compare.insert; import java.util.Arrays; public class DirectInsertSort { public static void main(String[] args) { int[] arrayA = new int[] {11, 213, 134, 65, 77, 78, 23, 43}; directInsertSort (arrayA); System.out.print

java 排序算法 折半 堆 希尔 快速 整理

试题1:折半查找 折半查找是在有序表中,把待查找数据值与查找范围的中间元素值进行比较,会有三种情况出现: 1)待查找数据值与中间元素值正好相等,则放回中间元素值的索引. 2)待查找数据值比中间元素值小,则以整个查找范围的前半部分作为新的查找范围,执行1),直到找到相等的值. 3)待查找数据值比中间元素值大,则以整个查找范围的后半部分作为新的查找范围,执行1),直到找到相等的值 4)如果最后找不到相等的值,则返回不存储数据的备用单位0. 给你的问题是,标准输入一升序排列有序整数表,使用折半查找方法

内部排序(3)——插入排序之折半插入排序

因为插入排序的基本思想是在一个有序序列中插入一个新的记录,则能够利用"折半查找"查询插入位置,由此得到的插入排序算法为"折半插入排序".算法例如以下: void BInsertSort () { // 对顺序表L作折半插入排序 for ( i=2; i<length; ++i ) { <span style="white-space:pre"> </span>r[0] = r[i]; // 将r[i]暂存到r[0]

数据结构——排序——直接插入排序和折半插入排序算法

直接插入排序(Insertion Sort)的基本思想是: 每次将一个待排序的记录,按其关键字大小插入到前面已经排好序的子序列中的适当位置,直到全部记录插入完成为止. 设数组为a[0…n-1]: 1. 初始时,a[0]自成1个有序区,无序区为a[1..n-1].令i=1 2. 将a[i]并入当前的有序区a[0…i-1]中形成a[0…i]的有序区间. 3. i++并重复第二步直到i==n-1.排序完成. #include<stdio.h> #include<stdbool.h> vo

常见排序集合(冒泡排序,选择排序,直接插入排序,二分插入排序,快速排序,希尔排序,归并排序)

一下是一些常见的排序算法: 交换元素(后面算法都有用到): // 交换元素 private static void swap(int[] a, int i, int j) { int temp; temp = a[i]; a[i] = a[j]; a[j] = temp; } 冒泡排序(有优化): // 冒泡排序(优化①,②,③,④) private static void bubbleSort(int[] a) { boolean flag = false;// ①表示整个序列是无序的 for

插入排序——2折半插入排序实现

折半插入与直接插入的不同在于,搜索要插入的位置的时候,使用的是折半搜索(二分搜索).这种查找方式理论上比顺序查找的效率要高. 其代码实现如下: public IList<int> InsertionSort(int[] ary) { for (int i = 1; i < ary.Length; i++) { int low = 0; int high = i - 1; var key = ary[i]; //不断的折半 while (low <= high)//注意包含等号 {

排序之插入排序:直接插入和希尔排序

一.插入排序 1.思想:原理类似抓扑克牌,在有序表中进行插入和查找,插入合适的位置时,之后的元素需要往后移动 2.时间复杂度: 最好:O(N),正序情况,只有比较时间,无移动时间 最坏:O(N2),逆序情况 平均:O(N2) 3.辅助空间:O(1) 4.稳定性:稳定 5.适用场合:适用于初始序列基本有序的情况,或者n小的时候,插入排序效率高 1 public static void insertSort(int[] a) { 2 int target,i,j; 3 for(j = 1;j<a.l