PYTHON QUESTION
Given the following function:
def sort(A, n):
for i in range (1, n):
for j in range(i-1, -1,-1):
if A[j + 1] > A[j]:
t = A[j+1]
A[j+1] = A[j
A[j] = t
What would the output be for the following array: A = [3, 10, 2, 8,15]; where n = 5; for each iteration of i (i.e. when i = 1, i=2, i=3, i=4)?
A. 10, 3, 2, 8, 15
B. 10, 8, 3, 2, 15
C. 15, 10, 8, 3, 2
Match i=1, i=2, i=3 and i=4 to the letters A, B, or C
The code snippet you have provided is reverse sorting the array.
In the ith iteration we are reverse sorting the array till index i by placing the larger element in the starting. Inner loop is used to pick that larger element by comparing it with all previous elements..
So array state after iteration 1 - [10, 3, 2, 8, 15] - A
array state after iteration 2 - [10, 3, 2, 8, 15] - A
array state after iteration 3- [10, 8, 3, 2, 15] - B
array state after iteration 4 - [15, 10, 8, 3, 2] - C
You could try and run the following code to see the output by yourself : -
def sort(A, n):
for i in range(1, n):
for j in range(i - 1, -1, -1):
if A[j + 1] > A[j]:
t = A[j + 1]
A[j + 1] = A[j]
A[j] = t
print(A)
return A
if __name__ == '__main__':
print(sort([3,10,2,8,15],5))
If you have any doubt or want any clarification just put a comment on the answer.
PYTHON QUESTION Given the following function: def sort(A, n): for i in range (1, n):...
Question 3 QUESTION //Sort the array arr[] for (int i = 0; i< arr.length - 1; i++) { for (int j = 1; i < arr.length - i; j++) { if (array[i-1] > array[i]) { // swap (j-1, ) }//if }//for i }//for i In the array= 19 7 23 12 4 8 17 158, after the completion of the outer iteration #4 , the array becomes = Answer
Consider the following Python program, where x is assumed to be a list of integers. def mystery_func(x): n = len(x) for i in range(n - 1): for j in range(i + 1, n): if x[j] - x[i] < j - i: return False return True 3a. In plain English, describe what it accomplishes (i.e., the relationship between input and output, not how the code works). 3b. Analyze the running time and express it with big O. 3c. Rewrite this function...
pointsConsider this code def f (n,p): for i in range(n): for j in range(i,p): for k in range(n*p): dosomething(i,j,k) + dosomething(j,i,k) Write down the number of calls to dosomething T(n, p) in summation notation
Consider the following python code that will sort the list of numbers using the Bubble Sort algorithm def bubbleSort(alist): print(alist) for j in range (len(alist) - 1, 8, -1): for i in range(): if alist[i] > alist[i + 1]: temp = alist[i] alist[i] = alist[i+1] alist[i+1] = temp print(alist) alist = [52, 25, 94, 17, 25, 52] bubbleSort (alist) print(alist) Sort the following series of values into ascending order using the Bubble Sort algorithm. Write out the complete row of...
QUESTION: //Sort the array arr[] for (int i = 0; i < arr.length - 1; i++) { //outer int index = i; for (int j = i + 1; j < arr.length; i++) if (arr[j] < arr[index]) index = j; int smallerNumber = arr[index]; arr[index] = arr[i]; arr[i] = smallerNumber; }//for i In the array= 16 13 15 14 19 24 9 3, the index of the smallest number at the outer iteration 4 = Answer
My tests for sorting algorithms are not working. sorting.py has 6 sorting algorithms and test_sorting.py has test cases to run those algorithms, but test cases are not working. please correct the errors in test_sorting.py file so that it can test all the sorting algorithms. WARNING: DON'T COPY AND PASTE THE QUESTION IN ANSWER. I WILL REPORT YOU. sorting.py # 1. selection sort # 2. insertion sort # 3. shell sort # 4. heap sort # 5. merge sort # 6....
Question 18 CLO3 Analyze the following code and answer the questions that follow def F(n): If n <= 1: return n else: return F(n-1)+F(n-2) for i in range (n) print (F(i)) Result: 0 1 1 2 3 5 8 13 a. Write number of operations as a function when the code is execute b If n 7, what is the total number of operations? c. What is the complexity of the algorithm behind the code? (2 Marks) (2 Marks) (1...
def selectionSortK(alist, k):
for i in range(0,len(alist) - 1):
min = i
for j in range(i + 1, len(alist)):
if alist[j] < alist[min]:
min = j
temp = alist[i]
alist[i] = alist[min]
alist[min] = temp
P3: Sanity Test: Is selectionSortK callable? ... ok
test_doNothing (__main__.TestProblem3)
P3: Does sorting the first k elements with k=0 do nothing? ...
ok
test_onePass (__main__.TestProblem3)
P3: Sorting a portion of a decreasing list ... FAIL
test_severalPasses (__main__.TestProblem3)
P3: Sorting a decreasing list in several stages...
Given a bubble sort and the following array: [10,7,19,5,16] What are the values after the first iteration? Given an insertion sort and the following array: [50,5,35,70,6] What does the array look like after the first insertion: Fill in the missing code for InsertionSort method // Insertion Sort method //sorts an array of Auto objects public static void insertionSort(Auto [] arr) { Auto temp; int j; for (int i=1; i<arr.length; i++) { j=i; temp=arr[i]; while (j!=0 &&(temp.getModel().compareTo(arr[[j-1].getModel())<0)...
I need help for the order of growth for functions in Python 3. Q1: What is the order of growth for the following functions? Kinds of Growth Here are some common orders of growth, ranked from no growth to fastest growth: 1. Θ(1) — constant time takes the same amount of time regardless of input size 2. Θ(log n) — logarithmic time 3. Θ(n) — linear time 4. Θ(n log n) — linearithmic time 5. Θ(n2 ) 6. Θ(n3 ),...