# 数据结构与算法（JavaScript实现）

1.如何在数组中间位置添加数组

function avaerageAdd(){
var nums = [1,2,3,4,5,6,7,8];
var newElements = [233,666];
nums.splice.apply(nums, [Math.floor(nums.length)/2, 0].concat(newElements));
return nums // [1, 2, 3, 4, 233, 666, 5, 6, 7, 8];
}

2.判定给定字符串是否回文

function isPalindrome(word){
var s = new Stack()
for (var i = 0; i < word.length; ++i) {
s.push(word[i])
}
var rword = ""
while(s.length() > 0){
rword += s.pop()
}
if(word == rword){
return true
}else{
return false
}
}
var Stack = function() {
this.dataStore = [];
this.top = 0;
};
Stack.prototype.push = function(element) {
this.dataStore.push(element);
};
Stack.prototype.pop = function() {
return this.dataStore.pop();
};
Stack.prototype.peek = function() {
return this.dataStore[this.dataStore.length - 1];
};
Stack.prototype.length = function() {
return this.dataStore.length;
};
Stack.prototype.isEmpty = function() {
return this.dataStore.length === 0;
};
Stack.prototype.seeAll = function() {
return this.dataStore.join('\n');
};

3.冒泡排序

function exchange(array, i, j) {
var t = array[i];
array[i] = array[j];
array[j] = t;
}
function bubbleSort(numbers) {
for (var i = 0; i < numbers.length; i++) {
for (var j = 0; j < numbers.length - i; j++) {
if (numbers[j] > numbers[j + 1]) {
exchange(numbers, j, j + 1);
}
}
console.log(numbers.toString());
}
return numbers;
}
var nums = [2,3,4,3,1,5,7,122,341,-1];
console.log(bubbleSort(nums));
4.快速排序

function quickSort(arr) {
if (arr.length <= 1) { return arr }
console.log("原数组是:" + arr)
var pivotIndex = Math.floor(arr.length / 2)
var pivot = arr.splice(pivotIndex, 1)[0]
var left = []
var right = []
console.log("将中介提取出来后数组是:" + arr)
for (var i = 0 ; i < arr.length ; i++){
console.log("此刻中介是:" + pivot + "当前元素是:" + arr[i])
if (arr[i] < pivot) {
left.push(arr[i])
console.log("移动" + arr[i] + "到左边")
} else {
right.push(arr[i])
console.log("移动" + arr[i] + "到右边")
}
}
return quickSort(left).concat([pivot], quickSort(right))
}
var nums = [2,3,4,3,1,5,7,122,341,-1]
console.log(quickSort(nums))

5.选择排序

function selectionSort(numbers) {
for (var i = 0; i < numbers.length; i++) {
var min = i;
for (var j = i + 1; j < numbers.length; j++) {
if (numbers[j] < numbers[min]) {
min = j;
}
}
if (i !== min) {
exchange(numbers, i, min);
}
}
return numbers;
}
var nums = [2,3,4,3,1,5,7,122,341,-1]
console.log(selectionSort(nums))

6.插入排序

function insertionSort(numbers) {
console.log("原数组:" + numbers)
for (var i = 0; i < numbers.length; i++) {
/*
* 当已排序部分的当前元素大于value，
* 就将当前元素向后移一位，再将前一位与value比较
*/
for (var j = i; j > 0 && numbers[j] < numbers[j - 1]; j--) {
// If the array is already sorted, we never enter this inner loop!
exchange(numbers, j, j - 1);
console.log("此时数组:" + numbers);
}
}
return numbers;
};
var nums = [2,3,4,3,1,5,7,122,341,-1];
console.log(insertionSort(nums));


7.希尔排序

function shellsort(numbers) {
console.log("原数组:" + numbers)
var h = 1;
while (h < numbers.length / 3) {
h = (3 * h) + 1;
}
while (h >= 1) {
console.log("此时h:" + h)
for (var i = h; i < numbers.length; i++) {
for (var j = i; j >= h && numbers[j] < numbers[j - h]; j -= h) {
exchange(numbers, j, j - h);
console.log("此时数组:" + numbers);
}
}
h = --h / 3;
}
return numbers;
}
var nums = [2,3,4,3,1,5,7,122,341,-1];
console.log(shellsort(nums));

8.归并算法

function mergeSort(numbers) {
if (numbers.length < 2) {
return numbers;
}
var middle = Math.floor(numbers.length / 2),
left = numbers.slice(0, middle),
right = numbers.slice(middle),
params = merge(mergeSort(left), mergeSort(right));
params.unshift(0, numbers.length);
numbers.splice.apply(numbers, params);
return numbers;
function merge(left, right) {
var result = [],
il = 0,
ir = 0;
while (il < left.length && ir < right.length) {
if (left[il] < right[ir]) {
result.push(left[il++]);
} else {
result.push(right[ir++]);
}
}
return result.concat(left.slice(il)) .concat(right.slice(ir));
}
}
var nums = [2,3,4,3,1,5,7,122,341,-1]
console.log(mergeSort(nums))

9.二分搜索

function binSearch(arr, data) {
arr = arr.sort(function(a, b) {
return a - b;
})
console.log(arr)
var upperBound = arr.length-1;
var lowerBound = 0;
while (lowerBound <= upperBound) {
var mid = Math.floor((upperBound + lowerBound) / 2);
console.log("Current midpoint: " + mid);
if (arr[mid] < data) {
lowerBound = mid + 1;
}
else if (arr[mid] > data) {
upperBound = mid - 1;
}
else {
return mid;
}
}
return -1；
}
var nums = [2,3,4,3,1,5,7,122,341,-1]
console.log(binSearch(nums,122))