PHP:
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
| <?
function Array_quickSort(a, low, high) {
if(low + 10 > high) {
this.bubbleSort(a, low, high);
}else{
var middle = Math.floor((low + high) / 2);
if(a[middle] < a[low]) this.swap(a, low, middle);
if(a[high] < a[low]) this.swap(a, low, high);
if(a[high] < a[middle]) this.swap(a, middle, high);
this.swap(a, middle, high - 1);
var pivot = a[high - 1];
var i = low;
var j = high - 1;
while(true) {
while( a[++i] < pivot);
while( pivot < a[--j]);
if(i < j) {
this.swap(a, i, j);
}else{
break;
}
}
this.swap(a, i, high - 1);
this.quickSort(a, low, i - 1);
this.quickSort(a, i + 1, high);
}
}
function Array_sortIt() {
this.quickSort(this, 0, this.length - 1);
}
function Array_swap(a, i, j) {
var temp = a[i];
a[i] = a[j];
a[j] = temp;
}
function Array_bubbleSort(inputArray, start, rest) {
for (var i = rest - 1; i >= start; i--) {
for (var j = start; j <= i; j++) {
if (inputArray[j+1] < inputArray[j]) {
var tempValue = inputArray[j];
inputArray[j] = inputArray[j+1];
inputArray[j+1] = tempValue;
}
}
}
}
Array.prototype.bubbleSort = Array_bubbleSort;
Array.prototype.quickSort = Array_quickSort;
Array.prototype.swap = Array_swap;
Array.prototype.sortIt = Array_sortIt;
?> |
var getallen = new Array(8, 6, 7, 4, 3, 2, 9, 1, 5, 12, 13, 18, 19, 204, 123, 233, 123, 11, 1, 12, 332);
getallen.sortIt();
oef, in java was het makkelijker