zl程序教程

您现在的位置是:首页 >  后端

当前栏目

python和C语言分别实现插入排序

PythonC语言 实现 分别 插入排序
2023-09-14 08:57:59 时间


代码Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-- 1 def InsertSort(numbers,n):

 for i in range(1,n):

 j = i-1

 tem = numbers[i]

 while numbers[j] tem and j =0:

 numbers[j+1] = numbers[j]

 j -= 1

 else:

 numbers[j+1] = tem

 print "Onthe sort:",numbers

a = [9,8,7,6,5,4,3,2,1,0]

print "BeforeSort:",a

InsertSort(a, 10)

print "After Sort:",a

最后输出为:

BeforeSort: [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
Onthe sort: [8, 9, 7, 6, 5, 4, 3, 2, 1, 0]
Onthe sort: [7, 8, 9, 6, 5, 4, 3, 2, 1, 0]
Onthe sort: [6, 7, 8, 9, 5, 4, 3, 2, 1, 0]
Onthe sort: [5, 6, 7, 8, 9, 4, 3, 2, 1, 0]
Onthe sort: [4, 5, 6, 7, 8, 9, 3, 2, 1, 0]
Onthe sort: [3, 4, 5, 6, 7, 8, 9, 2, 1, 0]
Onthe sort: [2, 3, 4, 5, 6, 7, 8, 9, 1, 0]
Onthe sort: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
Onthe sort: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
After Sort: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

C语言实现代码:


代码Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-- #include stdio.h 

void insertSort(int a[10],int n)

 int i,j,k;

 for(i=1;i i++)

 int tem = a[i];

 j = i - 1;

 while(a[j] tem j =0)

 a[j+1] = a[j];

 j--;

 a[j+1] = tem;

 for(k=0;k k++)

 printf("%d",a[k]);

 printf("\n");

int main()

 int b[10] = {9,8,7,6,5,4,3,2,1,0};

 int i;

 for(i=0;i i++)

 printf("%d",b[i]);

 printf("\n");

 insertSort(b,10);

 for(i=0;i i++)

 printf("%d",b[i]);

}

最后输出:

9876543210
8976543210
7896543210
6789543210
5678943210
4567893210
3456789210
2345678910
1234567890
0123456789



LeCun自曝使用C语言23年之久,2年前才上手Python,还曾短暂尝试Lua 云栖号资讯:【点击查看更多行业资讯】在这里您可以找到不同行业的第一手的上云资讯,还在等什么,快来! 程序员圈子的流行风潮,过几年就怀旧风走一波。 这不,最近Twitter上刮起了一阵编程语言使用历史的风潮。
Python: C语言 C语言是一门面向过程的、抽象化的通用程序设计语言,广泛应用于底层开发。C语言能以简易的方式编译、处理低级存储器。C语言是仅产生少量的机器语言以及不需要任何运行环境支持便能运行的高效率程序设计语言。