<script> var list = [25,15,60,24,30,70,10,9,8]; //冒泡排序 function bubble(list) { var len = list.length,n for (var i =0;i<len;i++){ //i为0:可以确定最小值,i为1:确定第二小的值 ... for (var j=i+1;j<len;j++){ if(list[i]>list[j]){ n = list[i] list[i] = list[j] list[j] = n } } } return list } //选择排序 function select(list) { var len = list.length,n,min for (var i =0;i<len;i++){ min = list[i] for (var j=i+1;j<len;j++){ if(min>list[j]){ n = list[j] list[j] = min min = n } } list[i] = min } return list } //快速排序 function quick(list) { if(list.length<=1) return list; var left=[],right=[],point; point = list.pop(); for (var i =0 ; i<list.length ; i++){ if(point<list[i]){ right.push(list[i]) }else { left.push(list[i]) } } return quick(left).concat([point],quick(right)) } //插入排序 function insert(list) { var arr=[list[0]],len=list.length,key,j for (var i = 1; i<len;i++){ key = list[i],j=i-1 while (j>=0 && key<arr[j]){ arr[j+1]= arr[j] j-- } arr[j+1] = key } return arr } //二分查找 function binaryFind(list,num,start,end) { start = start || 0,end = end || list.length; var mid = Math.floor((start + end) / 2); if(list[mid] == num){ return mid }else if(list[mid]>num){ return binaryFind(list,num,start,mid) }else { return binaryFind(list,num,mid,end) } } </script>
var arr = [343, 435, 23, 345, 234, 766, 436, 235, 578, 34, 56, 1, 57, 3] function Bubble() { /* * 内层的循环比较相邻的两个数字,比较---交换,将最大值调整到末尾 * 外层的循环每一次确定一个最大值到末尾 * */ var length = arr.length, newArr = arr; for (var j = 0; j < length - 1; j++) { // 每一轮确认一个最大的值到末尾 for (var i = 0; i < length - 1 - j; i++) { // 对比相邻的两个并且比较替换,最后把大的数字调整到末尾 if (newArr[i] > newArr[i + 1]) { var temp = newArr[i]; newArr[i] = newArr[i + 1] newArr[i + 1] = temp; } } } return newArr } function choose() { /* * 选择每次最大或者最小的值 * 把该值调整到开头或者末尾 * */ var length = arr.length, newArr = arr; for (var j = 0; j < length - 1; j++) { var min = j; for (var i = j + 1; i < length; i++) { if (newArr[min] > newArr[i]) { var temp = newArr[min]; newArr[min] = newArr[i] newArr[i] = temp; } } } return newArr } function insert() { /* * 以第一个为排序完成的的数组 * 向排序完成数组里面插入值 * */ var length = arr.length, newArr = arr; for (var j = 1; j < length; j++) { var index = j for (var i = j - 1; i >= 0; i--) { if (newArr[i] > newArr[index]) { var temp = newArr[index]; newArr[index] = newArr[i] newArr[i] = temp index = i } } } return newArr } //归并排序 function merge(leftArr, rightArr) { var index = 0, newArr = [], left = 0, right = 0 while (left < leftArr.length && right < rightArr.length) { if (leftArr[left] < rightArr[right]) { newArr[index] = leftArr[left] left++ } else { newArr[index] = rightArr[right] right++ } index++ if (left == leftArr.length) newArr = newArr.concat(rightArr.slice(right)) if (right == rightArr.length) newArr = newArr.concat(leftArr.slice(left)) } return newArr } function mergeSort(arr) { /* * 归并排序 * 先分小组排序 * 然后把多个排好序的数组两两合并排序 * */ if (arr.length == 1) return arr; var length = Math.ceil(arr.length / 2), left = arr.slice(0, Math.ceil(length)), right = arr.slice(length) var leftArr = mergeSort(left), rightArr = mergeSort(right) var result = merge(leftArr, rightArr) return result } //堆排序 function Node() { this.left = null; this.right = null; this.value = null this.index = null } //构建初始堆 function buildTree(arr) { var treeNode, parentNode, nodeList = [], node; for (var i = 0; i < arr.length; i++) { node = new Node(); node.value = arr[i] node.index = i if (!treeNode) { treeNode = node; nodeList.push(node) } else { if (!parentNode) parentNode = nodeList.shift() if (!parentNode.left) { parentNode.left = node nodeList.push(node) } else { parentNode.right = node nodeList.push(node) parentNode = null } } } return treeNode; } //构建最大堆 function buildMaxStach(node) { if (!node) return null if (!node.left && !node.right) return node; var leftNode = buildMaxStach(node.left), rightNode = buildMaxStach(node.right),temp if (leftNode && leftNode.value > node.value) { temp = node node = leftNode leftNode = temp } if (rightNode && rightNode.value > node.value) { temp = node node = rightNode rightNode = temp } return node; } function stackSort(arr) { var length = arr.length, newArr,maxNode,temp for (var i = 0; i < length; i++) { newArr = arr.slice(i); maxNode = buildMaxStach(buildTree(newArr)) temp = arr[i] arr[i] = maxNode.value arr[maxNode.index+i] = temp } return arr }