0

JavaScript- Sorting Number

Hi, I want to ask how to sort the number from lowest to highest without using sort() syntax. Can anybody show me the logic? Thanks

1st May 2020, 1:14 AM
Munawirul Hadi
Munawirul Hadi - avatar
3 Respostas
+ 4
Here is a simple algorithm to do it : 1) Make an empty array to store the sorted element 2) Make a copy of the main array (The array to be sorted) 3) Loop through all the elements of the main array 4) Find the minimum of the copied array 5) Push it to the empty array 6) Delete the corresponding minimum from the copied array! Check out the demonstration : https://code.sololearn.com/WcGDFzcueHbf/?ref=app
1st May 2020, 1:40 AM
Arb Rahim Badsa
Arb Rahim Badsa - avatar
+ 3
There is about a hundred ways to do it, google "sorting algorithms", or bubblesort, insertion sort, selection sort, heapsort, quicksort, timsort, bucketsort... This has also been asked many times so you will find many answers in the Sololearn Q&A section. Heres a simple but inefficient algorithm, selection sort: Imagine a big unsorted pile of lego bricks of different sizes. 1. In your pile, look for the biggest brick. 2. Put it to the side. 3. Go to step 1 A less simple and more efficient one, quicksort: 1. Take a random brick out of your lego pile, we call it the "pivot" 2. Make a pile left of the pivot where you put all bricks that are smaller than the pivot 3. Make a pile right of the pivot with all the bricks larger than the pivot 4. Take the left pile and go to step 1 5. Take the right pile and go to step 1
1st May 2020, 1:27 AM
Schindlabua
Schindlabua - avatar