[Sort Algorithm] js implements commonly used sorting algorithms

Tip: After the article is written, the table of contents can be automatically generated. How to generate it can refer to the help document on the right


Preface

js implements common algorithms such as "bubble sort", "insertion sort", "Hill sort", and "quick sort"


1. Big O notation

  1. Big O notation: a roughly qualitative function that measures the time complexity of an algorithm (the degree to which the number of algorithm operations increases as the data increases)
  2. Main function updates (in order of performance deterioration): O(1)>O(log(n))>O(n)> O(nlog(n)> O(n^2) O(2^n)
  3. Big O notation big inference rules: 1. If it is a constant, abstract it to 1. 2. If it is a polynomial, keep the highest term. 3. If the coefficient of the highest term is not 1, abstract it to 1.

2. Sorting algorithm

// 排序算法
function ArratList() {
    
    
    this.arr = [];
    this.getArr = function() {
    
    
        return this.arr;
    };
    // 交换位置
    this.swap = function(n, m) {
    
    
        let temp = this.arr[n];
        this.arr[n] = this.arr[m];
        this.arr[m] = temp;
    }
    ArratList.prototype.insert = function(item) {
    
    
        this.arr.push(item);
    };
    ArratList.prototype.string = function() {
    
    
        return this.arr.join('-');
    };
}

1. Bubble sort

  • 1. Bubble sort code implementation: The core idea is to move the largest item to the end in one comparison
  • 2. Time complexity of bubble sort (number of comparisons): (n-1)(n-2)…1 = n(n-1)/2 => n^2 ==> Number of exchanges n(n-1 )/4 => n^2
ArratList.prototype.bubblesort = function() {
    
    
    var length = this.arr.length;
    for(var j = length - 1; j >= 0; j--) {
    
    
        for(var i = 0; i < j; i++) {
    
    
            if (this.arr[i] > this.arr[i + 1]) {
    
    
                this.swap(i, i + 1);
            }
        }
    }
}

2.Select sort

  • Selection sorting code implementation: The core idea is to select the smallest item and put it first
  • Time complexity of selection sort (number of comparisons): (n-1)(n-2)…1 = n(n-1)/2 => n^2 ==> Number of exchanges (n-1) => n

The code is as follows (example):

ArratList.prototype.selectSort = function() {
    
    
    var length = this.arr.length;
    for(var j = 0; j < length; j++) {
    
    
        var min = j;
        for(var i = j + 1; i < length; i++) {
    
    
            if (this.arr[min] > this.arr[i]) {
    
    
                min = i;
            }
        };
        this.swap(j, min);
    }
}

3. Insertion sort

  • Insertion sort code implementation: the core idea is local ordering
  • The time complexity of insertion sort (maximum number of comparisons) is n(n-1)/2 => n^2. The maximum number of copies is n(n-1)/2 => n^2. The performance consumption of copying is small.

The code is as follows (example):

ArratList.prototype.insertSort = function() {
    
    
    var length = this.arr.length;
    for(var i = 1; i < length; i++) {
    
    
        var j = i;
        var temp = this.arr[i];
        while(this.arr[j - 1] > temp && j > 0) {
    
    
            this.arr[j] = this.arr[j - 1];
            j--;
        };
       this.arr[j] = temp;
    }
};

4. Hill sorting is an optimization of insertion sort (grouped incremental insertion sort)

  • Hill sorting code implementation: The core idea is to group by increment and insert sort each group separately.
  • The time complexity of Hill sort is higher than that of insertion sort
  • The increment is n / 2 n is the number of elements to be sorted

The code is as follows (example):


ArratList.prototype.shellSort = function() {
    
    
    let length = this.arr.length;
    // 获取增量
    let gap = Math.floor(length / 2);
    while(gap >= 1) {
    
    
        for(var i = gap; i < length; i++) {
    
    
            let j = i;
            let temp = this.arr[i];
            while(this.arr[j - gap] > temp && j > gap - 1) {
    
    
                this.arr[j] = this.arr[j - gap];
                j -= gap;
            };
            this.arr[j] = temp;
        }
        gap = Math.floor(gap / 2);
    }
}



Summarize

Guess you like

Origin blog.csdn.net/qq_48896417/article/details/126327100