Bucket sorting of javascript algorithm sorting


Event address: CSDN 21-day learning challenge

foreword

Many people have heard of the classic sorting algorithm, and many people may have used it, but there are also many people who have never heard of it. why? Now that we have more and more frameworks and dependent packages, we will be able to use the actual scene of sorting and encapsulate it into a function as a business. Therefore, some people only know the function but not its operating logic.

Based on the above, in order to better understand the operation logic of the function, I sorted out some operation rules of the basic sorting method, as well as some personal understanding, hoping to give you some help.

This article will cover bucket sorting. Bucket sorting is actually to put the data into different collections according to the rules, then sort the elements in each collection, and finally merge all the arrays into one;

bucket sort

Bucket sort is an upgraded version of counting sort. It makes use of the mapping relationship of functions, and the key to high efficiency lies in the determination of this mapping function;

Implementation principle of bucket sorting

  • Set a fixed positive integer as the number of buckets. We need to put the data into these buckets respectively. Generally, the default number of buckets is rounded down ((maximum value - minimum value)/array length) + 1;
  • Traverse the array, put the elements into the corresponding buckets according to a certain algorithm, the algorithm is generally, bucket position = round down ((element value - minimum value) / original array length;
  • Sort the elements in each bucket. At this time, you can borrow any sorting method you like, native, selection sorting, counting sorting, etc.;
  • Exclude empty buckets in the array;
  • Merge all buckets with data;

example:

the code

function bucketSort(array) {
    
    
  let min = Math.min(...array);
  let max = Math.max(...array);
  //初始化桶个数
  let buketNum = Math.floor((max - min) / array.length) + 1;
  let buckets = new Array(buketNum);
  //设置桶中的每个元素为数组
  for (let i = 0; i < buckets.length; i++) {
    
    
    buckets[i] = [];
  }
  console.log('new buckets', buckets);
  //将元素分到不同的桶中,元素在哪个桶中 =  (目标元素-最小值)/ 原数组长度
  for (let j = 0; j < array.length; j++) {
    
    
    buckets[Math.floor((array[j] - min) / buketNum)].push(array[j]);
  }
  console.log('buckets info', buckets);
  //对每个桶中的数据进行排序
  for (let k = 0; k < buckets.length; k++) {
    
    
    //array.sort()排序不会有人忘记了吧
    buckets[k].sort((a, b) => {
    
    
      return a - b;
    });
  }
  console.log('buckets sort', buckets);
  //聚合桶,flat()方法不会有人不知道吧
  let result = buckets.flat();
  console.log('bucketSort result:', result);
}

bucketSort([10, 9, 100, 20, 5, 1, 4, 100]);

image.png

the complexity

Time complexity: It is mainly related to the time complexity of sorting the data of each bucket, because the time complexity of other parts is O(n). Therefore, the more buckets are divided, the less data there is in each bucket, and the less time it takes to sort. But the corresponding space consumption will increase;
space complexity: O(n)

message

Since the time complexity and space complexity of bucket sorting are linear, the efficiency of bucket sorting is relatively stable; bucket sorting
can be said to be a standard space-for-time sorting method. Most of the time, we generally use the average number of buckets to operate instead of extreme cases.

What to get, what to give

Guess you like

Origin blog.csdn.net/Long861774/article/details/126436719