Show Buttons
Share On Facebook
Share On Twitter
Share On Google Plus
Share On Linkdin
Share On Pinterest
Share On Reddit
Share On Stumbleupon
Contact us
Hide Buttons

Sort an Array 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.

Objec­tive: Given an array of interg­ers, sort it such that the odd num­bers appear first fol­lowed by the even num­bers . The odd num­bers in ascend­ing order and the even num­bers in descend­ing order.

Input: An Array of Integers

Exam­ple:

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 sep­a­rate the odd num­bers and even num­bers, put odd num­bers first fol­lowed by even num­bers using quick sort tech­nique. (Click here to read about quick sort)
  • Now sort the odd num­bers in ascend­ing order
  • Sort the even num­bers in descend­ing order.

Com­plete Code:


Out­put:

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

You may also like...

  • danielwx

    Thx for shar­ing! What is the time btw?