JS插入,冒泡,merge,快,堆排序

//var arrays = new Array(11, 25, 2, 45, 3);
var arrays;
function getInputedArrays() {
	arrays = document.getElementById("arrays").value.split(",");
	document.getElementById("original_arrays").innerHTML = "original: ["
			+ arrays.toString() + "]";
	insertionSort();
	bubbleSort();
	mergeSort();
	quickSort();
	heapSort();
}

function insertionSort() {
	var ordered_arrays_insert = [].concat(arrays);
	var i = 1;
	for (; i < ordered_arrays_insert.length; i++) {
		var k = i;
		for (; k > 0; k--) {
			if (parseInt(ordered_arrays_insert[k]) < parseInt(ordered_arrays_insert[k - 1])) {
				temp = ordered_arrays_insert[k];
				ordered_arrays_insert[k] = ordered_arrays_insert[k - 1];
				ordered_arrays_insert[k - 1] = temp;
			}
		}
	}
	document.getElementById("insertionSort").innerHTML = "insertionSort: ["
			+ ordered_arrays_insert.toString() + "]";
}

function bubbleSort() {
	var ordered_arrays_bubble = [].concat(arrays);
	var i;
	for (i = 0; i < ordered_arrays_bubble.length; i++) {
		var j;
		for (j = arrays.length - 1; j > i; j--) {
			if (parseInt(ordered_arrays_bubble[j]) < parseInt(ordered_arrays_bubble[j - 1])) {
				temp = ordered_arrays_bubble[j];
				ordered_arrays_bubble[j] = ordered_arrays_bubble[j - 1];
				ordered_arrays_bubble[j - 1] = temp;
			}
		}
	}
	document.getElementById("bubbleSort").innerHTML = "bubbleSort: ["
			+ ordered_arrays_bubble.toString() + "]";
}

function mergeSort() {
	var ordered_arrays_merge = new Array();
	var array1 = new Array();
	var array2 = new Array();
	var len = arrays.length;
	// arrays.slice(start, end); -> end is not selected
	var array_1 = [].concat((arrays.slice(0, Math.floor(len / 2))));
	var array_2 = [].concat((arrays.slice(Math.floor(len / 2), len)));
	for (var i = 0; i < array_1.length; i++) {
		array1.push(parseInt(array_1[i]));
	}
	array1 = array1.sort(function(a, b){return a - b;});
	console.log((array1.toString()));
	for (var i = 0; i < array_2.length; i++) {
		array2.push(parseInt(array_2[i]));
	}
	array2 = array2.sort(function(a, b){return a - b;});
	console.log(array2.toString());
	var i = 0, j = 0;

	while (i < array1.length && j < array2.length) {
		if (array1[i] < array2[j]) {
			ordered_arrays_merge.push(array1[i++]);
		} else {
			ordered_arrays_merge.push(array2[j++]);
		}
	}

	if (i == array1.length)
		ordered_arrays_merge = ordered_arrays_merge.concat(array2.slice(j,
				array2.length));
	if (j == array2.length)
		ordered_arrays_merge = ordered_arrays_merge.concat(array1.slice(i,
				array1.length));
	document.getElementById("mergeSort").innerHTML = "mergeSort: ["
			+ ordered_arrays_merge.toString() + "]";
}

function quickSort() {
	var ordered_arrays_quick = [].concat(arrays);
	quickSortRecursion(ordered_arrays_quick, 0, ordered_arrays_quick.length - 1);
	document.getElementById("quickSort").innerHTML = "quickSort: ["
			+ ordered_arrays_quick.toString() + "]";
}

function quickSortRecursion(arr, left, right) {
	var len = arr.length;
	if (len <= 1)
		return arr;
	var index = partition(arr, left, right);
	if (left < index - 1)
		quickSortRecursion(arr, left, index - 1);
	if (index < right)
		quickSortRecursion(arr, index, right);
	return arr;
}

function partition(arr, left, right) {
	var pivot = arr[Math.floor((left + right) / 2)];
	var i = left;
	var j = right;

	while (i <= j) {
		while (parseInt(arr[i]) < parseInt(pivot))
			i++;
		while (parseInt(arr[j]) > parseInt(pivot))
			j--;
		if (i <= j) {
			temp = arr[i];
			arr[i] = arr[j];
			arr[j] = temp;
			i++;
			j--;
		}
	}
	return i;
}

function heapSort() {
	var ordered_arrays_heap = [].concat(arrays);
	for (var i = Math.floor(ordered_arrays_heap.length / 2) - 1; i >= 0; i--) {
		shifDown(ordered_arrays_heap, i, ordered_arrays_heap.length);
	}

	for (var i = Math.floor(ordered_arrays_heap.length - 1); i > 0; i--) {
		temp = ordered_arrays_heap[0];
		ordered_arrays_heap[0] = ordered_arrays_heap[i];
		ordered_arrays_heap[i] = temp;
		shifDown(ordered_arrays_heap, 0, i);
	}
	document.getElementById("heapSort").innerHTML = "heapSort: ["
			+ ordered_arrays_heap.toString() + "]";
}

function shifDown(ordered_arrays, i, length) {
	var tempAi = ordered_arrays[i];
	for (var j = 2 * i + 1; j < length; j = 2 * j + 1) {
		tempAi = ordered_arrays[i];
		if (j + 1 < length
				&& parseInt(ordered_arrays[j]) < parseInt(ordered_arrays[j + 1])) {
			j++;
		}
		if (parseInt(tempAi) < parseInt(ordered_arrays[j])) {
			temp = ordered_arrays[i];
			ordered_arrays[i] = ordered_arrays[j];
			ordered_arrays[j] = temp;
		} else
			break;
	}
}

 

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页