-1

Is javascript bubble sort the same as .sort((a,b) => a-b? Is it more efficient? Why? It seems the same, what is the difference? If different, when would I use bubble sort, when would .sort be best and why?

Say for use in a massive photo library using dates, what sorting method is the best to use in javascript?

I have never tried bubble sort, this (below) is on fcc, and I thought, what is the difference? I have never seen this used and .sort((a,b) => a-b does this, no?

I did read through all of the sort vs bubble sort on here asked before, they were mostly in different languages I also read the MDN docs and still wanted more clarity.

from freeCodeCamp

const bubbleSort = (arr) => {
  let swapped;

   do {
    swapped = false;
    for (let i = 0; i < arr.length - 1; i++) {
      if (arr[i] > arr[i + 1]) {
        let temp = arr[i];
        arr[i] = arr[i + 1];
        arr[i + 1] = temp;
        swapped = true;
      }
    }
  } while (swapped);

  return arr;
};
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Beary
  • 19
  • 6
  • 4
    The built-in `.sort()` is much, much faster than a bubble sort. There's no reason to implement your own sort other than as an exercise. – Pointy Mar 31 '23 at 12:12
  • 3
    BubbleSort is one of the worst sorting algorithms (based on speed O(n²) ). This might give you more insight in which algorithm is used by `.sort` https://stackoverflow.com/questions/234683/javascript-array-sort-implementation – MrSmith42 Mar 31 '23 at 12:15
  • 1
    *when would I use bubble sort* - never – Konrad Mar 31 '23 at 12:20
  • Thank you all, is .sort() the best for any amount of data then? – Beary Mar 31 '23 at 13:28

1 Answers1

0

You are confusing the comparator function with the sorting algorithm.

The comparator function is used to decide if one element is greater than, less than or equal to another element. The sorting algorithm changes a list by comparing pairs of elements and updating their position accordingly. The sorting algorithm determines run time efficiency, while the comparator determines sort order.

Efficiency of a sorting algorithm is usually thought of in regard to the length of the input array. All in all, you get three classes:

  • O(n log n) like Quick Sort, Merge Sort, Heap Sort - they manage to effectively half the problem with every iteration through the list
  • O(n²) like Selection Sort, Insertion Sort, Bubble Sort - they take one iteration for every sorted element
  • Those that are worse, like Stupid Sort or Bogosort - they are mostly of academic interest

While slower in general, some of the O(n²) sorters can reliably outperform O(n log n) sorters in specific scenarios, like when the input contains only one element out of order, which Insertion Sort will have sorted after the first iteration. Bubble Sort is amazing when you have to sort an array where elements are swapped pair-wise (like [2,1,4,3]), but this is not a common situation.

Modern sort implementations can test for those properties and select the best algorithm accordingly and do other clever stuff (like sorting in pages when data exceeds memory). So usually, you don't write your own sorting algorithm and don't even pick one, but leave it up to the language/system.

This is all in very broad strokes. Hope it still helps.

Moritz Ringler
  • 9,772
  • 9
  • 21
  • 34