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

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:



插入排序的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 ++代码

输出量

最佳面试编码问题/挑战

评论和讨论

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

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

《c语言插入排序算法_插入排序算法,流程图和C,C ++代码》
《c语言插入排序算法_插入排序算法,流程图和C,C ++代码》

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

c语言插入排序算法

    原文作者:cumt951045
    原文地址: https://blog.csdn.net/cumt951045/article/details/107801385
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