zoukankan      html  css  js  c++  java
  • 排序:冒泡和改进

    //冒泡排序
    function bubble_sort(array){
    var arr = array.concat();
    var len = arr.length;
    for(var i = len; i > 0; i--){
    var key = 0;
    while(key < i){
    if(arr[key] > arr[key+1]){
    var temp = arr[key];
    arr[key] = arr[key+1];
    arr[key+1] = temp;
    }
    key++;
    }
    }
    alert(arr);
    }
    //改进1:没有发生交换则代表排序完成
    function bubble_sort_1(array){
    var arr = array.concat();
    var len = arr.length;
    var flag;
    for(var i = len; i > 0; i--){
    var key = 0;
    flag = 0;
    while(key < i){
    if(arr[key] > arr[key+1]){
    var temp = arr[key];
    arr[key] = arr[key+1];
    arr[key+1] = temp;
    flag = 1;
    }
    key++;
    }
    if(flag == 0){break;}
    }
    alert(arr);
    }
    //改进:一趟同时冒泡大小两种
    function bubble_sort_2(array){
    var arr = array.concat();
    var len = arr.length;
    var front = 0;
    var back = len - 1;
    while(front < back){
    for(var i = front; i < back; i++){
    if(arr[i] > arr[i+1]){
    var temp = arr[i];
    arr[i] = arr[i+1];
    arr[i+1] = temp;
    }
    }
    back--;
    for(var j = back; j >= 0; j--){
    if(arr[j-1] > arr[j]){
    var temp = arr[j];
    arr[j] = arr[j-1];
    arr[j-1] = temp;
    }
    }
    front++;
    }
    alert(arr);
    }
  • 相关阅读:
    gym102215题解
    Codeforces6E_Exposition
    Codeforces893F_Subtree Minimum Query
    是男人就过八题A_A String Game
    gym101666题解
    gym102201E_Eat Economically
    gym102346题解
    C++输入函数用法
    迷宫问题(BFS+保存路径) POJ No.3984
    【BFS】迷宫的最短路径问题
  • 原文地址:https://www.cnblogs.com/zawjdbb/p/6663749.html
Copyright © 2011-2022 走看看