# js数值排序中冒泡算法的4种简单实现

changli

``````function getRandomArr(n) {
let arr = [];
for (let i = 0; i < n; i++) {
arr.push(~~(Math.random() * 100));
}
return arr
}
let randomArr = getRandomArr(20);``````

``````function bubbleSort(arr) {
let len = arr.length;
let temp;
let i = len - 1;
while(i > 0) {
for (let j = 0; j < i; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
i--;//不断缩小范围
}
return arr;
}
console.log(randomArr)//[ 93, 72, 29, 17, 82, 26, 56, 71, 35, 48, 37, 42, 3, 11, 33, 66, 81, 53, 59, 53 ]
console.log('bubbleSort', bubbleSort(randomArr.concat()));//bubbleSort [ 3, 11, 17, 26, 29, 33, 35, 37, 42, 48, 53, 53, 56, 59, 66, 71, 72, 81, 82, 93 ]``````

``````function bubbleSort2(arr) {
let len = arr.length;
let i = len - 1;
let temp;
let pos;//用来记录位置的
while (i > 0) {
pos = 0;//初始为0如果数组一开始已经排好序了,那么就可以很快终止冒泡
for (let j = 0; j < i; j++) {
if (arr[j] > arr[j + 1]) {
pos = j;
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
i = pos;
}
return arr;
}
console.log(randomArr)//[47, 31, 85, 65, 44, 56, 54, 5, 67, 44, 76, 13, 90, 12, 83, 72, 2, 69, 58, 60]
console.log('bubbleSort2', bubbleSort2(randomArr.concat()));//bubbleSort2 [2, 5, 12, 13, 31, 44, 44, 47, 54, 56, 58, 60, 65, 67, 69, 72, 76, 83, 85, 90]``````

``````function bubbleSort3(arr) {
let len = arr.length;
let low = 0;
let higth = len - 1;
let temp;
while (low < higth) {
for (let j = low; j < higth; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
higth--;
for (let j = higth; j > low; j--) {
if (arr[j] < arr[j - 1]) {
temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
}
}
low++;
}
return arr;
}
console.log(randomArr)//[40, 78, 16, 97, 38, 27, 66, 44, 45, 31, 12, 1, 99, 68, 36, 42, 40, 54, 6, 42]
console.log('bubbleSort3', bubbleSort3(randomArr.concat()));//bubbleSort3 [1, 6, 12, 16, 27, 31, 36, 38, 40, 40, 42, 42, 44, 45, 54, 66, 68, 78, 97, 99]``````

``````function bubbleSort4(arr) {
let len = arr.length;
let low = 0;
let higth = len - 1;
let temp;
while (low < higth) {
let hPos = 0;
let lPos = higth;
for (let j = low; j < higth; j++) {
if (arr[j] > arr[j + 1]) {
hpos = j;
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
heigth = hPos;
for (let j = higth; j > low; j--) {
if (arr[j] < arr[j - 1]) {
lPos = j;
temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
}
}
low = lPos;
}
return arr;
}
console.log(randomArr)//[40, 78, 16, 97, 38, 27, 66, 44, 45, 31, 12, 1, 99, 68, 36, 42, 40, 54, 6, 42]
console.log('bubbleSort4', bubbleSort4(randomArr.concat()));//[1, 6, 12, 16, 27, 31, 36, 38, 40, 40, 42, 42, 44, 45, 54, 66, 68, 78, 97, 99]``````

``````var randomArr = getRandomArr(10000);
console.time('1');
bubbleSort(randomArr.concat());
console.timeEnd('1');
console.time('2');
bubbleSort2(randomArr.concat());
console.timeEnd('2');
console.time('3');
bubbleSort3(randomArr.concat());
console.timeEnd('3');
console.time('4');
bubbleSort4(randomArr.concat());
console.timeEnd('4');
VM371:4 1: 329.705ms
VM371:7 2: 379.501ms
VM371:10 3: 310.843ms
VM371:13 4: 306.847ms``````

497 声望
4 粉丝
0 条评论