快速排序过程数据演示:
function CArray(numElements) {
this.dataStore = [];
this.numElements = numElements;
this.prints = prints;
this.setData = setData;
//this.bubbleSort = bubbleSort;
this.selectionSort = selectionSort;
}
function setData() {
for ( var i = 0; i < this.numElements; ++i) {
this.dataStore[i] = Math.floor(Math.random()
* (this.numElements + 1));
}
}
function prints() {
for ( var i = 0; i < this.dataStore.length; ++i) {
document.write(this.dataStore[i] + " ");
if (i > 0 & i % 10 == 0) {
document.write("<br />");
}
}
}
function swap(arr, index1, index2) {
var temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
}
var numElements = 100;
var myNums = new CArray(numElements);
myNums.setData();
function selectionSort() {
var min;
for ( var outer = 0; outer < this.dataStore.length - 1; ++outer) {
min = outer;
for ( var inner = outer + 1; inner < this.dataStore.length; ++inner) {
if (this.dataStore[inner] < this.dataStore[min]) {
min = inner;
}
}
swap(this.dataStore, outer, min);
}
}
myNums.selectionSort();
myNums.prints();