c語言插入排序算法_插入排序算法,流程圖和C,C ++代碼

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論壇 。


insertion sort output 2
insertion sort output 1

翻譯自: https://www.includehelp.com/algorithms/insertion-sort-algorithm-flowchart-and-c-cpp-code.aspx

c語言插入排序算法

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/542405.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/542405.shtml
英文地址,請注明出處:http://en.pswp.cn/news/542405.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

EF使用CodeFirst方式生成數據庫技巧經驗

前言 EF已經發布很久了&#xff0c;也有越來越多的人在使用EF。如果你已經能夠非常熟練的使用EF的功能&#xff0c;那么就不需要看了。本文意在將自己使用EF的方式記錄下來備忘&#xff0c;也是為了給剛剛入門的同學一些指導。看完此文&#xff0c;你應該就學會以CodeFirst的方…

java jar包示例_Java包getImplementationVersion()方法和示例

java jar包示例包類的getImplementationVersion()方法 (Package Class getImplementationVersion() method) getImplementationVersion() method is available in java.lang package. getImplementationVersion()方法在java.lang包中可用。 getImplementationVersion() method …

c語言中字母的定義,c語言字符串定義與初始化 - 且聽風吟

字符串的兩種定義方式char數組char sa[] “hello world”;char指針char *sp “hello world”;這兩種方式都產生了一個”hello world”的字符串常量,字符串常量存儲在靜態存儲區中&#xff0c;靜態存儲區中的內容在程序運行的整個過程中都存在&#xff0c;而且只存儲一份。數組…

python計算兩字符串中的位置_python – 計算兩個字符串之間距離的算法

是否有任何字符串距離算法沒有考慮到單詞的順序&#xff1f;以下算法未提供所需結果(在該示例中,所需結果應為1)&#xff1a;import jarojaro.jaro_winkler_metric(uMichael Jordan,uJordan Michael)>>>0.47import LevenshteinLevenshtein.ratio(Michael Jordan,Jorda…

php unset函數_PHP | 使用unset()函數從數組中刪除元素

php unset函數Given an array and we have to remove an element from the array. 給定一個數組&#xff0c;我們必須從數組中刪除一個元素。 unset()函數 (unset() function) To remove an element from an array, we can use a PHP library unset() function, it accepts th…

vi顯示行號

vi顯示行號 :set nu 帶行號查看&#xff0c;并不改變文件內容:set nonu 取消帶行號查看在每個用戶的主目錄下,都有一個 vi 的配置文件".vimrc"或".exrc"用戶可以編輯它,使這些設置在每次啟動 vi 時,都有效.例如,加入如下設置行:set nu 顯示行號…

對象過濾某個屬性 循環 php_37道PHP面試題(附答案)

1、什么事面向對象&#xff1f;主要特征是什么&#xff1f;面向對象是程序的一種設計方式&#xff0c;它利于提高程序的重用性&#xff0c;使程序結構更加清晰。主要特征&#xff1a;封裝、繼承、多態。2、SESSION 與 COOKIE的區別是什么&#xff0c;請從協議&#xff0c;產生的…

項響琴C語言書籍在線瀏覽,電子琴 c語言程序

實用#include unsigned char code table[]{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};unsigned char temp;unsigned char key;unsigned char i,j;unsigned char STH0;unsigned char STL0;unsigned int code tab[]{64021,64103,64260,…

Java File類boolean createNewFile()方法(帶示例)

文件類布爾型createNewFile() (File Class boolean createNewFile()) This method is available in package java.io.File.createNewFile(). 軟件包java.io.File.createNewFile()中提供了此方法。 This method is used to create a new file by using createNewFile() method a…

oracle ? SQL執行過程

1.sql執行過程1>解析&#xff08;判斷對象是否存在&#xff0c;是否有權限查詢&#xff0c;語義解析&#xff0c;檢查緩存中是否有相同的SQL等等&#xff09;2>優化&#xff08;CBO確定優化模式&#xff0c;確定訪問路徑&#xff0c;聯接順序&#xff0c;過程中通過很多綜…

