JavaScript What would be the better algorithm for generating following result? what would better suit me,what would be better,what

We have a input of array having n numbers (non-zero) i.e.

input--> [a1, a2, a3, a4, a5, ...., aN] (not in order)

Now we want the output as

output--> Ai <= Aj >= Ak <= Al >= Am <= ....An.

i have written a code for this problem and it is quite fine but not optimized if i talk about time and space complexity then it must not be good solution.

function test(array){
    if(array && array.length){
        let newArray = [];
        array = array.sort();
        let m;
        if(array.length % 2){
            m = (array.length +1 )/2;
        }else{
            m = (array.length)/2;
        }
        newArray.push(array[m-1]);
        for(let i=1;i<=m;i++){
            if(array[m-1+i])
            newArray.push(array[m-1+i]);
            if(array[m-1-i])
            newArray.push(array[m-1-i]);
        }
        console.log(newArray);
        return newArray;
    }else{
        throw 'invalid argument';
    }
}

test([1,2,3,4]);

Please help me if you any idea to optimise like using no other variables(it will reduce space complexity). Thanks

Answer:1

Your current algorithm runs on O(n log n) time. The .sort() at the beginning takes O(n log n) time, while the for loop below it runs in O(n) time. So, one possible improvement would be to change the sort function to use counting sort instead (which has time complexity O(n + k), where k is the range from the lowest number to the highest number). This will reduce the overall time complexity from O(n log n) to O(n + k), which will be a significant improvement with larger sets:

function countingSort(array) {
  const counts = array.reduce((a, num) => {
    a[num] = (a[num] || 0) + 1;
    return a;
  }, {});
  /*
  ok, maybe this is cheating
  return Object.entries(counts).reduce((arr, [num, occurrences]) => {
    for (let i = 0; i < occurrences; i++) {
      arr.push(Number(num));
    }
    return arr;
  }, []);
  */
  const sorted = [];
  const min = Math.min(...array);
  const max = Math.max(...array);
  for (let i = min; i <= max; i++) {
    for (let j = 0; j < counts[i]; j++) {
      sorted.push(i);
    }
  }
  return sorted;
}


function test(array){
    if(array && array.length){
        let newArray = [];
        array = countingSort(array);
        let m;
        if(array.length % 2){
            m = (array.length +1 )/2;
        }else{
            m = (array.length)/2;
        }
        newArray.push(array[m-1]);
        for(let i=1;i<=m;i++){
            if(array[m-1+i])
            newArray.push(array[m-1+i]);
            if(array[m-1-i])
            newArray.push(array[m-1-i]);
        }
        console.log(newArray);
        return newArray;
    }else{
        throw 'invalid argument';
    }
}

test([1,2,3,4]);
Answer:2

I have a HTML code like below which I use AngularJS framework within it: <select name="choose-staff" ng-model="admin_times[0].user" ng-change="update(reserve.staff)" id="choose-staff"> &...

I have a HTML code like below which I use AngularJS framework within it: <select name="choose-staff" ng-model="admin_times[0].user" ng-change="update(reserve.staff)" id="choose-staff"> &...

  1. ngrepeat dupes error
  2. ng-repeat dupes error in angularjs
  3. angular ng repeat dupes error
  4. error ngrepeat dupes solution
  5. ngrepeat dupes
  6. ngrepeat dupes issue

I tried to call .map on Uint8Array and got a surprise. const tt = new Uint8Array(1); tt[0] = 0xFF; const ttt = tt.map(x => x.toString(2)); console.log(ttt[0]); console.log(tt[0]....

I tried to call .map on Uint8Array and got a surprise. const tt = new Uint8Array(1); tt[0] = 0xFF; const ttt = tt.map(x => x.toString(2)); console.log(ttt[0]); console.log(tt[0]....

I have to populate a third select based on second. I'm trying like code below. As it is I'm feeding first select with all countries in DB. Second select is feeded based on first select. I need to ...

I have to populate a third select based on second. I'm trying like code below. As it is I'm feeding first select with all countries in DB. Second select is feeded based on first select. I need to ...

kindly assist me to know why when i call a function and pass a parameter using a string in its 'raw form' i get the right output but when i assign it to a variable first i get a different output. i ...

kindly assist me to know why when i call a function and pass a parameter using a string in its 'raw form' i get the right output but when i assign it to a variable first i get a different output. i ...