Sort an Array – odd numbers appear first in ascending order followed by the even numbers in descending order.

Objective: Given an array of intergers, sort it such that the odd numbers appear first followed by the even numbers . The odd numbers in ascending order and the even numbers in descending order.

Input: An Array of Integers

Example:

Input Array : 1 2 3 4 5 6 7 8 9 10
Output : 1 3 5 7 9 10 8 6 4 2

Approach:

  • First separate the odd numbers and even numbers, put odd numbers first followed by even numbers using quick sort technique. (Click here to read about quick sort)
  • Now sort the odd numbers in ascending order
  • Sort the even numbers in descending order.

Complete Code:

Output:

Original Array :
1 2 3 4 5 6 7 8 9 10
Output Array :
1 3 5 7 9 10 8 6 4 2

1 thought on “Sort an Array – odd numbers appear first in ascending order followed by the even numbers in descending order.”

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.