-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCArray.js
More file actions
62 lines (53 loc) · 1.36 KB
/
Copy pathCArray.js
File metadata and controls
62 lines (53 loc) · 1.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// 数组测试平台
function CArray(numElements) {
this.dataStore = [];
this.pos = 0;
this.numElements = numElements;
this.insert = insert;
this.toString = toString;
this.clear = clear;
this.setData = setData;
this.swap = swap;
// this.bubbleSort = bubbleSort;
// this.selectionSort = selectionSort;
// this.insertionSort = insertionSort;
// this.shellSort = shellSort;
// this.mergeSort = mergeSort;
this.quickSort = quickSort;
for (var i = 0; i < numElements; i++) {
this.dataStore[i] = i;
}
}
function insert(element) {
this.dataStore[this.pos++] = element;
}
function toString() {
var res = '';
for (var i = 0; i < this.dataStore.length; i++) {
res += this.dataStore[i] + ' ';
if ((i + 1) % 10 == 0) {
res += '\n';
}
}
return res;
}
function clear() {
for (var i = 0; i < this.dataStore.length; i++) {
this.dataStore[i] = 0;
}
}
function setData() {
for (var i = 0; i < this.numElements; i++) {
this.dataStore[i] = Math.floor(Math.random() * (this.numElements + 1));
}
}
function swap(arr, index1, index2) {
var tmp = arr[index1];
arr[index1] = arr[index2];
arr[index2] = tmp;
}
// // test
// var numElements = 100;
// var myNums = new CArray(numElements);
// myNums.setData();
// print(myNums.toString());