排序效率探究

netfox 3月前 429

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);

还有啥效率更高的排序方式, 炎炎夏日, 夏日炎炎, 太热了,有空的跟帖讨论下

最新回复 (4)
  • Viewer8122 3月前
    0 2
    支持,学习了。
  • amin 19天前
    0 3
    此楼层已删除
  • amin 19天前
    0 4
    import console;
    import time.performance;
    
    // 定义一个数组
    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};
    
    // 使用 table.sort 对数组进行排序
    bubbleSort=function(data){
    	table.sort(data);
    }
    
    console.log("冒泡排序( 交换类换排序 )")
    console.log("---------------------------")
    t = time.performance.tick();
    
    bubbleSort(arr); 
    console.log(time.performance.tick() - t); 
    // 输出排序后的数组
    console.dumpTable(arr)
    
    console.pause();


  • amin 19天前
    0 5

返回