加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

lua 排序

发布时间:2020-12-14 22:06:15 所属栏目:大数据 来源:网络整理
导读:本文源自: http://blog.csdn.net/wziyx513225244/article/details/8439648 --[[?? print?an?array?with?some?descriptions?? ????descriptions??打印array之前的描述信息?? ????array?需要打印的数组?? --]]?? function?show(descriptions,array)?? ????io.
本文源自: http://blog.csdn.net/wziyx513225244/article/details/8439648


  1. --[[??
  2. print?an?array?with?some?descriptions??
  3. ????descriptions??打印array之前的描述信息??
  4. ????array?需要打印的数组??
  5. --]]??
  6. function?show(descriptions,array)??
  7. ????io.write(descriptions,"nrt")??
  8. ????for?i,value?in?ipairs(array)?do??
  9. ????????io.write(value)??
  10. ????????if?array[i+1]?then??
  11. ????????????io.write(",")??
  12. ????????end??
  13. ????end??
  14. ????io.write("nr")??
  15. end??
  16. --[[??
  17. 获取数组的长度??
  18. --]]??
  19. function?GetArrayLength(array)??
  20. ????local?n=0;??
  21. ????while?array[n+1]?do??
  22. ????????n=n+1??
  23. ????return?n;??
  24. 冒泡排序??
  25. ????array?需要排序的数字??
  26. ????compareFunc?比较函数??
  27. function?bubbleSort(array,compareFunc)??
  28. ????local?len?=?GetArrayLength(array)??
  29. ????local?i?=?len??
  30. ????while?i?>?0?do??
  31. ????????j=1??
  32. ????????while?j<?len?do??
  33. ????????????if?compareFunc(array[j],array[j+1])?then??
  34. ????????????????array[j],array[j+1]?=?array[j+1],array[j]??
  35. ????????????end??
  36. ????????????j?=?j?+?1??
  37. ????????end??
  38. ????????i?=?i?-?1??
  39. end??
  40. ??
  41. ??
  42. 选择排序算法??
  43. ????array?需要排序的数字??
  44. ????compareFunc?比较函数??
  45. function?selectSort(array,compareFunc)??
  46. ????local?len?=?GetArrayLength(array)??
  47. ????local?i?=?1??
  48. ????while?i?<=?len?do??
  49. ????????local?j=?i?+?1??
  50. ????????while?j?<=len?do??
  51. ????????????if?compareFunc(array[i],array[j])?then??
  52. ????????????????array[i],array[j]?=?array[j],array[i]??
  53. ????????????end??
  54. ????????????j?=?j?+?1??
  55. ????????i?=?i?+?1??
  56. ????end??
  57. 快速排序方便统一调用??
  58. function?quickSort(array,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> ????quick(array,1,GetArrayLength(array),108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> 快速排序??
  59. ????left??左边已经完成比较的数组下标??
  60. ????right?右边已经完成比较的数组下标??
  61. function?quick(array,left,right,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> ????if(left?<?right?)?then??
  62. ????????local?index?=?partion(array,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> ????????quick(array,index-1,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> ????????quick(array,index+1,108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> 快速排序的一趟排序??
  63. ????left??左边已经完成比较的数组下标??
  64. ????right?右边已经完成比较的数组下标??
  65. function?partion(array,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> ????local?key?=?array[left]?--?哨兵??一趟排序的比较基准??
  66. ????local?index?=?left??
  67. ????array[index],array[right]?=?array[right],array[index]?--?与最后一个元素交换??
  68. ????local?i?=?left??
  69. ????while?i<?right?do??
  70. ????????if?compareFunc(?key,array[i])?then??
  71. ????????????array[index],array[i]?=?array[i],array[index]--?发现不符合规则?进行交换??
  72. ????????????index?=?index?+?1??
  73. ????????i?=?i?+?1??
  74. ????array[right],array[index]?=?array[index],array[right]?--?把哨兵放回??
  75. ????return?index;??
  76. array={5,6,7,9,2,3,4,8,12,11,10}??
  77. show("original?array",108); list-style:decimal-leading-zero outside; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> bubbleSort(array,?function(x,y)?return?x<y?end)??
  78. show("after?bubbleSort?array",array)??
  79. selectSort(array,y)?return?y<x?end)??
  80. show("after?selectsort?array",?array)??
  81. quickSort(array,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> show("after?quickSort?array",?array)?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读