Advertisement
Guest User

Sorting for leaderboards

a guest
May 29th, 2015
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.22 KB | None | 0 0
  1. array = {}
  2.  
  3.  
  4. function randomnum(howmany,maxvalue)
  5.     for i = 1, howmany do
  6.         local x = math.random(1,maxvalue)
  7.         table.insert(array,x)
  8.     end
  9.     --quicksort(array, 1, #array)
  10. end
  11.  
  12. function partition(array, left, right, pivotIndex)
  13.     local pivotValue = array[pivotIndex]
  14.     array[pivotIndex], array[right] = array[right], array[pivotIndex]
  15.    
  16.     local storeIndex = left
  17.    
  18.     for i =  left, right-1 do
  19.         if array[i] <= pivotValue then
  20.             array[i], array[storeIndex] = array[storeIndex], array[i]
  21.             storeIndex = storeIndex + 1
  22.         end
  23.         array[storeIndex], array[right] = array[right], array[storeIndex]
  24.     end
  25.    
  26.    return storeIndex
  27. end
  28.  
  29. function quicksort(array, left, right)
  30.     if right > left then
  31.         local pivotNewIndex = partition(array, left, right, left)
  32.         quicksort(array, left, pivotNewIndex - 1)
  33.         quicksort(array, pivotNewIndex + 1, right)
  34.     end
  35. end
  36.  
  37.  
  38. --array = { 1, 5, 2, 17, 11, 3, 1, 22, 2, 37 }
  39.  
  40. arrayString = ""
  41.  
  42.  
  43. randomnum(10,20)
  44.  
  45.  
  46.  
  47. for i = 1, #array do
  48.     if i == 1 then
  49.         arrayString= arrayString ..tostring(array[i])
  50.     else
  51.         arrayString= arrayString ..", " ..tostring(array[i])
  52.     end
  53. end
  54.  
  55. print(arrayString)
  56. --[[
  57. for _, v in pairs(array) do
  58.     print("Num: "..tostring(v))
  59. end
  60. --]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement