lua 冒泡排序的几种写法
1 function bubbleSort(t) 2 local index = 0 3 for i=1, #t - 1 do 4 for j = 1, #t - i do 5 if t[j] > t[j + 1] then 6 t[j],t[j + 1] = t[j+1], t[j] 7 end 8 index = index + 1 9 end 10 end 11 print("---------bubbleSort---------------------") 12 print("循环次数:" .. index) 13 print("-----------------------------------------") 14 for k,v in pairs(t) do 15 print(k,v) 16 end 17 print("---------bubbleSort---------------------") 18 print(" ") 19 print(" ") 20 end 21 22 function bubbleSort2(t) 23 local index = 0 24 for i = 1, #t - 1 do 25 local isSorder = true 26 for j = 1, #t - 1 do 27 if t[j] > t[j + 1] then 28 t[j],t[j + 1] = t[j+1], t[j] 29 isSorder = false 30 end 31 index = index + 1 32 end 33 if isSorder then 34 break 35 end 36 end 37 print("---------bubbleSort2---------------------") 38 print("循环次数:" .. index) 39 print("-----------------------------------------") 40 for k,v in pairs(t) do 41 print(k,v) 42 end 43 print("---------bubbleSort2---------------------") 44 print(" ") 45 print(" ") 46 end 47 48 function bubbleSort3(t) 49 local index = 0 50 local isSorderLength = #t - 1 51 for i = 1, #t - 1 do 52 local isSorder = true 53 for j = 1, isSorderLength do 54 if t[j] > t[j + 1] then 55 t[j],t[j + 1] = t[j+1], t[j] 56 isSorder = false 57 isSorderLength = j 58 end 59 index = index + 1 60 end 61 62 if isSorder then 63 break 64 end 65 end 66 print("---------bubbleSort3---------------------") 67 print("循环次数:" .. index) 68 print("-----------------------------------------") 69 for k,v in pairs(t) do 70 print(k,v) 71 end 72 print("---------bubbleSort3---------------------") 73 print(" ") 74 print(" ") 75 end 76 77 function bubbleSort4(t) 78 local index = 0 79 for i = 1, #t - 1 do 80 for j = #t - 1, i, -1 do 81 if t[j] > t[j + 1] then 82 t[j],t[j + 1] = t[j+1], t[j] 83 end 84 index = index + 1 85 end 86 end 87 print("---------bubbleSort4---------------------") 88 print("循环次数:" .. index) 89 print("-----------------------------------------") 90 for k,v in pairs(t) do 91 print(k,v) 92 end 93 print("---------bubbleSort4---------------------") 94 print(" ") 95 print(" ") 96 end 97 98 function bubbleSort5(t) 99 local index = 0 100 for i = 1, #t - 1 do 101 local isSorder = true 102 for j = #t - 1, i, -1 do 103 if t[j] > t[j + 1] then 104 t[j],t[j + 1] = t[j+1], t[j] 105 isSorder = false 106 end 107 index = index + 1 108 end 109 if isSorder then 110 break 111 end 112 end 113 print("---------bubbleSort5---------------------") 114 print("循环次数:" .. index) 115 print("-----------------------------------------") 116 for k,v in pairs(t) do 117 print(k,v) 118 end 119 print("---------bubbleSort5---------------------") 120 print(" ") 121 print(" ") 122 end 123 124 function bubbleSort6(t) 125 local index = 0 126 for i = 1, #t - 1 do 127 local isSorder = true 128 local isSorderLength = #t - 1 129 for j = isSorderLength, i, -1 do 130 if t[j] > t[j + 1] then 131 t[j],t[j + 1] = t[j+1], t[j] 132 isSorder = false 133 isSorderLength = j 134 end 135 index = index + 1 136 end 137 if isSorder then 138 break 139 end 140 end 141 print("---------bubbleSort6---------------------") 142 print("循环次数:" .. index) 143 print("-----------------------------------------") 144 for k,v in pairs(t) do 145 print(k,v) 146 end 147 print("---------bubbleSort6---------------------") 148 print(" ") 149 print(" ") 150 end 151 152 function bubbleSort7(t) 153 local index = 0 154 for i = 1, #t / 2 do 155 local isSorder = true 156 for j = i, #t - i do 157 if t[j] > t[j + 1] then 158 t[j],t[j + 1] = t[j+1], t[j] 159 isSorder = false 160 end 161 index = index + 1 162 end 163 if isSorder then 164 break 165 end 166 167 isSorder = true 168 for j = #t - i, i+1, -1 do 169 if t[j] < t[j - 1] then 170 t[j],t[j - 1] = t[j-1], t[j] 171 isSorder = false 172 end 173 index = index + 1 174 end 175 if isSorder then 176 break 177 end 178 end 179 print("---------bubbleSort7---------------------") 180 print("循环次数:" .. index) 181 print("-----------------------------------------") 182 for k,v in pairs(t) do 183 print(k,v) 184 end 185 print("---------bubbleSort7---------------------") 186 print(" ") 187 print(" ") 188 end 189 190 local tab = {9, 8, 7, 6, 5, 4, 3, 2, 1} 191 local tab = {2, 3,4,5,6,7,1,8} 192 local tab = {1, 2, 1,3,32, 2, 7, 6, 5} 193 bubbleSort(tab) 194 bubbleSort2(tab) 195 bubbleSort3(tab) 196 bubbleSort4(tab) 197 bubbleSort5(tab) 198 bubbleSort6(tab) 199 bubbleSort7(tab)
一撇一捺 书写人的身价