Pre-condition:A[1...n] is an array of integers
Loop invariant:During each iteration the 'min' index value wil be compared with the adjacent value to find the smallest value.
Post condition:'min'wil return the index of the smallest value in the given array A[1..n].
(15 points) Consider the algorithm for insertion sort shown below. The input to this algorithm is...
Loop Invariants. I need help on the 15 point question. The 10 point question is the "procedure" from the previous question. 2: (10 points) Consider the algorithm for insertion sort shown below. The input to this algorithm is an array A. You must assume that indexing begins at 1. 1: for j = 2: A.length do key = A[j] i=j-1 while i > 0) and A[i] > key do A[i+1] = A[i] i=i-1 A[i+1] = key 3: 4: 5: 6:...
Bubble sort is a popular, but inefficient, sorting algorithm. It works by repeatedly swapping adjacent elements that out of order. BUBBLESORT(A) 1. for i = 1 to A.length – 1 2. for j = i + 1 to A.length 3. if A[j] < A[i] 4. exchange A[j] with A[i] a) A loop invariant for the outer for loop in lines 1 – 4 is: At iteration i, the sub-array A[1..i] is sorted and any element in A[i+1..A.size] is greater or...
3. Modify the insertion sort algorithm discussed in class so that it can sort strings. That is, its input will be an array, the type of which is string. The insertion sort algorithm will sort the elements in this array. Finally, its output will be a sorted array. As the solution of this problem, you need to submit the following answer(s): (1). The implementation of your algorithm in C# (20points). Algorithm: At each array-position, it checks the value there against...
Use a loop invariant to prove that the following algorithm correctly identifies the location of the minimum value in the array data. Input: data: array of integers Input: n: size of data Output: index min such that data[min] <= data[i] for any i from 1 to n Algorithm: FindMin min = 1; for i = 2 to n do if data[i] < data[min] then min = i; end end return min
Let A[1..n] be an array with n elements. Consider the Count-Occurrence algorithm with the pseudocode below. Count-Occurrence(A, n, k). count 0 for j = 1 to n if A[j] ==k count = count+1 print count Which of the following is the correct loop invariant for the for loop? At the start of each iteration jof the for loop, count represents the number of times that k occurs in the subarray A[1.j]. At the start of each iteration of the for...
Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...
For the while loop below, use the loop invariant given to show that if the pre-condition is the loop.In each step, clearly state true before the loop then the post-condition is true after what facts are assumed and what facts will be proven. (computes the sum of a list of numbers.) fa ,an While (<n) sum := sum + am j j+1 End-while ° Pre-condition: j- 1, sum a, n is a positive integer, a,,.. a, is a list of...
6. Consider the following algorithm, where P is an array containing random numbers. The function swap(v1,v2) will swap the values stored in the variables v1 and v2. Note that % is the modulus operation, and will return the integer remainder r of a/b, i.e., r-a%b Require: Array P with n > 0 values 1: i-1, j-n-l 2: while i<=j do for a=i to j by i do 4: 5: 6: 7: if Pla>Pat 11 and Pla]%2--0 then swap(Plal, Pla+1l) end...
1. Consider the following well-known sorting algorithm, which is studied later in the book, with a counter inserted to count the number of key comparisons. ALGORITHM SortAnalysis(A[0..n − 1]) //Input: An array A[0..n − 1] of n orderable elements //Output: The total number of key comparisons made count ←0 for i ←1 to n − 1 do v ←A[i] j ←i − 1 while j ≥ 0 and A[j ]> v do count ←count + 1 A[j + 1]←A[j ]...