概述本文源自: http://blog.csdn.net/wziyx513225244/article/details/8439648 --[[ print an array with some descriptions descriptions 打印array之前的描述信息 array 需要打印的数组 --]] function show(description
本文源自: http://blog.csdn.net/wziyx513225244/article/details/8439648
--[[ print an array with some descriptions descriptions 打印array之前的描述信息 array 需要打印的数组 --]] function show(descriptions,array) io.write(descriptions,"\n\r\t") for i,value in ipairs(array) do io.write(value) if array[i+1] then io.write(",") end end io.write("\n\r") end --[[ 获取数组的长度 --]] function GetArrayLength(array) local n=0; while array[n+1] do n=n+1 return n; 冒泡排序 array 需要排序的数字 compareFunc 比较函数 function bubbleSort(array,compareFunc) local len = GetArrayLength(array) local i = len while i > 0 do j=1 while j< len do if compareFunc(array[j],array[j+1]) then array[j],array[j+1] = array[j+1],array[j] end j = j + 1 end i = i - 1 end 选择排序算法 array 需要排序的数字 compareFunc 比较函数 function selectSort(array,compareFunc) local len = GetArrayLength(array) local i = 1 while i <= len do local j= i + 1 while j <=len do if compareFunc(array[i],array[j]) then array[i],array[j] = array[j],array[i] end j = j + 1 i = i + 1 end 快速排序方便统一调用 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"> 快速排序 left 左边已经完成比较的数组下标 right 右边已经完成比较的数组下标 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 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"> 快速排序的一趟排序 left 左边已经完成比较的数组下标 right 右边已经完成比较的数组下标 function partion(array,108); List-style:decimal-leading-zero outsIDe; color:inherit; line-height:18px"> local key = array[left] -- 哨兵 一趟排序的比较基准 local index = left array[index],array[right] = array[right],array[index] -- 与最后一个元素交换 local i = left while i< right do if compareFunc( key,array[i]) then array[index],array[i] = array[i],array[index]-- 发现不符合规则 进行交换 index = index + 1 i = i + 1 array[right],array[index] = array[index],array[right] -- 把哨兵放回 return index; array={5,6,7,9,2,3,4,8,12,11,10} 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) show("after bubbleSort array",array) selectSort(array,y) return y<x end) show("after selectsort array", array) quickSort(array,108); List-style:decimal-leading-zero outsIDe; color:inherit; line-height:18px"> show("after quickSort array", array) 总结 以上是内存溢出为你收集整理的lua 排序全部内容,希望文章能够帮你解决lua 排序所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
评论列表(0条)