当前位置:网站首页>Insert sort directly
Insert sort directly
2022-06-30 02:17:00 【Life needs depth】
This chapter introduces the direct insertion sorting in the sorting algorithm . The content includes :
1. Introduction to direct insertion sorting
2. Insert the sorting text description directly
3. The time complexity and stability of direct insertion sorting
4. Direct insertion sort implementation
4.1 Direct insert sort C Realization
4.2 Direct insert sort C++ Realization
4.3 Direct insert sort Java Realization
Reprint please indicate the source : Direct insert sort - If the sky doesn't die - Blog Garden
More : Data structure and algorithm series Catalog
Introduction to direct insertion sorting
Direct insert sort (Straight Insertion Sort) The basic idea of : hold n The elements to be sorted look like an ordered table and an unordered table . At the beginning, the ordered table contains only 1 Elements , The unordered table contains n-1 Elements , The first element is taken out of the unordered table every time during sorting , Insert it in place in an ordered table , Make it a new ordered table , repeat n-1 The sorting process can be completed .
Insert the sorting text description directly
Insert sort code directly
/*
* Direct insert sort
*
* Parameter description :
* a -- Array to sort
* n -- Length of array
*/
void insert_sort(int a[], int n)
{
int i, j, k;
for (i = 1; i < n; i++)
{
// by a[i] In front of a[0...i-1] Find a suitable position in the ordered interval
for (j = i - 1; j >= 0; j--)
if (a[j] < a[i])
break;
// If you find a suitable position
if (j != i - 1)
{
// Will compare a[i] Big data moves backward
int temp = a[i];
for (k = i - 1; k > j; k--)
a[k + 1] = a[k];
// take a[i] Put it in the right place
a[k + 1] = temp;
}
}
}Next, select an intermediate process of direct insertion sorting to explain it . hypothesis {20,30,40,10,60,50} In front of 3 The number has been arranged , It's ordered ; And then to 10 Arrange . The schematic diagram is as follows :

