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

2 篇文章 0 订阅
1 篇文章 0 订阅
//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;
	}
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值