Insertion sort and shell sort Detailed

Insertion Sort

Insertion sort (Insertion sort) is a straightforward and stable sorting algorithm. There has ordered a sequence of data required in this row of good data has been inserted into the sequences of a number, but still requires the insertion of this sequence data ordered, this time is necessary to use a new sorting method - Insertion Sort method, the basic operation is the insertion sort data into an already sorted ordered data to obtain a new, plus a number of sequenced data , sorting algorithm is suitable for small amounts of data, the time complexity is O (n ^ 2). Stable sorting method.

(Excerpt from Baidu Encyclopedia, boldface type defined in Bowen has seen many, but basically no embodied in the code. Blogger considered too literally the probability of relatively large, so to Wikipedia's definition below shall prevail)

Wikipedia:

Insertion sort works by constructing an ordered sequence, for unsorted data, scanning in the sorted sequence from back to front, and find the corresponding insertion positions. Insertion sort on the implementation, usually in-place sort (that is just the sort of extra space used), thus scanning forward from the process, the need to repeatedly ordered elements gradually move back position, is the latest element provide insertion space

Algorithm is described as follows:

1. Starting from the first element, which can be considered to have been sorted
2. Remove the next element, the forward scan from the sequence of elements has been ordered in
3. If the element (sorted) is greater than the new element, the element to the next position
4. repeat step 3 until you find the position of the sorted elements less than or equal to the new element
5. the element inserted into the new position
6. repeat steps 2 to 5

As shown below:
Here Insert Picture Description

Shell sort

Hill sorting (Shellsort), also known as incremental descending sorting algorithms, insertion sort is a more efficient improved version. Hill sorting non-stationary sorting algorithm.
Hill sorting improved method is proposed based on the following two points insertion sort properties:

1. When data insertion sort operation almost sorted, high efficiency, i.e., efficiency of the linear ordering can be achieved
2. However, insertion sort is generally inefficient because the data insertion sort can only move one at a time place

Working principle:
First, the data elements in accordance with the increment i.e. gap into several groups (Packet logic), then each team for each insertion sort, this time, insertion sort acts relatively small amount of data (for each group) inserted more efficient. Then reduce the value gap continues to be sorted. Until gap = 1.
Here Insert Picture Description

First, select an incremental gap = 10/2, continue to be reduced incremental manner gap = gap / 2 of

The initial increment of gap = 10/2 = 5, the entire array is divided into 5 groups divided by color

【 8 , 3 】,【 9 , 5 】,【 1 , 4 】,【 7 , 6 】,【 2 , 0 】

These five separate sets were used

The section talking about insertion sort

The results can be found in this relatively small group of five elements are transferred to the front

Incremental narrow gap = 5/2 = 2, the entire array is divided into 2 groups

【 3 , 1 , 0 , 9 , 7 】,【 5 , 6 , 8 , 4 , 2 】

These 2 groups were used on separate sections spoken insertion sort order at this time the entire array is

Significant increment further reduced gap = 2/2 = 1, the entire array is divided into groups 1

【 0, 2 , 1 , 4 , 3 , 5 , 7 , 6 , 9 , 0 】

In this case, only the number of columns of the above simple fine adjustment, does not require extensive movement operation to complete

To sort the whole array of
graphic citations
link: https: //www.jianshu.com/p/40dcc3b83ddc

Algorithm Analysis:
time complexity Hill sorting time is O (), Hill sort time complexity lower bound is n * log2n. Hill sort quick sort algorithm is not fast O (n (logn)), and therefore good performance of medium-sized scale, data sorting very large scale is not optimal. But more than O () algorithm is much faster complexity. And very easy to achieve Hill sorting algorithm code is short and simple. In addition, Hill algorithm in the worst case and the average case efficiency is not much difference, at the same time efficiency of the implementation of quick sort in the worst case scenario will be very poor. Experts advocate that almost any sort of work can be sorted by Hill at the beginning, if it is not fast enough proof, then changed so quickly sort more advanced sorting algorithms. In actual use, in essence, Hill sorting algorithm is a direct improved insertion sort algorithm, reducing the number of its replication, much faster. The reason is that, when the value of n is large items to be moved to sort each trip number is small, but long distance data items. When the n value decreases each pass requires moving data increase, time has been closer to the final position after the sort them. It is the combination of these two cases before the Hill sorting efficiency is much higher than the insertion sort.

vs compiled by
head.h

#ifndef HEAD_H
#define HEAD_H
#include<iostream>
#include<time.h>
#include <iomanip>

using namespace std;
#define Maxsize 30

void Array_generate(int a[], int len);
void Insertion_Sort(int a[], int len);
void Shell_Sort(int a[], int len);
void Print(int a[], int len);

operation.cpp

#include"head.h"
void Array_generate(int a[], int len)
{
	srand(time(NULL));
	for (int i = 0;i < len;i++)
	{
		a[i] = rand() % 100;
	}
	for (int j = 0;j < len;j++)
	{
		cout << setw(3) << a[j];
	}
	cout << endl;
}
void Insertion_Sort(int a[], int len)
{
	int i, j, temp;                //前插法
	for (i = 1; i < len; i++) {   //(i=1)将序列的第一个数据看成是一个有序的子序列,然后从第二
								 //个数据逐个向该有序的子序列进行有序的插入,直至整个序列有序
		temp = a[i];
		for (j = i; j > 0; j--) {          // 从后向前比较
			if (a[j - 1] > temp)          //如果a[j]前面的数比它大
				a[j] = a[j - 1];          // 将大数向后移动
			else      break;              //插入完成跳出循环
		}
		a[j] = temp;  //完成插入操作
	}
}
void Shell_Sort(int a[], int len)
{

	for (int gap = len / 2;gap > 0;gap /= 2)//只需要在外层加一层控制增量的循环即可
	{
		Insertion_Sort(a, len);
	}
}
void Print(int a[], int len)
{
	int i = 0;
	for (i;i < len;i++)
	{
		cout << setw(3) << a[i];
	}
	cout << endl;
}

mian.cpp

#include "head.h"


int main()
{
	int  len;
	int a[Maxsize];
    cout << "输入数组元素个数" << endl;
	cin  >> len ;
	cout << endl;
	cout << "生成一个元素个数为"<<len<<"的随机数组"<<endl;
	Array_generate(a, len);
	cout << endl;
	cout << "插入排序:" << endl;
	Insertion_Sort(a, len);
	Print(a, len);
	cout << endl;
	
	cout << "输入数组元素个数" << endl;
	cin >> len;
	cout << endl;
	cout << "生成一个元素个数为" << len << "的随机数组" << endl;
	Array_generate(a, len);
	cout << endl;
	cout << "希尔排序:" << endl;
	Shell_Sort(a, len);
	Print(a, len);
}
Published 21 original articles · won praise 10 · views 2651

Guess you like

Origin blog.csdn.net/weixin_42199022/article/details/104220995