lua --- merge sort
来源:互联网 发布:内存数据库 比较 编辑:程序博客网 时间:2024/06/05 17:02
--merge sortlocal function printArray(a)for i=1, #a doprint(a[i]);endendlocal function divide(a)local b = {};local c = {};local n = math.floor( #a/2);for i=1, n dob[i] = a[i];endfor i=n+1, #a doc[i - n] = a[i];endreturn b, c;endlocal mergesort;mergesort = function (inarray)--printArray(inarray)--if #inarray >=1 then--print(#inarray)--endif #inarray <= 1 then return inarray;endlocal a,b = divide(inarray)a = mergesort(a);b = mergesort(b);--mergelocal i = 1;local j = 1;local c = {};while a[i] and b[j] doif a[i] < b[j] thenc[i + j - 1] = a[i];i = i+1;elsec[i + j - 1] = b[j];j = j+1;endendwhile a[i] doc[i+j-1] = a[i];i=i+1endwhile b[j] doc[i+j-1] = b[j];j=j+1endreturn c;endarray = {sort = mergesort,out = printArray,}--test programtestarray = {12, 2, 4, 54, 67, 32 , 11, 1, 4, 45, 666}sortres = array.sort(testarray)array.out(testarray)print("sorted:")array.out(sortres)
0 0
- lua --- merge sort
- Merge Sort
- Merge sort
- merge sort
- merge sort
- merge sort
- Merge Sort
- merge sort
- Merge Sort
- Merge Sort
- merge sort
- Merge Sort
- Merge sort
- Merge-sort
- Merge Sort
- Merge sort
- Merge Sort
- Merge-Sort
- cocos2d-x3.0中bezier曲线的分析
- Unreal里的动画
- Cocos2d 3.2 cocosstudio 按钮无法点击
- 2-12. 两个有序链表序列的交集
- hdu 1999 不可摸数(朴素打表法求质因子之和)
- lua --- merge sort
- javascript学习六~BOM和DOM简介,window,screen,navigator,location,history,节点增删改查
- Android开发之—.9.png图片的制作
- getch()和getchar()的区别
- Linux platform总线(2):应用实例
- MSP430--低功耗模式
- 美素数(杭电4548)
- 【Vish23n】Java设计模式之代理模式
- HDU 1874 畅通工程续 最短路径