Last active
January 26, 2021 06:46
-
-
Save hansamlin/0cbf62facb1a3c1b6fe9baa465c71d05 to your computer and use it in GitHub Desktop.
排序法
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function bubbleSort(arr) { | |
for(var i = 0; i < arr.length; i++) { | |
if(arr[i] > arr[i + 1]) { | |
var tmp = arr[i + 1]; | |
arr[i + 1] = arr[i]; | |
arr[i] = tmp; | |
} | |
} | |
var isOk = true; | |
for(var j = 0; j < arr.length; j++) { | |
if(arr[j] > arr[j + 1]) { | |
isOk = false; | |
break; | |
} | |
} | |
return isOk ? arr : bubbleSort(arr); | |
} | |
console.log(bubbleSort([8, 2, -50, 5, 39, 7, 4, -1])); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function insertSort(arr) { | |
var list = []; | |
for(var i = 0; i < arr.length; i++) { | |
var tmp = i; | |
var value = arr[i]; | |
for(var j = 0; j < i; j++) { | |
value = arr[i]; | |
if(list[j] < arr[i]) { | |
tmp = j + 1; | |
} else { | |
tmp = j; | |
break; | |
} | |
} | |
list.splice(tmp, 0, value); | |
} | |
return list; | |
} | |
console.log(insertSort([8, 2, -50, 5, 39, 7, 4, -1])) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function mergeSort(arr) { | |
if(arr.length === 1) return arr; | |
function split(array) { | |
if(array.length === 1) return array; | |
var middle = Math.floor(array.length / 2); | |
var left = array.slice(0, middle); | |
var right = array.slice(middle); | |
return merge(split(left), split(right)); | |
} | |
function merge(left, right) { | |
var list = []; | |
var leftIndex = 0; | |
var rightIndex = 0; | |
while(leftIndex < left.length && rightIndex < right.length) { | |
if(left[leftIndex] < right[rightIndex]) { | |
list.push(left[leftIndex]); | |
leftIndex++; | |
} else { | |
list.push(right[rightIndex]); | |
rightIndex++; | |
} | |
} | |
return list.concat(left.slice(leftIndex)).concat(right.slice(rightIndex)); | |
} | |
return split(arr); | |
} | |
console.log(mergeSort([8, 2, 5, 200, -50, 1, 4, 3, -1])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function quickSort(arr) { | |
if(arr.length === 1) return arr; | |
if(arr.length === 0) return []; | |
var middleIndex = Math.floor(arr.length / 2); | |
var left = []; | |
var right = []; | |
var pivot = arr.splice(middleIndex, 1)[0]; | |
for(var i = 0; i < arr.length; i++) { | |
if(arr[i] > pivot) { | |
right.push(arr[i]) | |
} else { | |
left.push(arr[i]) | |
} | |
} | |
console.log(left, right) | |
return quickSort(left).concat([pivot]).concat(quickSort(right)); | |
} | |
console.log(quickSort([8, 2, 5, 200, -50, 1, 4, 3, -1])) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function selectionSort(arr) { | |
for(var i = 0; i < arr.length; i++) { | |
var temp = arr[i + 1]; | |
var index = i + 1; | |
for (var j = i + 1; j < arr.length; j++) { | |
if (temp > arr[j]) { | |
temp = arr[j]; | |
index = j; | |
} | |
} | |
if(temp < arr[i]) { | |
arr[index] = arr[i]; | |
arr[i] = temp; | |
} | |
} | |
return arr; | |
} | |
console.log(selectionSort([-1, 46, 16, 22, -50,12, 77, 29, 33])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment