赞
踩
-
- a = {1,3,5,2,6,8,10,3,6,20}
-
- --快速排序(以数组最左边的值为基准,比它小的放左边,比它大的放右边,然后再对左右两边的数组进行同样的操作,直到细分排列完成)
- local function KuaiSuPaiXu(arr, startIndex, endIndex)
- if startIndex >= endIndex then
- return
- end
- local lowIndex = startIndex
- local highIndex = endIndex
- local key = arr[lowIndex]
- while(lowIndex < highIndex) do
- while(arr[highIndex] >= key and highIndex > lowIndex) do --找到一个小于key的值放在左边
- highIndex = highIndex - 1
- end
- arr[lowIndex] = arr[highIndex]
- while(arr[lowIndex] <= key and highIndex > lowIndex) do --找到一个大于Key的放在右边
- lowIndex = lowIndex + 1
- end
- arr[highIndex] = arr[lowIndex]
- arr[lowIndex] = key --把key放在游标停止处
- end
- --对数组整体做一次排序,得到中间索引,切分成左右两个数组,然后再递归
- KuaiSuPaiXu(arr, startIndex, lowIndex - 1)
- KuaiSuPaiXu(arr, lowIndex + 1, endIndex)
- end
-
- local function PrintTab(t)
- for k, v in pairs(t) do
- print(v)
- end
- end
-
- print("KuaiSuPaiXu")
- KuaiSuPaiXu(a, 1, #a)
- PrintTab(a)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。