1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| function quickSort(array, start, end) { if (end - start < 1) return
let target = array[start] let l = start let r = end
while (l < r) { while (l < r && array[r] >= target) { r-- } array[l] = array[r] while (l < r && array[l] < target) { l++ } array[r] = array[l] }
array[l] = target
quickSort22(array, start, l - 1) quickSort22(array, l + 1, end) return array }
console.time('quickSort') console.log(quickSort(list, 0, list.length - 1)) console.timeEnd('quickSort')
function quickSort2Obj(array, start, end, key) { if (end - start < 1) return
let target = array[start] let l = start let r = end
while(l < r) { while(l < r && array[r][key] >= target[key]) { r-- } array[l] = array[r]
while(l < r && array[l][key] < target[key]) [ l++ ] array[r] = array[l] }
array[l] = target
quickSort2Obj(array, start, l - 1, key) quickSort2Obj(array, l + 1, end, key)
return array }
console.time('quickSort2Obj') console.log(quickSort2Obj(ageLists, 0, ageLists.length - 1, 'id')) console.timeEnd('quickSort2Obj')
|