各种简单的算法小例子

作者: 岛屿失梦w | 来源:发表于2016-12-01 21:39 被阅读164次

    1.分治去重(数组去重:一分为二,left递归去重,right递归去重,循环right,如果不在left里,加到left,返回left)

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title></title>
    <style></style>
    <script>
    function findInArray(n,arr){
    for(var i = 0; i<arr.length; i++){
    if(arr[i]==n){return true;}
    }
    return false;
    }
    function removeDup(arr,s,e){
    if(s>e){return [];}
    else if(s==e){
    return [arr[s]];
    }
    var c = Math.floor((s+e)/2);
    var left = removeDup(arr,s,c);
    var right = removeDup(arr,c+1,e);
    for(var i=0; i<right.length; i++){
    if(!findInArray(right[i],left)){
    left.push(right[i]);
    }
    }
    return left;
    }
    var arr = [28,19,48,29,28,18,48];
    document.write(removeDup(arr,0,arr.length-1));
    </script>
    </head>
    <body>
    </body>
    </html>
    

    2.分治找最小(找最小值:一分为二,left=递归左最小,right=递归右最小,比较left和right返回)

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>智能社</title>
    <style></style>
    <script>
    function findMin(arr,s,e){
    if(s>e){return false;}
    else if (s == e){
    return arr[s];
    }
    var c = Math.floor((s+e)/2);
    var left = findMin(arr,s,c);
    var right = findMin(arr,c+1,e);
    if(left < right){
    return left;
    }
    else{
    return right;
    }
    }
    var arr = [28,19,48,29,-39,18,-100];
    alert(findMin(arr,0,arr.length-1));
    </script>
    </head>
    <body>
    </body>
    </html>
    

    3.冒泡排序(比较相领的两个值,如果后面的小于前面的,就交换位置。)

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title></title>
    <style></style>
    <script>
    function bubbleSort(arr){
    for(var i = 0; i<arr.length; i++){
    for(var j = 0; j<arr.length-i-1; j++){
    if(arr[j] > arr[j+1]){
    var tmp = arr[j];
    arr[j] = arr[j+1];
    arr[j+1] = tmp;
    }
    }
    }
    return arr;
    }
    var arr=[18,38,29,48,-98,27];
    document.write(bubbleSort(arr));
    </script>
    </head>
    <body>
    </body>
    </html>
    

    4.选择排序(从剩余的数据中,找最小的,放到前面(和当前交换位置)。)

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title></title>
    <style></style>
    <script>
    function selectionSort(arr){
    for(var i = 0; i<arr.length; i++){
    for(var j = i+1; j<arr.length; j++){
    if(arr[i] > arr[j]){
    var tmp = arr[j];
    arr[j] = arr[i];
    arr[i] = tmp;
    }
    }
    }
    return arr;
    }
    var arr=[18,38,29,348,-198,29];
    document.write(selectionSort(arr));
    </script>
    </head>
    <body>
    </body>
    </html>
    

    5.归并排序(分治法,二分法,左边排序自己,右面排序自己,每次比较左右数组第一个,小的扔到新的数组里面。)

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title></title>
    <style></style>
    <script>
    function mergeSort(arr,s,e){
    if(s>e){return [];}
    else if(s==e){
    return [arr[s]];
    }
    var c = Math.floor((s+e)/ 2);
    var left = mergeSort(arr,s,c);
    var right = mergeSort(arr,c+1,e);
    var result = [];
    while(left.length || right.length){
    if(left[0] > right[0]){
    result.push(right.shift());
    }else{
    result.push(left.shift());
    }
    if(left.length==0){
    result=result.concat(right);
    break;
    }
    else if(right.length == 0){
    result=result.concat(left);
    break;
    }
    }
    return result;
    }
    var arr=[18,38,29,348,-198,29];
    document.write(mergeSort(arr,0,arr.length-1));
    </script>
    </head>
    <body>
    </body>
    </html>
    

    6.快速排序(二分法,中间一刀,两个结果数组,分别放左右,递归调用,连接数组,c=arr.splice(cIndex,1),c[0]是中间数组。)

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title></title>
    <style></style>
    <script>
    function quickSort(arr){
    if(arr.length == 0){return [];}
    var cIndex = Math.floor(arr.length/2);
    var c = arr.splice(cIndex,1);
    var left = [];
    var right = [];
    for(var i = 0; i<arr.length; i++){
    if(arr[i]<c[0]){
    left.push(arr[i]);
    }
    else{
    right.push(arr[i]);
    }
    }
    return quickSort(left).concat(c,quickSort(right));
    }
    var arr=[18,38,129,348,-198,209];
    document.write(quickSort(arr));
    </script>
    </head>
    <body>
    </body>
    </html>
    

    相关文章

      网友评论

      本文标题:各种简单的算法小例子

      本文链接:https://www.haomeiwen.com/subject/xckfmttx.html