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.mzph.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…