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:

.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

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
.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

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.


Please enable JavaScript to view the comments powered by Disqus.

    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:

.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

插入排序的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 ;
}
 输出量
.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

插入排序的C ++代码

 输出量

最佳面试编码问题/挑战

  • 游程编码(字符串中字母的查找/打印频率)

  • 以线性时间复杂度对0、1和2的数组进行排序

  • 检查字谜(检查两个字符串是否是字谜)

  • 相对排序算法

  • 查找给定总和的子数组

  • 在给定总和K的二叉树中找到级别

  • 检查二叉树是否为BST(二叉搜索树)

  • 1 [0] 1个样式计数

  • 大写一行中每个单词的第一个和最后一个字母

  • 打印二叉树的垂直和

  • 打印二叉树的边界和

  • 反转单个链表

  • 解决主要算法问题的贪婪策略

  • 工作排序问题

  • 根到叶的路径总和

  • 矩阵中的出口点

  • 在链表中查找循环长度

  • 一流的礼帽

  • 打印所有没有兄弟的节点

  • 转换为求和树

  • 最短的源到目标路径



评论和讨论

广告:您是博主吗? 加入我们的Blogging论坛 。


请启用JavaScript以查看由Disqus提供的评论。


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

c语言插入排序算法

c语言插入排序算法_插入排序算法,流程图和C,C ++代码相关推荐

  1. cb32a_c++_STL_算法_查找算法_(5)adjacent_find

    cb32a_c++_STL_算法_查找算法_(5)adjacent_find adjacent_find(b,e),b,begin(),e,end() adjacent_find(b,e,p),p-p ...

  2. 常用十大算法_回溯算法

    回溯算法 回溯算法已经在前面详细的分析过了,详见猛击此处. 简单的讲: 回溯算法是一种局部暴力的枚举算法 循环中,若条件满足,进入递归,开启下一次流程,若条件不满足,就不进行递归,转而进行上一次流程. ...

  3. java寻优算法_模拟退火算法SA原理及python、java、php、c++语言代码实现TSP旅行商问题,智能优化算法,随机寻优算法,全局最短路径...

    模拟退火算法SA原理及python.java.php.c++语言代码实现TSP旅行商问题,智能优化算法,随机寻优算法,全局最短路径 模拟退火算法(Simulated Annealing,SA)最早的思 ...

  4. ls路由算法_路由算法详解

    路由算法详解1. 引言 2. 路由器基础知识 3. LS算法 4. 示例:Dijkstra算法 5. DV算法 6. 分级路由 如果您已经阅读过博闻网中的路由器工作原理一文,您会了解到路由器的作用是管 ...

  5. 数据挖掘算法_数据挖掘算法入门

    有南方的朋友讲过北方人喜欢打比方,尤其是甲方的,其实也没什么不好了.如果是做菜的话,那么这些算法就相当于烹饪的工具了.对原始的食材进行预处理.加工整合,选择合适烹饪工具,以及对应的方法步骤,最后收获舌 ...

  6. 接受拒绝算法_通过算法拒绝大学学位

    接受拒绝算法 数据科学 (Data Science) Nina was close to tears when she accused Nick Gibb of ruining her life. N ...

  7. python序列模式的关联算法_关联算法

    以下内容来自刘建平Pinard-博客园的学习笔记,总结如下: 1 Apriori算法原理总结 Apriori算法是常用的用于挖掘出数据关联规则的算法,它用来找出数据值中频繁出现的数据集合,找出这些集合 ...

  8. 编程神奇算法_分类算法的神奇介绍

    编程神奇算法 由Bryan Berend | 2017年3月23日 (by Bryan Berend | March 23, 2017) About Bryan: Bryan is the Lead ...

  9. prim算法_贪心算法详解(附例题)

    贪心算法的特征规律 贪心算法,"贪心"二字顾名思义,因此其规律特征就是更加注重当前的状态,贪心法做出的选择是对于当前所处状态的最优选择,它的解决问题的视角是微观的"局部& ...

  10. 回溯算法和贪心算法_回溯算法介绍

    回溯算法和贪心算法 回溯算法 (Backtracking Algorithms) Backtracking is a general algorithm for finding all (or som ...

最新文章

  1. 安装kube-prometheus项目:k8s部署prometheus、监控k8s核心组件、添加告警(微信、钉钉、企业微信)、进行数据持久化
  2. ASP.NET Core微服务(七)——【docker部署linux上线】(RDS+API接口测试部分)
  3. 查询成绩最好的前两名_一级建造师成绩查询前这几件事很重要
  4. VueConf China 2021 《Vue3生态进展-尤雨溪》 Reaction
  5. 系统即将关机请保存关机是由nt_设置Windows电脑自动关机
  6. 【渝粤教育】国家开放大学2018年春季 0675-22T中级财务会计(2) 参考试题
  7. MySQL中CREATE DATABASE和CREATE SCHEMA区别(转)
  8. DevOps 国际峰会,为你讲解腾讯的 Git 转型之路
  9. 电信5g网络apn接入点_华为就5G网络设备禁令起诉瑞典邮政和电信管理局
  10. linux 查看jvm虚拟内存_看完你还敢说你懂JVM吗?
  11. fread 单独测试没有问题 在正式项目里面丢数据 可能是系统资源不足 预读出了问题
  12. Docker代理设置方法
  13. SQL 错误代码汇总
  14. NTP详解(网络时间协议)
  15. 苹果越狱后怎么还原_iOS 13.3 越狱提升稳定性,自签又可以使用了
  16. spring中动态代理的两种方式
  17. 15个网页数据采集中最常遇到的问题(干货)
  18. 离线浏览工具webdup 可下载网站页面
  19. 商汤的AI伴游小精灵(2019 计蒜之道 初赛 第一场)
  20. python3 全局变量_Python3基础 当函数中的局部变量与全局变量同名了,各管各的...

热门文章

  1. jade软件_TEM衍射斑点标定之DM软件
  2. tensorflow分类的loss函数_tensorflow 分类损失函数使用小记
  3. python字符串前面加f什么意思_Python 字符串前面加u,r,b,f的含义
  4. 计算payload长度c语言,C语言0长度数组(可变数组/柔性数组)详解
  5. matlab 三维 作图 坐标轴_MATLAB学习——MATLAB中的三维绘图指令
  6. oem监控mysql_OEM12c 安装配置MySQL Plug-in用来监控MySQL
  7. c语言提取七位数讲解,C语言-体育彩票7位数,感受身中500万的fell
  8. java jsonobject.parse_JSON.parseObject的几种用法
  9. Flutter 使用动画播放一组图片
  10. 网络框架 Retrofit(三)