Home > Web Front-end > Front-end Q&A > How to sort a set of numbers in order of size in javascript

How to sort a set of numbers in order of size in javascript

PHPz
Release: 2023-04-21 09:46:09
Original
1188 people have browsed it

JavaScript is a widely used programming language that can implement many functions in web development, game development, mobile development and other fields. In the actual development process, sorting algorithms are one of the common programming problems. How to use JavaScript to sort a set of numbers in order of size?

JavaScript provides a variety of sorting algorithms to solve this problem. This article will introduce several commonly used sorting algorithms.

Bubble sort

Bubble sort is a simple sorting algorithm. Its principle is to compare two adjacent numbers. If the former number is greater than the latter number, then Swap the two numbers and repeat this process until all the numbers are in order of size.

The following is the JavaScript implementation of bubble sorting:

function bubbleSort(arr) {
  var len = arr.length;
  for (var i = 0; i < len - 1; i++) {
    for (var j = 0; j < len - 1 - i; j++) {
      if (arr[j] > arr[j + 1]) {
        var temp = arr[j];
        arr[j] = arr[j + 1];
        arr[j + 1] = temp;
      }
    }
  }
  return arr;
}

var arr = [5, 3, 8, 4, 2];
console.log(bubbleSort(arr)); // [2, 3, 4, 5, 8]
Copy after login

Selection sorting

Selection sorting is another simple sorting algorithm. Its principle is to sort the array in the array to be sorted. Find the smallest element and place it first, then find the smallest element among the remaining elements and place it after the sorted elements in turn until all elements are arranged.

The following is the JavaScript implementation of selection sort:

function selectionSort(arr) {
  var len = arr.length;
  for (var i = 0; i < len - 1; i++) {
    var minIndex = i;
    for (var j = i + 1; j < len; j++) {
      if (arr[j] < arr[minIndex]) {
        minIndex = j;
      }
    }
    var temp = arr[i];
    arr[i] = arr[minIndex];
    arr[minIndex] = temp;
  }
  return arr;
}

var arr = [5, 3, 8, 4, 2];
console.log(selectionSort(arr)); // [2, 3, 4, 5, 8]
Copy after login

Insertion sort

Insertion sort is a simple and efficient sorting algorithm. Its principle is to insert the elements to be sorted into already The appropriate position within the sorted element. Insertion sorting is divided into two types: direct insertion sorting and Hill sorting. Below we will introduce the implementation of direct insertion sorting.

The following is the JavaScript implementation of insertion sort:

function insertionSort(arr) {
  var len = arr.length;
  var preIndex, current;
  for (var i = 1; i < len; i++) {
    preIndex = i - 1;
    current = arr[i];
    while (preIndex >= 0 && arr[preIndex] > current) {
      arr[preIndex + 1] = arr[preIndex];
      preIndex--;
    }
    arr[preIndex + 1] = current;
  }
  return arr;
}

var arr = [5, 3, 8, 4, 2];
console.log(insertionSort(arr)); // [2, 3, 4, 5, 8]
Copy after login

Quick sort

Quick sort is one of the most commonly used sorting algorithms. Its principle is to sort the elements according to the reference elements. Divide it into two parts, the left part is smaller than the base element, and the right part is larger than the base element, and then recursively sort the left and right parts.

The following is the JavaScript implementation of quick sort:

function quickSort(arr, left, right) {
  var len = arr.length,
    partitionIndex;
  left = typeof left != "number" ? 0 : left;
  right = typeof right != "number" ? len - 1 : right;

  if (left < right) {
    partitionIndex = partition(arr, left, right);
    quickSort(arr, left, partitionIndex - 1);
    quickSort(arr, partitionIndex + 1, right);
  }
  return arr;
}

function partition(arr, left, right) {
  var pivot = left,
    index = pivot + 1;
  for (var i = index; i <= right; i++) {
    if (arr[i] < arr[pivot]) {
      swap(arr, i, index);
      index++;
    }
  }
  swap(arr, pivot, index - 1);
  return index - 1;
}

function swap(arr, i, j) {
  var temp = arr[i];
  arr[i] = arr[j];
  arr[j] = temp;
}

var arr = [5, 3, 8, 4, 2];
console.log(quickSort(arr)); // [2, 3, 4, 5, 8]
Copy after login

Summary

The above are several commonly used JavaScript sorting algorithms, which are bubble sort, selection sort, and insertion sort. and quick sort. In the actual development process, different sorting algorithms can be selected according to different scenarios to achieve optimal efficiency results.

The above is the detailed content of How to sort a set of numbers in order of size in javascript. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template