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語言插入排序算法