• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests
November 24, 2022 |2.7K Views
C Program to Segregate Even and Odd numbers
  Share  2 Likes
Description
Discussion

In this video, we will write a C Program to Segregate Even and Odd numbers. Segregating even and odd numbers mean putting even numbers first and odd numbers last in the array. 

Example:
Input: [12,9,4,7,2,13]
Output: [12,4,2,9,7,13]
Alternate Output: [12,2,4,13,7,9]

Methods to Segregate Even & Odd Numbers

1) Using nested while loop
2) Using the Lomuto partition method
3) Using Two pointer method

1) Using nested while loop: In this method, first we find the pivot element. So we have two pointers starting from the first and last digits and both the pointers will move towards each other. Now we will swap the elements if they are not in the desired position.

2) Using the Lomuto partition method: In this method, we will have a single for loop and a variable to store the index. If the element is even. We will increment the variable and swap the position of the current index and the variable.

3) Using Two pointer method: In this method, we will use 2 pointers, the first will be pointed at the first element, and the last pointer at the last element. Then we will iterate over every element and if the pointers contain odd and even numbers respectively. We will swap them as bring an even number forward. If the numbers are in right place, we will just decrement the second pointer.

Read More