In the graph, the sequence is divided into ordered region and disordered region . There are only two things we need to do :(1) Take out the... In the disordered area 1 Number , And find its corresponding position in the ordered region .(2) Insert the data of the unordered area into the ordered area ; If necessary , Then shift the relevant data in the ordered area .
The time complexity and stability of direct insertion sorting
Direct insertion sort time complexity
The time complexity of direct insertion sorting is O(N2).
Let's assume that the sequence being sorted has N Number . The time complexity of traversing a trip is O(N), How many times does it take to traverse ?N-1! therefore , The time complexity of direct insertion sorting is O(N2).
Direct insert sort stability
Direct insertion sorting is a stable algorithm , It satisfies the definition of stable algorithm .
Algorithm stability -- Let's assume that there exists in a sequence a[i]=a[j], If before sorting ,a[i] stay a[j] front ; And after sorting ,a[i] Still in a[j] front . Then the sorting algorithm is stable !
Direct insertion sort implementation
Direct insert sort C Realization
Implementation code (insert_sort.c)
/**
* Direct insert sort :C Language
*
* @author skywang
* @date 2014/03/11
*/
#include <stdio.h>
// The length of the array
#define LENGTH(array) ( (sizeof(array)) / (sizeof(array[0])) )
/*
* Direct insert sort
*
* Parameter description :
* a -- Array to sort
* n -- Length of array
*/
void insert_sort(int a[], int n)
{
int i, j, k;
for (i = 1; i < n; i++)
{
// by a[i] In front of a[0...i-1] Find a suitable position in the ordered interval
for (j = i - 1; j >= 0; j--)
if (a[j] < a[i])
break;
// If you find a suitable position
if (j != i - 1)
{
// Will compare a[i] Big data moves backward
int temp = a[i];
for (k = i - 1; k > j; k--)
a[k + 1] = a[k];
// take a[i] Put it in the right place
a[k + 1] = temp;
}
}
}
void main()
{
int i;
int a[] = {20,40,30,10,60,50};
int ilen = LENGTH(a);
printf("before sort:");
for (i=0; i<ilen; i++)
printf("%d ", a[i]);
printf("\n");
insert_sort(a, ilen);
printf("after sort:");
for (i=0; i<ilen; i++)
printf("%d ", a[i]);
printf("\n");
} Direct insert sort C++ Realization
Implementation code (InsertSort.cpp)
/**
* Direct insert sort :C++
*
* @author skywang
* @date 2014/03/11
*/
#include <iostream>
using namespace std;
/*
* Direct insert sort
*
* Parameter description :
* a -- Array to sort
* n -- Length of array
*/
void insertSort(int* a, int n)
{
int i, j, k;
for (i = 1; i < n; i++)
{
// by a[i] In front of a[0...i-1] Find a suitable position in the ordered interval
for (j = i - 1; j >= 0; j--)
if (a[j] < a[i])
break;
// If you find a suitable position
if (j != i - 1)
{
// Will compare a[i] Big data moves backward
int temp = a[i];
for (k = i - 1; k > j; k--)
a[k + 1] = a[k];
// take a[i] Put it in the right place
a[k + 1] = temp;
}
}
}
int main()
{
int i;
int a[] = {20,40,30,10,60,50};
int ilen = (sizeof(a)) / (sizeof(a[0]));
cout << "before sort:";
for (i=0; i<ilen; i++)
cout << a[i] << " ";
cout << endl;
insertSort(a, ilen);
cout << "after sort:";
for (i=0; i<ilen; i++)
cout << a[i] << " ";
cout << endl;
return 0;
} Direct insert sort Java Realization
Implementation code (InsertSort.java)
/**
* Direct insert sort :Java
*
* @author skywang
* @date 2014/03/11
*/
public class InsertSort {
/*
* Direct insert sort
*
* Parameter description :
* a -- Array to sort
* n -- Length of array
*/
public static void insertSort(int[] a, int n) {
int i, j, k;
for (i = 1; i < n; i++) {
// by a[i] In front of a[0...i-1] Find a suitable position in the ordered interval
for (j = i - 1; j >= 0; j--)
if (a[j] < a[i])
break;
// If you find a suitable position
if (j != i - 1) {
// Will compare a[i] Big data moves backward
int temp = a[i];
for (k = i - 1; k > j; k--)
a[k + 1] = a[k];
// take a[i] Put it in the right place
a[k + 1] = temp;
}
}
}
public static void main(String[] args) {
int i;
int[] a = {20,40,30,10,60,50};
System.out.printf("before sort:");
for (i=0; i<a.length; i++)
System.out.printf("%d ", a[i]);
System.out.printf("\n");
insertSort(a, a.length);
System.out.printf("after sort:");
for (i=0; i<a.length; i++)
System.out.printf("%d ", a[i]);
System.out.printf("\n");
}
}above 3 The principle and output of the two implementations are the same . Here is their output :
before sort:20 40 30 10 60 50 after sort:10 20 30 40 50 60
边栏推荐
- DDoS surge in mobile and data centers
- JS advanced -h5 new features
- AutoJS代碼能加密嗎?YES,AutoJS加密技巧展示
- [MySQL 04] use MySQL workbench 8.0 CE to back up and restore MySQL databases in Linux
- Implementation of a simple camera based on pyqt5
- Share the source code of the website of graduation student record
- Jenkins continuous integration environment build 8 (configure mailbox server to send build results)
- [naturallanguageprocessing] [multimodality] ofa: unified architecture, tasks and modes through a simple sequence to sequence learning framework
- Can autojs code be encrypted? Yes, display of autojs encryption skills
- Recheck on February 15, 2022
猜你喜欢
随机推荐
DMX的配置
006_ radio
DHU programming exercise
Weekly recommended short video: why is the theory correct but can not get the expected results?
[Galaxy Kirin V10] [desktop] Firefox browser settings home page does not take effect
007_ checkbox
7 — filter
How do PMP candidates respond to the new exam outline? Look!
Jenkins continuous integration environment construction VII (Jenkins parametric construction)
PMP考生如何应对新考纲?看过来!
Local page floating animation is realized with the help of scroll wheel
After the blueprint node of ue5 is copied to UE4, all connections and attribute values are lost
DDoS attacks - are we really at war?
Using face_ Recognition library reports an error reason: cudnn_ STATUS_ NOT_ SUPPORTED
【MySQL 04】使用MySQL Workbench 8.0 CE 备份及恢复Linux中的MySQL数据库
If mybaits cannot query the data, it can query how to change it in the database
Encapsulate a complete version of the uniapp image and video upload component, which can be used immediately, switch between images and videos, customize the upload button style, delete the button sty
DHU programming exercise
The odoo15 page jumps directly to the editing status
[MySQL 04] sauvegarde et restauration de la base de données MySQL sous Linux en utilisant MySQL Workbench 8.0 ce









