<!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 = num
s.match(reg
); var numList = new Array(
); if(numStrList
!= null)
{ for(var i = 0;i < numStrList.length;i++)
{ var int
number = parseInt(numStrList[i]
); numList.push(int
number
); }
}
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)
{ thi
s.resultList = list;
};
Sort.prototype.Partition = function(start,end)
{ var baseValue = thi
s.resultList[start];
var basePos = start;
for(var j = start + 1;j <= end;j++)
{ if(baseValue > thi
s.resultList[j])
{ basePos++; //move the base position
thi
s.Swap(basePos,j
); }
}
// move the base value to the correct place,before are
smaller,behind are bigger
thi
s.Swap(start,basePos
); return basePos;
}
Sort.prototype.QuickSort = function(start,end)
{ if(start < end)
{ var basePos = thi
s.Partition(start,end
); thi
s.QuickSort(start,basePos - 1
); thi
s.QuickSort(basePos + 1,end
); }
};
Sort.prototype.Swap = function(pos1,pos2)
{ var temp = thi
s.resultList[pos1];
thi
s.resultList[pos1] = thi
s.resultList[pos2];
thi
s.resultList[pos2] = temp;
}
Sort.prototype.getSortedList = function()
{ thi
s.QuickSort(0,thi
s.resultList.length - 1
); return thi
s.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>