标签:blog http io os ar java for strong sp
基本准备:
function CArray(numElems) {
this.dataStore = [];
this.pos = 0;
this.numElems = numElems;
this.insert = insert;
this.toString = toString;
this.clear = clear;
this.setData = setData;
this.swap =swap;
for(var i = 0; i < numElems; ++i) {
this.dataStore[i] = i;
}
}
function setData() {
for(var i = 0; i < this.numElems; ++i) {
this.dataStore[i] = Math.floor(Math.random() * 100);//0-99
}
}
function clear() {
for (var i = 0; i < this.numElems; ++i) {
this.dataStore[i] = 0;
}
}
function insert(elem) {
this.dataStore[this.pos++] = elem;
}
function toString() {
var str = "";
for(var i = 0; i < this.dataStore.length; ++i) {
str += this.dataStore[i] + " ";
if( i > 0 && (i+1) %10 === 0) {
str += "\n";
}
}
return str;
}
function swap(arr,index1,index2) {
var temp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = temp;
}
基本排序算法:
效果:

function bubbleSort() {
var numElems = this.dataStore.length;
var temp;
for(var outer = numElems; outer > 1 ; --outer) {
for(var inner = 0; inner < outer; ++inner) {
if(this.dataStore[inner] > this.dataStore[inner + 1]) {
swap(this.dataStore,inner,inner + 1);
}
}
}
}
标签:blog http io os ar java for strong sp
原文地址:http://www.cnblogs.com/jinkspeng/p/4035146.html