Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,128 questions

40,777 answers

573 users

How to segregate even and odd numbers of an array (even on left and odd on right) in Ruby

1 Answer

0 votes
def segregateElement(arr) 
	size = arr.length
	j = 0
	i = 0
	while (i < size) 
		if (arr[i] % 2 == 0) 
			arr[i], arr[j] = arr[j], arr[i]
			j += 1
		end

		i += 1
	end
end

def main() 
	arr = [1, 8, 7, 3, 9, 4, 2, 5, 10]

	size = arr.length

	segregateElement(arr)
	
	print(arr)
end

main()


 
#
# run:
# 
# [8, 4, 2, 10, 9, 1, 7, 5, 3]
#

 





answered Nov 21, 2021 by avibootz
...