冒泡排序


	function aa() {

		//获取输入框中的数字
		var getStr = document.getElementById("textarea").value;
		
		var toArr = new Array();

		var toArr2 = new Array();

		//分割为单个数字
		toArr = getStr.split(",");

		//将分割后的数字转换类型为Number
		for (var i = 0; i < toArr.length; i++ ){

			var toNum = parseInt(toArr[i]);

			toArr2[i] = toNum;

		};

		//冒泡排序
		var temp = 0;

		for (var i = 0; i < toArr2.length; i++){

			for (var j = 0; j < toArr2.length - i; j++){

				if (toArr2[j] > toArr2[j + 1]){

					temp = toArr2[j + 1];

					toArr2[j + 1] = toArr2[j];

					toArr2[j] = temp;

				}

			}

		}

		//toArr2 是排好顺序的数组
		alert(toArr2)

	}

发表评论

电子邮件地址不会被公开。 必填项已用*标注