How to sort an array of odd numbers in ascending order, but keep even numbers at their position?

淺唱寂寞╮ 提交于 2019-12-02 02:37:25

You could take a helper array for the odd indices and another for the odd numbers, sort them and apply them back on the previously stored indices of the original array.

var array = [5, 3, 2, 8, 1, 4],
    indices = [];

array
    .filter((v, i) => v % 2 && indices.push(i))
    .sort((a, b) => a - b)
    .forEach((v, i) => array[indices[i]] = v);

console.log(array);

Here's a solution using mostly the built-in array methods. Get a list of just the odds, sort it, then map through the original, replacing each item with the first sorted odd if the item is odd, or itself if even:

const array = [5, 3, 2, 8, 1, 4] // to: [1, 3, 2, 8, 5, 4]

function sortOddsOnly(arr) {
    const odds = arr
        .filter(x => x%2)
        .sort((a, b) => a - b);
        
    return arr
        .map(x => x%2 ? odds.shift() : x);
}

console.log(sortOddsOnly(array));

I have a solution like this.
Build a sorted odd number array 1st, and then fill the rest of even numbers in order:

const arr = [5, 3, 2, 8, 1, 4];

const odd = arr.filter(i => i%2 !== 0).sort();
let i = 0,
  result = [];
arr.forEach(e => {
  if (e%2 === 0) {
    result.push(e)
  } else {
    result.push(odd[i]);
    i++;
  }
});

console.log(result);

just do:

arr.sort((a, b) => a%2 && b%2 ? a - b : 0)

If that works depends on the sort algorithm your browser uses. A browserindependent version:

for(const [i1, v1] of arr.entries())
  for(const [i2, v2] of arr.entries())
    if( v1%2 && v2%2 && (i1 < i2) === (v1 > v2))
        ([arr[i1], arr[i2]] = [v2, v1]);

One of the possible solutions is this. What I have done is created new array odd(array with odd position in original array using Array.prototype.filter) and then sort that array using Array.prototype.sort. Then using Array.prototype.map change value of all odd element of original array with odd array.

x1=[5, 3, 2, 8, 1, 4];
function sortArray(array) {
  var odd = array.filter((x,i) => (i+1) % 2 ).sort((a,b) => a > b); //sort odd position and store that in new array
  return array.map((x,i) => (i+1) % 2 ? odd.shift() : x ); //if i is odd then replace it with element from 
                                                  //odd array otherwise keep the element as it is
}
console.log(sortArray(x1));

Here is a possible solution using a slightly customized selection sort :

var xs = [5, 3, 2, 8, 1, 4];

console.log(sortOddsOnly(xs));

function sortOddsOnly (xs) {
  var n = xs.length;
  for (var i = 0; i < n - 1; i++) {
    if (xs[i] % 2 === 1) {
      for (var j = i + 1; j < n; j++) {
        if (xs[j] % 2 === 1) {
          if (xs[i] > xs[j]) {
            var min = xs[j];
            xs[j] = xs[i];
            xs[i] = min;
          }
        }
      }
    }
  }
  return xs;
}

The first two if guarantee that we swap only odd numbers (x % 2 === 1 means "x is odd").

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!