import console;
import time.performance;
bubbleSort = function(arr) {
var n = #arr;
var newn;
while(n > 1) {
newn = 1;
for(i=1;n-1;1) {
if(arr[i] > arr[i+1]) {
arr[i], arr[i+1] = arr[i+1], arr[i];
newn = i;
}
}
n = newn;
}
}
console.log("冒泡排序( 交换类换排序 )")
console.log("---------------------------")
var arr = {2,46,5,17,1,2,3,99,12,56,66,21};
t = time.performance.tick();
bubbleSort(arr);
console.log(time.performance.tick() - t);
console.dumpTable(arr)
console.pause(true);
import console;
import time.performance;
bubbleSort = function(arr) {
var n = #arr;
var swapped;
for(i=1;n;1) {
swapped = false;
for(j=1;n-i;1) {
if(arr[j] > arr[j+1]) {
arr[j],arr[j+1] = arr[j+1],arr[j];
swapped = true;
}
}
if(!swapped) break;
}
}
console.log("冒泡排序( 交换类换排序 )")
console.log("---------------------------")
var arr = {2,46,5,17,1,2,3,99,12,56,66,21,234,324,3,33,3424,324,234,234,234,45,34,234,45,46,3456,435,2122,5,67,438,89,11111,3246,34534534,346435643,345345,556555};
t = time.performance.tick();
bubbleSort(arr);
console.log(time.performance.tick() - t);
console.dumpTable(arr)
console.pause(true);
import console;
import time.performance;
quickSort = function(arr, low = 1, high) {
if (!high) high = #arr
if(low < high) {
var pivot = partition(arr, low, high);
quickSort(arr, low, pivot - 1);
quickSort(arr, pivot + 1, high);
}
return arr;
}
partition = function(arr, low, high) {
var pivot = arr[high];
var i = low - 1;
for(j = low; high - 1; 1) {
if(arr[j] <= pivot) {
i++;
arr[i], arr[j] = arr[j], arr[i];
}
}
arr[i + 1], arr[high] = arr[high], arr[i + 1];
return i + 1;
}
console.log("快速排序")
console.log("---------------------------")
var arr = {2,46,5,17,1,2,3,99,12,56,66,21,234,324,3,33,3424,324,234,234,234,45,34,234,45,46,3456,435,2122,5,67,438,89,11111,3246,34534534,346435643,345345,556555};
t = time.performance.tick();
p = quickSort(arr);
console.log(time.performance.tick() - t);
console.dumpTable(p)
console.pause(true);
import console;
import time.performance;
//冒泡排序函数
mergeSort = function(arr) {
if(#arr <= 1) return arr;
var mid = math.floor(#arr / 2);
var left = ..table.slice(arr, 1, mid);
var right = ..table.slice(arr, mid + 1);
left = mergeSort(left);
right = mergeSort(right);
return merge(left, right);
}
merge = function(left, right) {
var result = {};
var i = 1;
var j = 1;
while(i <= #left && j <= #right) {
if(left[i] <= right[j]) {
..table.push(result, left[i]);
i++;
}
else {
..table.push(result, right[j]);
j++;
}
}
while(i <= #left) {
..table.push(result, left[i]);
i++;
}
while(j <= #right) {
..table.push(result, right[j]);
j++;
}
return result;
}
console.log("合集排序")
console.log("---------------------------")
var arr = {2,46,5,17,1,2,3,99,12,56,66,21,234,324,3,33,3424,324,234,234,234,45,34,234,45,46,3456,435,2122,5,67,438,89,11111,3246,34534534,346435643,345345,556555};
t = time.performance.tick();
p = mergeSort(arr);
console.log(time.performance.tick() - t);
console.dumpTable(p)
console.pause(true);
还有啥效率更高的排序方式, 炎炎夏日, 夏日炎炎, 太热了,有空的跟帖讨论下