排序和搜索算法

冒泡排序:比较任何两个相邻的项,如果左边比右边大,则交换它们(从小到大排序)。

function ArrayList(){
    let array = [];
    this.insert = function (item) {
        array.push(item);
    };
    this.toString = function () {
        return array.join();
    };
    this.bubbleSort = function () {
        let length = array.length;
        for(let i = 0; i<length; i++){
            for(let j = 0; j<length - 1; j++){
                if(array[j] > array[j+1]){
                    swap(j, j+1);  //交换索引为j 和j+1 的数组元素
                }
            }
        }
    };

    let swap = function (index1, index2) {
        let temp = array[index1];
        array[index1] = array[index2];
        array[index2] = temp;
    } ;

}

冒泡排序的工作过程:数组 【5,4,3,2,1】

改进的冒泡排序:冒泡排序每一轮的比较都会排好一个数据,无论是从小到大还是从大到小排序,所以以后的每一轮都可以比前一轮少比较一次

this.modifiedBubbleSort = function () {
        let length = array.length;
        for(let i = 0; i<length; i++){
            for(let j = 0; j < length-i-1; j++){ //内循环减去外循环已跑过的轮数
                if(array[j] > array[j+1]){
                    swap(j, j+1);
                }
            }
        }
    };

或者不应该叫改进,冒泡排序本来应该就是那样的。两者的算法复杂度都是O(n*n);

 

选择排序:大致思路是找到数据结构中的最小值并将其放在第一位,接着找到第二小的值并将其放在第二位,依次类推

this.selectionSort = function () {
        let length = array.length;
        let indexMin;
        for(let i = 0; i<length; i++){
            indexMin = i;
            for(let j = i; j<length; j++){
                if(array[indexMin] > array[j]){
                    indexMin = j;
                }
            }
            if(i !== indexMin){
                swap(i, indexMin);
            }
        }
    };

选择排序的工作流程:

 

插入排序 :插入排序每次排一个数组项,假定第一项已经排好了,与第二项进行比较,第二项应该在第一项的前面还是后面,然后第三项插进来在进行比较,应该是插进来第一还是第二还是第三的位置。

this.insertionSort = function () {
        let length = array.length,
            j,temp;
        for(let i = 1; i<length; i++){
            j = i;
            temp = array[i];
            while(j>0 && array[j-1] > temp){
                array[j] = array[j-1];
                j--;
            }
            array[j] = temp;
        }
    };

流程:

 

归并排序:分治算法,将原始数组分割成较小的数组,直到每个小数组只有一个位置,接着将小数组归并成较大的数组,直到最后只有一个排序完毕的数组:

 this.mergeSort = function(){
        array = mergeSortRec(array);
    };

    let mergeSortRec = function(array){

        let length = array.length;

        if(length === 1) {
            console.log(array);
            return array;
        }

        let mid = Math.floor(length / 2),
            left = array.slice(0, mid),
            right = array.slice(mid, length);

        return merge(mergeSortRec(left), mergeSortRec(right));
    };

    let merge = function(left, right){
        let result = [],
            il = 0,
            ir = 0;

        while(il < left.length && ir < right.length) {

            if(left[il] < right[ir]) {
                result.push(left[il++]);
            } else{
                result.push(right[ir++]);
            }
        }

        while (il < left.length){
            result.push(left[il++]);
        }

        while (ir < right.length){
            result.push(right[ir++]);
        }

        console.log(result);

        return result;
    };

流程:

 

快速排序:比较常用

this.quickSort = function(){
        quick(array,  0, array.length - 1);
    };

    let partition = function(array, left, right) {

        let pivot = array[Math.floor((right + left) / 2)],
            i = left,
            j = right;

        console.log('pivot is ' + pivot + '; left is ' + left + '; right is ' + right);

        while (i <= j) {
            while (array[i] < pivot) {
                i++;
                console.log('i = ' + i);
            }

            while (array[j] > pivot) {
                j--;
                console.log('j = ' + j);
            }

            if (i <= j) {
                console.log('swap ' + array[i] + ' with ' + array[j]);
                swapQuickStort(array, i, j);
                i++;
                j--;
            }
        }

        return i;
    };

    let swapQuickStort = function(array, index1, index2){
        let aux = array[index1];
        array[index1] = array[index2];
        array[index2] = aux;
    };

    let quick = function(array, left, right){

        let index;

        if (array.length > 1) {

            index = partition(array, left, right);

            if (left < index - 1) {
                quick(array, left, index - 1);
            }

            if (index < right) {
                quick(array, index, right);
            }
        }
        return array;
    };

    

 

顺序搜索算法:顺序迭代整个数组,并将每个数组元素与搜索项作比较

 this.findMaxValue = function(){
        let max = array[0];
        for (let i=1; i<array.length; i++){
            if (max < array[i]){
                max = array[i];
            }
        }

        return max;
    };

    this.findMinValue = function(){
        let min = array[0];
        for (let i=1; i<array.length; i++){
            if (min > array[i]){
                min = array[i];
            }
        }

        return min;
    };

 

二分搜索:类似数学上的夹逼法则:

this.sequentialSearch = function(item){

        for (let i=0; i<array.length; i++){
            if (item === array[i]){
                return i;
            }
        }

        return -1;
    };

   

    this.binarySearch = function(item){
        this.quickSort();

        let low = 0,
            high = array.length - 1,
            mid, element;

        while (low <= high){
            mid = Math.floor((low + high) / 2);
            element = array[mid];
            console.log('mid element is ' + element);
            if (element < item) {
                low = mid + 1;
                console.log('low is ' + low);
            } else if (element > item) {
                high = mid - 1;
                console.log('high is ' + high);
            } else {
                console.log('found it');
                return mid;
            }
        }
        return -1;
    };

}

(看完了相关的数据结构和基本算法,现在又忘记得差不多了,这些当作复盘,也是为了方便以后复习使用)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值