JavaScript實(shí)現(xiàn)快速排序(自已編寫)
簡(jiǎn)述:
用到j(luò)avascript的排序一組數(shù)字,js沒(méi)有直接的數(shù)字比較的函數(shù)可以調(diào)用,所以自己寫了一個(gè)快速排序
知識(shí)點(diǎn):
1. 正則表達(dá)式提取正負(fù)數(shù)字的string
2. str 轉(zhuǎn)數(shù)字 放回列表
3. js的對(duì)象Sort類的聲明及定義
4. Sort類構(gòu)造函數(shù)、成員函數(shù)定義方式(prototype)
5. 快速排序算法
代碼:
<!DOCTYPE html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />.
<html>
<title>Quick Sort</title>
<head>
<script type = "text/javascript">
/*************Get Number From Input***********/
function getNumList(){
var result = "";
var nums = document.getElementById('numbers').value;
var reg = /([-][1-9][0-9]*)|([1-9][0-9]*)/g;
var numStrList = nums.match(reg);
var numList = new Array();
if(numStrList != null){
for(var i = 0;i < numStrList.length;i++){
var intNumber = parseInt(numStrList[i]);
numList.push(intNumber);
}
}
return MainProgram(numList);
};
/*****************Main*************************/
function MainProgram(numList){
var sort = new Sort(numList);
var sortedList = sort.getSortedList();
if(sortedList == null)
document.getElementById('result').innerHTML = "WRONG INPUT";
else{
document.getElementById('result').innerHTML = sortedList.join(',');
}
}
/**************Sort Class***********************/
var Sort = function(list){
this.resultList = list;
};
Sort.prototype.Partition = function(start,end){
var baseValue = this.resultList[start];
var basePos = start;
for(var j = start + 1;j <= end;j++){
if(baseValue > this.resultList[j]){
basePos++; //move the base position
this.Swap(basePos,j);
}
}
// move the base value to the correct place , before are smaller , behind are bigger
this.Swap(start,basePos);
return basePos;
}
Sort.prototype.QuickSort = function(start,end){
if(start < end){
var basePos = this.Partition(start,end);
this.QuickSort(start,basePos - 1);
this.QuickSort(basePos + 1, end);
}
};
Sort.prototype.Swap = function(pos1,pos2){
var temp = this.resultList[pos1];
this.resultList[pos1] = this.resultList[pos2];
this.resultList[pos2] = temp;
}
Sort.prototype.getSortedList = function(){
this.QuickSort(0,this.resultList.length - 1);
return this.resultList;
};
</script>
</head>
<body>
<B> Quick Sort</B>
<br>
<br>
<input type= "text" id = 'numbers' value = '' />
<input type = 'button' value = "exec" onclick = 'getNumList()'/>
<br>
<br>
<B>SORTED LIST: <B> <b id = 'result'></b>
</body>
</html>
輸出:
- JavaScript算法系列之快速排序(Quicksort)算法實(shí)例詳解
- JavaScript希爾排序、快速排序、歸并排序算法
- Javascript實(shí)現(xiàn)快速排序(Quicksort)的算法詳解
- Javascript快速排序算法詳解
- javascript快速排序算法詳解
- js對(duì)象數(shù)組按屬性快速排序
- js快速排序的實(shí)現(xiàn)代碼
- JavaScript快速排序
- JS實(shí)現(xiàn)隨機(jī)化快速排序的實(shí)例代碼
- js實(shí)現(xiàn)數(shù)組冒泡排序、快速排序原理
- 深入理解JS實(shí)現(xiàn)快速排序和去重
- 基于JavaScript實(shí)現(xiàn)的快速排序算法分析
相關(guān)文章
javascript實(shí)現(xiàn)用戶必須勾選協(xié)議實(shí)例講解
這篇文章主要介紹了javascript實(shí)現(xiàn)用戶必須勾選協(xié)議實(shí)例講解,寫頁(yè)面的時(shí)候經(jīng)常會(huì)用到,有感興趣的同學(xué)可以學(xué)習(xí)下2021-03-03
JavaScript中對(duì)JSON對(duì)象的基本操作示例
JSON格式本就發(fā)自于JavaScript中的對(duì)象和數(shù)組,所以js操作起來(lái)自然也是最為簡(jiǎn)單原始,接下來(lái)我們就來(lái)看一些常用的JavaScript中對(duì)JSON對(duì)象的基本操作示例2016-05-05
javascript中數(shù)組array及string的方法總結(jié)
本文結(jié)合自己的使用經(jīng)驗(yàn),給大家總結(jié)了javascript中數(shù)組array及string的使用方法,這里推薦給有需要的小伙伴。2014-11-11
深入理解ECMAScript的幾個(gè)關(guān)鍵語(yǔ)句
下面小編就為大家?guī)?lái)一篇深入理解ECMAScript的幾個(gè)關(guān)鍵語(yǔ)句。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-06-06
Javascript數(shù)組循環(huán)遍歷之forEach詳解
本篇文章主要介紹了Javascript 數(shù)組循環(huán)遍歷之forEach詳解,對(duì)學(xué)習(xí)forEach有很好的幫助,有需要的可以了解一下。2016-11-11
js對(duì)象內(nèi)部訪問(wèn)this修飾的成員函數(shù)示例
這篇文章主要介紹了js對(duì)象內(nèi)部訪問(wèn)this修飾的成員函數(shù)示例,需要的朋友可以參考下2014-04-04
js中的鼠標(biāo)事件有哪些(用法示例學(xué)習(xí)進(jìn)階)
在JavaScript中,鼠標(biāo)事件是 Web 開(kāi)發(fā)中最常用的事件類型。鼠標(biāo)點(diǎn)擊事件包括 4 個(gè):click(單擊)、dblclick(雙擊)、mousedown(按下)和 mouseup(松開(kāi))。其中 click 事件類型比較常用,而 mousedown和mouseup事件類型多用在鼠標(biāo)拖放、拉伸操作中。2023-02-02
document 和 document.all 分別什么時(shí)候用
document 和 document.all 分別什么時(shí)候用...2006-09-09

