Alex is a young computer science enthusiast who loves solving coding problems. One day, Alex stumbled upon a unique challenge related to sorting numbers. The challenge involves sorting a list of integers with a unique twist: Alex aims to place the even numbers in their original input order first, followed by the odd numbers in the same input order.Your task is to help Alex implement a logic of merge sort and a recursive function to arrange the even and odd numbers separately.Input format :The first line contains an integer n, the number of integers in the list.The second line contains n space-separated integers, a[i], representing the elements of the list.Output format :The output displays a single line containing n space-separated integers, representing the sorted list, with even numbers appearing first in the given input order, followed by odd numbers in the given input order.Refer to the sample outputs for the exact format.Code constraints :1 ≤ n ≤ 30-100 <= a[i] <= 100Sample test cases :Input 1 :41 2 3 4Output 1 :2 4 1 3 Input 2 :51 5 3 -4 0Output 2 :-4 0 1 5 3
Question
Alex is a young computer science enthusiast who loves solving coding problems. One day, Alex stumbled upon a unique challenge related to sorting numbers. The challenge involves sorting a list of integers with a unique twist: Alex aims to place the even numbers in their original input order first, followed by the odd numbers in the same input order.Your task is to help Alex implement a logic of merge sort and a recursive function to arrange the even and odd numbers separately.Input format :The first line contains an integer n, the number of integers in the list.The second line contains n space-separated integers, a[i], representing the elements of the list.Output format :The output displays a single line containing n space-separated integers, representing the sorted list, with even numbers appearing first in the given input order, followed by odd numbers in the given input order.Refer to the sample outputs for the exact format.Code constraints :1 ≤ n ≤ 30-100 <= a[i] <= 100Sample test cases :Input 1 :41 2 3 4Output 1 :2 4 1 3 Input 2 :51 5 3 -4 0Output 2 :-4 0 1 5 3
Solution
Here is a Python solution for the problem:
def sort_numbers(n, arr):
even = [i for i in arr if i % 2 == 0]
odd = [i for i in arr if i % 2 != 0]
return even + odd
n = int(input())
arr = list(map(int, input().split()))
result = sort_numbers(n, arr)
print(' '.join(map(str, result)))
This Python script works by first reading the number of elements in the list from the user, then reading the list of numbers. It uses a function sort_numbers to sort the numbers according to the problem's requirements. This function uses list comprehensions to create two lists: one for even numbers and one for odd numbers. It then returns a new list that concatenates the list of even numbers with the list of odd numbers. Finally, the script prints the sorted list.
Similar Questions
Given the following list of numbers: [21, 1, 26, 45, 29, 28, 2, 9, 16, 49, 39, 27, 43, 34, 46, 40] which answer illustrates the list to be sorted after 3 recursive calls to mergesort? Select one: a. [21,1] b. [16, 49, 39, 27, 43, 34, 46, 40] c. [21] d. [21, 1, 26, 45]
If one uses a straight two-way merge sort algorithm to sort the following elements in ascending order: 20, 47, 15, 8, 9, 4, 40, 30,12, 17Then the order of these elements after the second pass of the algorithm is ________.
Given an array of size N, implement Merge sort.Input FormatThe first line of input contains an integer N - the size of an array. The second line contains the elements of the array.Output FormatFor each merge call of Merge Sort, print the array elements.Constraints1 <= N <= 201 <= A[i] <= 103ExampleInput65 1 3 15 10 4Output1 5 3 15 10 4 1 3 5 15 10 4 1 3 5 10 15 4 1 3 5 4 10 15 1 3 4 5 10 15
Write and explain the recurrence relation of Merge Sort.
Write a functional program that sorts 10 input integers (signed) using either bubble sort orselection sort algorithm. The output window must show both unsorted array of integers andsorted array of integers.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.