FOR FREE MATERIALS

Sorted the array and then separate even and odd elements in different array

Back to Programming

Description

The elements of the array are taken as input. The array is first sorted in ascending order (using bubble sort) and then the even and odd elements are separated into two separate arrays.

 

 

 

Algorithm

INPUT: Array elements
OUTPUT: the sorted arrays of odd and even elements separately
PROCESS:
Step 1: [taking the input]
	Read n [number of elements]
	For i=0 to n-1 repeat
		Read a[i]
	[end of ‘for’ loop]
Step 2: [separating the odd and even elements]
/*Sort the array ‘a’ using any sorting technique*/
	Set o<-0
	Set e<-0
	For i=0 to n-1 repeat
		If a[i] mod 2=0 then
			Set even[e]<-a[i]
			Set e<-e+1
		else
			Set odd[o]<-a[i]
			Set o<-o+1
		[End of ‘if’]
	[End of ‘for’ loop]
	[printing the arrays]
	For i=0 to e-1 repeat
		Print even[i]
	[End of ‘for’ loop]
	For i=0 to o-1 repeat
		Print odd[i]
	[End of ‘for’ loop]
Step 3: Stop.

Code

Complexity: