JS array sorting (bubble, fast, insert)

1. Bubble sort

Compare two adjacent elements in the array, move the larger (smaller) number to the end (start) of the array through pairwise comparison, execute an inner loop to determine a maximum (minimum) number, and the outer layer Loop from the end (start) of the array to the beginning (end)
Insert picture description here

function MaoPaoSort(arr){
    
    
        for(var i = 0;i<arr.length-1;i++) {
    
    
            for(var j = 0;j<arr.length-i-1;j++){
    
    
                if(arr[j]>arr[j+1]){
    
    
                    //把大的数字放到后面
                    var str = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = str;
                }
            }
        }
    }
    var arr = [3,5,1,2,7,8,4,5,3,4];
    //console.log(arr);[3,5,1,2,7,8,4,5,3,4];
    MaoPaoSort(arr);
    //console.log(arr);[1, 2, 3, 3, 4, 4, 5, 5, 7, 8]


2. Insertion sorting method (cutting the queue)

Divide the array to be sorted into two parts, and insert the element with the smallest index from the latter part into the appropriate position of the former part each time
Insert picture description here

  • Starting from the first element, the element can be considered to have been sorted;
  • Take out the next element and scan from back to front in the sorted sequence of elements;
  • If the element (sorted) is larger than the new element, move the element to the next position;
  • Repeat step 3 until you find the position where the sorted element is less than or equal to the new element;
  • After inserting the new element into this position;
  • Repeat steps 2~5.
function InsertSort(arr) {
    
    
  let len = arr.length;
  let preIndex, current;
  for (let i = 1; i < len; i++) {
    
    
    preIndex = i - 1;
    current = arr[i];
    while (preIndex >= 0 && current < arr[preIndex]) {
    
    
      arr[preIndex + 1] = arr[preIndex];
      preIndex--;
    }
    arr[preIndex + 1] = current;
  }
  return arr;
}
 
 
var arr = [3,5,7,1,4,56,12,78,25,0,9,8,42,37];
InsertSort(arr);


3. Quick sort

After reading the above things, I don’t know if you have found that if the amount of data is too large and the array is more complicated in actual work, two traversals will bring performance problems at the same time. Don’t panic, we can still use Quick sort method to solve,快速排序对冒泡排序的一种改进

The realization idea is that the sorting problem of an array is regarded as the sorting problem of two small arrays, with a number as the benchmark (the number in the middle), the smaller than the benchmark is placed on the left, and the larger than the benchmark is placed on the right, and each A small array can continue to be regarded as two smaller arrays, recursively, until the maximum size of the array is 2

function quickSort(arr){
    
    
   //如果数组长度小于1,没必要排序,直接返回
   if(arr.length<=1) return arr;
   //pivot 基准索引,长度的一半
   let pivotIndex = Math.floor(arr.length/2);//奇数项向下取整
   //找到基准,把基准项从原数组删除
   let pivot = arr.splice(pivotIndex,1)[0];
   //定义左右数组
   let left = [];
   let right = [];
   //把比基准小的放left,大的放right
   arr.forEach(element => {
    
    
       if(element<pivot){
    
    
           left.push(element)
       }else{
    
    
           right.push(element)
       }
   });
   return quickSort(left).concat([pivot],quickSort(right))
}
     
     
var arr=[4,56,3,67,44,5,66];
console.log(quickSort(arr));//[3, 4, 5, 44, 56, 66, 67]

Guess you like

Origin blog.csdn.net/zxlong020/article/details/108531423