Write (and test) a Python function that rearranges a sequence of integer values so that all the even values appear before all the odd values. Make sure to test all common use-cases (ie, an array with all even numbers, one with all odd numbers, and one with a mixture of odd and even numbers).
What is the running time of your algorithm? Justify your answer.
just not the solution, also need to understand
it.
Thanks
Screenshot of the code:
Sample Output:
#Sample Run 1:
#Sample Run 2:
#Sample Run 3:
Code To Copy:
#Run the code in python version 3.x.
#Please indent the code before executing the
#code as per the above screenshot of the code.
#Otherwise, it may give error.
#Define the function to rearrange the
#elements of the list.
def Rearrange(list_arr, n) :
#Define the variables.
j = -1
#Begin the for loop.
#Perform the method of quick sort.
for i in range(0, n) :
#If the element at index i is odd then swap it.
if (list_arr[i] % 2 == 0) :
#Increase the value of j by 1.
j = j + 1
#Swap the element at i and j index.
temp_var = list_arr[i]
list_arr[i] = list_arr[j]
list_arr[j] = temp_var
#Prompt the user to enter the number of elements of the list.
num = int(input('Enter the number of elements of the list: '))
#Declare the variable.
element_list = []
print('Enter', num, 'elements:')
#Enter the elements.
for _ in range(num):
ele = int(input())
element_list.append(ele)
#Call the function to arrange the elements.
Rearrange(element_list, num)
#Display the updated list.
for i in range(0,num) :
print(element_list[i] ,end= " ")
Running Time of the algorithm:
Get Answers For Free
Most questions answered within 1 hours.