你可以用代码解释C中的插入排序吗? [英] can u explain the insertion sort in C with the code ?
本文介绍了你可以用代码解释C中的插入排序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在C中遇到插入排序问题。能用正确的代码和算法解释一下吗?
I have faced a problem in C with insertion sort. Can you please explain it with the proper code and algorithm?
推荐答案
#include<stdio.h>
int main(){
int i,j,s,temp,a[20];
printf("Enter total elements: ");
scanf("%d",&s);
printf("Enter %d elements: ",s);
for(i=0;i<s;i++)>
scanf("%d",&a[i]);
for(i=1;i<s;i++){>
temp=a[i];
j=i-1;
while((temp<a[j])&&(j>=0)){
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
printf("After sorting: ");
for(i=0;i<s;i++)>
printf(" %d",a[i]);
return 0;
}
Output:
Enter total elements: 5
Enter 5 elements: 3 7 9 0 2
After sorting: 0 2 3 7 9</stdio.h>
此程序将第一个元素替换为目标位置在每次迭代中按照升序或降序...
This program will replace first element to the target place in each iteration according to ascending or descending orders..
好吧,谷歌第一次点击插入排序C ++给了这个 [ ^ ],这看起来不错。
Google是一款功能强大的工具。这是你的朋友。你应该接受它并学会使用它。
Well, the first hit at google for "Insertion sort C++" gave this[^], which looks good to me.
Google is a powerful tool. It is your friend. You should embrace it and learn to use it.
不如谷歌那么快: Google插入排序 [ ^ ]
第一首是维基百科,其中有一个很好的解释和一个代码示例,在C: WikipediaInsertion_sort [< a href =http://en.wikipedia.org/wiki/Insertion_sorttarget =_ blanktitle =New Window> ^ ]
将来,请尝试自己做至少基础研究,不要浪费我们的时间或你的时间。
Not as well as a very quick Google would have done: Google "Insertion sort"[^]
The first hit is Wikipedia with both a good explanation and a code sample, in C: Wikipedia "Insertion_sort"[^]
In future, please try to do at least basic research yourself, and not waste our time or yours.
这篇关于你可以用代码解释C中的插入排序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文