Separate odd and even numbers in an array

Problem :

Given an array of integers, you need to segregate odd and even numbers in an array.
Please note : Order of elements can be changed.
For example:

Algorithm:

Lets say array is arr[]

  • Initialise two index variable , left=0 and right=arr.length-1
  • Increment left variable until you get odd number
  • Decrement right variable until you get even number.
  • If left < right, swap arr[left] and arr[right]
  • In the end, you will see that you have even numbers on left side and odd numbers on right side.

Java code to separate odd and even numbers in an array :

when you run above program, you will get below output:

One Response

  1. Praveen December 7, 2016

Add Comment