physicist
physicist

Reputation: 49

Sort the odd numbers in the list

How can I sort ascending the odd numbers in a list of integers, but leaving the even numbers in their original places?

Example:

sortArray([5, 3, 2, 8, 1, 4]) == [1, 3, 2, 8, 5, 4]

My code:

def sort_array(source_array):     
    odd_numbers = [n for n in source_array if n%2!=0]
    odd_numbers = sorted(odd_numbers)

How do I switch the indices of the odd numbers in source_array with the ones in odd_numbers?

Upvotes: 4

Views: 12190

Answers (7)

Mark Markovich
Mark Markovich

Reputation: 1

You can get the result you want by iterating over the length of the array and performing a sort only on the items that are odd, which can be determined by modulus division. This algorithm utilizes Bubblesort, which is inefficient, but can operate as a working example of the principal.

public static int[] SortArray(int[] array)
{
    for (int i = 0; i < array.Length; i++)
    {
        for (int j = i; j < array.Length; j++)
        {
            if (array[i] % 2 == 1 && array[j] % 2 == 1 && array[i] > array[j])
            {
                var por = array[i];
                array[i] = array[j];
                array[j] = por;
            }
        }
    }

    return array;
}

Upvotes: -4

user18609885
user18609885

Reputation: 1

def sort_array(arr):
  odds = sorted((x for x in arr if x%2 != 0), reverse=True)
  return [x if x%2==0 else odds.pop() for x in arr]

Upvotes: 0

Pavel
Pavel

Reputation: 1

def sort_array(source_array):
    sorted_array = [i for i in source_array if i % 2 != 0]
    sorted_array.sort()
    for index, item in enumerate(source_array):
        if item % 2 == 0:
            sorted_array.insert(index, item)
    return sorted_array

Upvotes: 0

Rakesh
Rakesh

Reputation: 82755

def sort_array(source_array):
    odd_numbers = sorted([n for n in source_array if n%2!=0])
    c = 0
    res = []
    for i in source_array:
        if i %2!=0:
            res.append(odd_numbers[c])
            c += 1
        else:
            res.append(i)
    return res  

Upvotes: 0

Moses Koledoye
Moses Koledoye

Reputation: 78546

If you're open to using numpy, you can get the indices of odd numbers using np.where, sort the odd numbers and update the array using the previously obtained indices assigning to the sorted array of odd numbers:

import numpy as np

a = np.array([5, 3, 2, 8, 1, 4])
ind = np.where(a%2)                 # get indices of odd items
a[ind] = np.sort(a[ind])            # update items at indices using sorted array
print(a)
# array([1, 3, 2, 8, 5, 4])

Upvotes: 1

Xuyan Xiao
Xuyan Xiao

Reputation: 81

More lines but also more readable

a = [5, 3, 2, 8, 1, 4]
b = sorted([item for item in a if item%2 != 0])
odd_int = 0
for i in range(len(a)):
    if a[i] %2 != 0:
        a[i] = b[odd_int]
        odd_int += 1

Upvotes: 5

Jon Clements
Jon Clements

Reputation: 142126

Looks like you're almost there - you can make your sorted odd numbers an iterable and re-build your source list with either the original even number or the next sorted odd number, eg:

>>> data = [5, 3, 2, 8, 1, 4]
>>> odds = iter(sorted(el for el in data if el % 2))
>>> [next(odds) if el % 2 else el for el in data]
[1, 3, 2, 8, 5, 4]

Upvotes: 15

Related Questions