flash Sort Algorithm

The flash sort algorithm is an efficient sorting technique that is based on the concept of partitioning the array into a number of smaller segments and then sorting these segments independently. This algorithm is particularly effective when dealing with large datasets having a uniform distribution, as it takes advantage of the underlying structure of the data to significantly reduce the number of comparisons and movements required to sort the array. The main idea behind the flash sort algorithm is to estimate the position of each element in the sorted array, and then move the elements towards their estimated positions in a single pass through the array. The flash sort algorithm has three main steps: classification, permuting, and a final insertion sort. In the classification step, the range of the data is divided into a number of classes or buckets, and elements are assigned to these buckets based on their values. The permuting step involves swapping elements within and across the buckets to move them closer to their correct positions. This step uses a technique called "cyclic permutation" to minimize the number of swaps required. Finally, the insertion sort is applied to the partially sorted data within each bucket. This final step capitalizes on the fact that the data within each bucket is already partially sorted, allowing the insertion sort to perform much faster than it would on the unsorted data. Overall, the flash sort algorithm provides a highly efficient sorting technique for large datasets with a relatively uniform distribution.
/*
 * Flashsort is a distribution sorting algorithm showing linear computational complexity O(n) for uniformly distributed
 * data sets and relatively little additional memory requirement.
 * more information: https://en.wikipedia.org/wiki/Flashsort
 */

function flashSort (arr) {
  let max = 0; let min = arr[0]
  const n = arr.length
  const m = ~~(0.45 * n)
  const l = new Array(m)

  for (let i = 1; i < n; ++i) {
    if (arr[i] < min) {
      min = arr[i]
    }
    if (arr[i] > arr[max]) {
      max = i
    }
  }

  if (min === arr[max]) {
    return arr
  }

  const c1 = (m - 1) / (arr[max] - min)

  for (let k = 0; k < m; k++) {
    l[k] = 0
  }

  for (let j = 0; j < n; ++j) {
    const k = ~~(c1 * (arr[j] - min))
    ++l[k]
  }

  for (let p = 1; p < m; ++p) {
    l[p] = l[p] + l[p - 1]
  }

  let hold = arr[max]
  arr[max] = arr[0]
  arr[0] = hold

  // permutation
  let move = 0; let t; let flash
  let j = 0
  let k = m - 1

  while (move < (n - 1)) {
    while (j > (l[k] - 1)) {
      ++j
      k = ~~(c1 * (arr[j] - min))
    }
    if (k < 0) break
    flash = arr[j]
    while (j !== l[k]) {
      k = ~~(c1 * (flash - min))
      hold = arr[t = --l[k]]
      arr[t] = flash
      flash = hold
      ++move
    }
  }

  // insertion
  for (j = 1; j < n; j++) {
    hold = arr[j]
    let i = j - 1
    while (i >= 0 && arr[i] > hold) {
      arr[i + 1] = arr[i--]
    }
    arr[i + 1] = hold
  }
  return arr
}

const array = [3, 0, 2, 5, -1, 4, 1, -2]

// Array before Sort
console.log('-----before sorting-----')
console.log(array)
// Array after sort
console.log('-----after sorting-----')
console.log(flashSort(array))

LANGUAGE:

DARK MODE: