c语言插入排序算法
In the last article, we discussed about the bubble sort with algorithm, flowchart and code. In this article, we are going to discuss about another basic sorting technique i.e. insertion sort.
在上一篇文章中,我们讨论了用算法,流程图和代码进行的冒泡排序 。 在本文中,我们将讨论另一种基本的排序技术,即插入排序 。
As the name suggests the sorting is done by using successive insertions of the key element selected by the sorting algorithm at its correct place. As the sorting begins the key element chosen is always the second element so that there can be at least one element at the left of the key element to be compared. After comparison the key element is swapped with the element at its left if required and the key element is changed to the element at the immediate right of the previous key element after that the key element is again compared with the elements at it left and the element is swapped with the key element after comparison if required, if the sorting is done in ascending order then the key element should always be greater than the element at its left if not, then the swapping is performed with key element and vice versa for the descending order.
顾名思义,排序是通过在正确的位置使用由排序算法选择的关键元素的连续插入来完成的。 在排序开始时,所选的关键元素始终是第二个元素,以便在要比较的关键元素的左侧至少可以有一个元素。 比较之后,如果需要,将关键元素与其左侧的元素交换,然后将关键元素更改为上一个关键元素的紧邻右侧的元素,之后再次将关键元素与其左侧的元素和该元素进行比较如果需要,则在比较后与key元素交换,如果排序以升序进行,则key元素应始终大于其左侧的元素(如果不是),则对key元素进行交换,反之亦然订购。
Let us look at the algorithm of insertion sort for a better understanding of the logic to be used:
让我们看一下插入排序算法,以更好地了解要使用的逻辑:
Insertion sort(a[],n)
for j->2 to n
do key =0 and a[i]>key
do a[i+1]
The algorithm can also be explained in the form of a flowchart as follows:
C code for Insertion sort
#include<stdio.h>
int main()
{
int a[6];
int key;
int i,j;
int temp;
printf("Enter any six elements to be sorted using insertion sort\n");
for(i=0;i<6;i++)
{
scanf("%d",&a[i]);
}
for(j=1;j<6;j++)
{
key=a[j];
i=j-1;
while((i>=0)&&(a[i]>=key))
{
temp=a[i+1];
a[i+1]=a[i];
a[i]=temp;
i=i-1;
}
a[i+1]=key;
}
printf("elements after sorting using insertion sort are \n");
for(i=0;i<6;i++)
{
printf("%d \n",a[i]);
}
return 0;
}
Output
C++ code for Insertion sort
Output
TOP Interview Coding Problems/Challenges
Run-length encoding (find/print frequency of letters in a string)
Sort an array of 0's, 1's and 2's in linear time complexity
Checking Anagrams (check whether two string is anagrams or not)
Relative sorting algorithm
Finding subarray with given sum
Find the level in a binary tree with given sum K
Check whether a Binary Tree is BST (Binary Search Tree) or not
1[0]1 Pattern Count
Capitalize first and last letter of each word in a line
Print vertical sum of a binary tree
Print Boundary Sum of a Binary Tree
Reverse a single linked list
Greedy Strategy to solve major algorithm problems
Job sequencing problem
Root to leaf Path Sum
Exit Point in a Matrix
Find length of loop in a linked list
Toppers of Class
Print All Nodes that don't have Sibling
Transform to Sum Tree
Shortest Source to Destination Path
Comments and Discussions
Ad:
Are you a blogger? Join our Blogging forum.
Insertion sort(a[],n)
for j->2 to n
do key =0 and a[i]>key
do a[i+1]
The algorithm can also be explained in the form of a flowchart as follows:
插入排序的C代码
# include < stdio.h >
int main ( )
{
int a [ 6 ] ;
int key ;
int i , j ;
int temp ;
printf ( " Enter any six elements to be sorted using insertion sort \n " ) ;
for ( i = 0 ; i < 6 ; i + + )
{
scanf ( " %d " , & a [ i ] ) ;
}
for ( j = 1 ; j < 6 ; j + + )
{
key = a [ j ] ;
i = j - 1 ;
while ( ( i > = 0 ) & & ( a [ i ] > = key ) )
{
temp = a [ i + 1 ] ;
a [ i + 1 ] = a [ i ] ;
a [ i ] = temp ;
i = i - 1 ;
}
a [ i + 1 ] = key ;
}
printf ( " elements after sorting using insertion sort are \n " ) ;
for ( i = 0 ; i < 6 ; i + + )
{
printf ( " %d \n " , a [ i ] ) ;
}
return 0 ;
}
输出量
插入排序的C ++代码
输出量
最佳面试编码问题/挑战
游程编码(字符串中字母的查找/打印频率)
以线性时间复杂度对0、1和2的数组进行排序
检查字谜(检查两个字符串是否是字谜)
相对排序算法
查找给定总和的子数组
在给定总和K的二叉树中找到级别
检查二叉树是否为BST(二叉搜索树)
1 [0] 1个样式计数
大写一行中每个单词的第一个和最后一个字母
打印二叉树的垂直和
打印二叉树的边界和
反转单个链表
解决主要算法问题的贪婪策略
工作排序问题
根到叶的路径总和
矩阵中的出口点
在链表中查找循环长度
一流的礼帽
打印所有没有兄弟的节点
转换为求和树
最短的源到目标路径
评论和讨论
广告:您是博主吗? 加入我们的Blogging论坛 。
翻译自: https://www.includehelp.com/algorithms/insertion-sort-algorithm-flowchart-and-c-cpp-code.aspx
c语言插入排序算法