vue-video-player修改src就會報錯_4、修改入口點代碼

在riscv上電時&#xff0c;會進行CPU自檢&#xff0c;然后跳轉到bootloader處執行。bootloader設置好kernel的運行環境后&#xff0c;從硬盤加載kernel到內存&#xff0c;最后再跳轉到kernel入口地址。我們采用的bootloader為OpenSBI&#xff0c;被加載到0x80000000地址&#x…

數碼管超聲波c語言黑51,51單片機開發板-超聲波測距-數碼管顯示

《51單片機開發板-超聲波測距-數碼管顯示》由會員分享&#xff0c;可在線閱讀&#xff0c;更多相關《51單片機開發板-超聲波測距-數碼管顯示(16頁珍藏版)》請在人人文庫網上搜索。1、計算機技術系項目工作報告課程名稱單片機開發板設計與制作實訓班級學號姓名項目名稱超聲波測距…

java 方法 示例_Java ArrayDeque帶有示例的removeFirstOccurrence()方法

java 方法 示例ArrayDeque類removeFirstOccurrence()方法 (ArrayDeque Class removeFirstOccurrence() method) removeFirstOccurrence() method is available in java.lang package. removeFirstOccurrence()方法在java.lang包中可用。 removeFirstOccurrence() method is use…

社交應用動態九宮格圖片的規則

這里主要以微信和QQ空間為作為研究對象&#xff0c;得到的結論如下。 QQ空間里的動態 iOS設備&#xff0c;以iPhone6為分界 iPhone6及以上分辨率的設備&#xff1a; 當寬且高同時 > 512px時&#xff0c;判斷 寬/高的比例值&#xff1a;大于 2時&#xff0c;以高度為基準&…

c語言實現鏈表結構6,用c語言實現的鏈表結構--數據結構實驗

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓#include"stdio.h"//使用new指針來將臨時變量重新初始化#include"stdio.h"typedef int ElemType;typedef struct LNode{ElemType data;struct LNode *next;}LNode,*LinkList;void InitList(LinkList &L)//…

splunk中 如何隱藏input_翻糖制作中,如何避免裂縫,如何隱藏裂縫,如何防粘?...

翻糖蛋糕 因精致的樣子和栩栩如生的各種造型深得人們的喜愛&#xff0c;它不僅滿足了人們對蛋糕口味及裝飾日益多樣化的需求&#xff0c;同時也在動手制作的過程中&#xff0c;享受到美食與生活的無窮樂趣。不過裂縫&#xff0c;不平整&#xff0c;干燥對翻糖作品來說無疑是噩夢…

Java DataInputStream readUnsignedByte()方法(帶示例)

DataInputStream類readUnsignedByte()方法 (DataInputStream Class readUnsignedByte() method) readUnsignedByte() method is available in java.io package. readUnsignedByte()方法在java.io包中可用。 readUnsignedByte() method is used to read 1 byte (i.e. 8 bit) of …

wpf中groupbox有什么用_展示設計中的標攤是什么 用的什么材料

經常聽從事展示設計的工作人員說起標攤&#xff0c;那什么是標攤呢&#xff1f;顧名思義&#xff0c;標攤就是通用標準的國際展會攤位的縮寫。但是不少人看到干巴巴的詞語還是不能理解。那么這篇文章從用途、材料等方面來詳細介紹標攤究竟是什么。 標攤的主要材質是什么一般來說…

Java BigInteger類| nextProbablePrime()方法與示例

BigInteger類nextProbablePrime()方法 (BigInteger Class nextProbablePrime() method) nextProbablePrime() method is available in java.math package. nextProbablePrime()方法在java.math包中可用。 nextProbablePrime() method is used to get the next probable prime n…

SQL 行轉列的兩種做法

if object_id(tb)is not null drop table tbGocreate table tb(姓名 varchar(10),課程 varchar(10),分數 int)insert into tb values(張三,語文,74)insert into tb values(張三,數學,83)insert into tb values(張三,物理,93)insert into tb values(李四,語文,74)insert into tb…