所謂排序
輸入
輸出
這裡
直接插入排序基本思想
假設待排序的記錄存放在數組R[
算法描述
function InsertSort(arr) { //插入排序
var st = new Date();
var temp
for(var i=
if((arr[i]) < (arr[i
temp = arr[i];
j = i
do {
arr[j+
j
}
while (j>
arr[j+
}//endif
}
status = (new Date()
return arr;
}
希爾排序基本思想
先取一個小於n的整數d
該方法實質上是一種分組插入方法
算法描述
function ShellSort(arr) { //插入排序
var st = new Date();
var increment = arr
do {
increment = (increment/
arr = ShellPass(arr
}
while (increment >
status = (new Date()
return arr;
}
function ShellPass(arr
var temp
for(var i=d; i<arr
if((arr[i]) < (arr[i
temp = arr[i]; j = i
do {
arr[j+d] = arr[j];
j = j
}
while (j>
arr[j+d] = temp;
}//endif
}
return arr;
}
冒泡排序基本思想
將被排序的記錄數組R[
算法描述
function BubbleSort(arr) { //交換排序
var st = new Date();
var temp;
var exchange;
for(var i=
exchange = false;
for(var j=arr
if((arr[j+
temp = arr[j+
arr[j+
arr[j] = temp;
exchange = true;
}
}
if(!exchange) break;
}
status = (new Date()
return arr;
}
快速排序基本思想
將原問題分解為若干個規模更小但結構與原問題相似的子問題
在R[low
算法描述
function QuickSort(arr) { //交換排序
if (arguments
var low = arguments[
var high = arguments[
} else {
var low =
var high = arr
}
if(low < high){
// function Partition
var i = low;
var j = high;
var pivot = arr[i];
while(i<j) {
while(i<j && arr[j]>=pivot)
j
if(i<j)
arr[i++] = arr[j];
while(i<j && arr[i]<=pivot)
i++;
if(i<j)
arr[j
}//endwhile
arr[i] = pivot;
// end function
var pivotpos = i; //Partition(arr
QuickSort(arr
QuickSort(arr
} else
return;
return arr;
}
直接選擇排序基本思想
n個記錄的文件的直接選擇排序可經過n
①初始狀態
②第
在無序區R[
……
③第i趟排序
第i趟排序開始時
這樣
算法描述
function SelectSort(arr) { //選擇排序
var st = new Date();
var temp;
for(var i=
var k = i;
for(var j=i+
if((arr[j]) < (arr[k]))
k = j;
}
if (k != i){
temp = arr[i];
arr[i] = arr[k];
arr[k] = temp;
}
}
status = (new Date()
return arr;
}
<style>
fieldset {
font
padding:
width:
margin:auto;
}
input {
font
font
}
</style>
<title>排序</title><h
<legend>插入排序</legend><p><b>直接插入排序</b>
請輸入一段要排序的字符
<input name=insert type=text size=
<br><input type=button value=
<br><input type=button value=
<input name=bubble type=text size=
<br><input type=button value=
<br><input type=button value=
<legend>選擇排序</legend><b>直接選擇排序</b><br>
<input name=select
<br><input type=button value=
function InsertSort(arr) { //插入排序
var st = new Date();
var temp
for(var i=
if((arr[i]) < (arr[i
temp = arr[i];
j = i
do {
arr[j+
j
}
while (j>
arr[j+
}//endif
}
status = (new Date()
return arr;
}
function ShellSort(arr) { //插入排序
var st = new Date();
var increment = arr
do {
increment = (increment/
arr = ShellPass(arr
}
while (increment >
status = (new Date()
return arr;
}
function ShellPass(arr
var temp
for(var i=d; i<arr
if((arr[i]) < (arr[i
temp = arr[i]; j = i
do {
arr[j+d] = arr[j];
j = j
}
while (j>
arr[j+d] = temp;
}//endif
}
return arr;
}
function BubbleSort(arr) { //交換排序
var st = new Date();
var temp;
var exchange;
for(var i=
exchange = false;
for(var j=arr
if((arr[j+
temp = arr[j+
arr[j+
arr[j] = temp;
exchange = true;
}
}
if(!exchange) break;
}
status = (new Date()
return arr;
}
function QuickSortDemo(arr) {
var st = new Date();
var result = QuickSort(arr);
status = (new Date()
return result;
}
function QuickSort(arr) { //交換排序
if (arguments
var low = arguments[
var high = arguments[
} else {
var low =
var high = arr
}
if(low < high){
// function Partition
var i = low;
var j = high;
var pivot = arr[i];
while(i<j) {
while(i<j && arr[j]>=pivot)
j
if(i<j)
arr[i++] = arr[j];
while(i<j && arr[i]<=pivot)
i++;
if(i<j)
arr[j
}//endwhile
arr[i] = pivot;
// end function
var pivotpos = i; //Partition(arr
QuickSort(arr
QuickSort(arr
} else
return;
return arr;
}
/*function Partition(arr
var pivot = arr[i];
while(i<j) {
while(arr[j]>=pivot)
j
if(i<j)
arr[i++] = arr[j];
while(arr[i]<=pivot)
i++;
if(i<j)
arr[j
}
arr[i] = pivot;
return arr;
}*/
function SelectSort(arr) { //選擇排序
var st = new Date();
var temp;
for(var i=
var k = i;
for(var j=i+
if((arr[j]) < (arr[k]))
k = j;
}
if (k != i){
temp = arr[i];
arr[i] = arr[k];
arr[k] = temp;
}
}
status = (new Date()
return arr;
}
function unicode(str) {//求字符串的unicode碼
var uni=
for(var i=
uni += str
}
return uni;
}
</script>
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19224.html