1

I know various ways to sort an array, but when the thing comes like i will have to manage time and space complexity i just don’t know what to do how to do

Help me to sort an array in better way with less time and space complexity

Assuming like i have 10k elements in an array and i will have to sort that array, which sorting algorithm will be better?

 var Arr = [ 3, 4,1,2, 5,7];
     Arr.sort((a,b)=> a-b);
     console.log('Arr.sort------',Arr);//[ 1, 2, 3, 4, 5, 7 ]
    
    OR
    
    for (var i = 1; i < Arr.length; i++){ 
      console.log('i:',i)
        for (var j = 0; j < i; j++){
          console.log(j)
            if (Arr[i] < Arr[j]) {
              var x = Arr[i];
              Arr[i] = Arr[j];
              Arr[j] = x;
            }
        }
    }
    
    console.log('sort------',Arr);//[ 1, 2, 3, 4, 5, 7 ]
OR
function selectionSort(arr) {
   var minIdx, temp,
       len = arr.length;
   for (var i = 0; i < len; i++) {
       minIdx = i;
       for (var j = i + 1; j < len; j++) {
           if (arr[j] < arr[minIdx]) {
               minIdx = j;
           }
       }
       temp = arr[i];
       arr[i] = arr[minIdx];
       arr[minIdx] = temp;
   }
   return arr;
}
var selectionSortArr=Arr
var ddd = selectionSort(selectionSortArr);
console.log('sort -------',selectionSortArr);//[ 1, 2, 3, 4, 5, 7 ]
Ishwar Chandra Tiwari
  • 5,099
  • 6
  • 22
  • 41
  • 4
    The built-in `.sort`'s method is probably already far more optimized than anything custom you could come up with, for the majority of cases – CertainPerformance Sep 17 '20 at 17:07
  • 1
    Use `array.sort()`? It should have satisfactory complexity and implementations also usually take into account the size of the input, so they won't, for example, run quick sort on three elements or similar. – VLAZ Sep 17 '20 at 17:08
  • 2
    You'll want to look up the various general sorting algorithms to understand their trade offs and limiting behavior (i.e. [Big O notation](https://en.wikipedia.org/wiki/Big_O_notation)). Each one is different and can be more optimal depending on your use case. However, most day-to-day programming does not get to the level of complexity where the built in `Array#sort` method becomes a bottleneck. – Aaron Sep 17 '20 at 17:17
  • Arrays. sort() is based on the TimSort algorithm, giving us a time complexity of O(n log(n)). In short, TimSort makes use of the Insertion sort and the MergeSort algorithms. However, it is still slower compared to other sorting algorithms – Ishwar Chandra Tiwari Sep 17 '20 at 17:22
  • OK, then use a faster sorting implementation, what's the problem? If you know how sorting algorithms work and which one suits your needs then use that one. – VLAZ Sep 17 '20 at 17:32
  • i do't know actually , i am just searching on Internet – Ishwar Chandra Tiwari Sep 17 '20 at 17:40
  • i really want to briefly understand the time & space complexity , like how can i mange or how can i reduce the complexity – Ishwar Chandra Tiwari Sep 17 '20 at 17:41
  • Assuming like i have 10k elements in array and i will have to sort that array, in which way i should sort? which algorithm will be better? – Ishwar Chandra Tiwari Sep 17 '20 at 17:45
  • 1
    The best algorithm for sorting arrays so far is the quick merge sort algorithm. You can read more about it. – Dejazmach Sep 17 '20 at 17:48
  • 1
    Does this answer your question? [Javascript Array.sort implementation?](https://stackoverflow.com/questions/234683/javascript-array-sort-implementation) – alotropico Sep 18 '20 at 07:03
  • 1
    Built in sort would be as close as you can get to O(NlogN) or even O(N) in some cases, which is the fastest possible – alotropico Sep 18 '20 at 07:07
  • major comments are related to arr.sort(), i think arr.sort() is best, – Ishwar Chandra Tiwari Sep 18 '20 at 08:34

0 Answers0