Question

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 ass(15 points) Using the same procedure of the previous question, prove that the following algorithm finds the minimum integer i

0 0
Add a comment Improve this question Transcribed image text
Answer #1

15 points

Given algorithm to find minimum integer in array A
//assuming array contains at least 1 item
Precondition://it is placed before the segment(loops/statements),it matches all requirements for program segement, inorder produce desire output
min=1 //here min denotes the minimum integer index of A,initially setting first integer as min value in array
//so that, we can compare it with remaining elements, to satisy loop invaraint
loop invariant:
before the jth iteration of loop, min(variable) contains the index minimum index of integer in subarray A[1:j-1]
//so, precondition is true by loop invariant
Postcondition://it is place after the segment/code
//after executing loop//breaks when j exceeds n
min contains the index of minimum integer in array A
//since precondition is true, and by loop invariant, min will always contains index of minimum integer in array A[1:n]

Add a comment
Know the answer?
Add Answer to:
Loop Invariants. I need help on the 15 point question. The 10 point question is the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (15 points) Consider the algorithm for insertion sort shown below. The input to this algorithm is...

    (15 points) Consider the algorithm for insertion sort shown below. The input to this algorithm is an earray A. You must assume that indexing begins at 1. 1: for j = 2: A.length do key = A i=j-1 while i > 0 and A[i] > key do Ali + 1] = Ai i=i-1 7: A[i+1] = key (a) Follow this algorithm for A[1..4) =< 7,9,6,8 >. Specifically, please indicate the contents of the array after each iteration of the outer...

  • Bubble sort is a popular, but inefficient, sorting algorithm. It works by repeatedly swapping adjacent elements...

    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...

  • need help editing or rewriting java code, I have this program running that creates random numbers...

    need help editing or rewriting java code, I have this program running that creates random numbers and finds min, max, median ect. from a group of numbers,array. I need to use a data class and a constructor to run the code instead of how I have it written right now. this is an example of what i'm being asked for. This is my code: import java.util.Random; import java.util.Scanner; public class RandomArray { // method to find the minimum number in...

  • Prove that the Merge() function of your own merge sort algorithm in the question 2 is...

    Prove that the Merge() function of your own merge sort algorithm in the question 2 is correct using "induction on loop invariants" by following the guidelines below : (10 points) 3.1 Write down general description of loop invariant technique in your own words as proof of correctness. (2 points) 3.2 Identify the loop invariant of the loop in your merge() function (3 points) 3.3 Describe initialization step (0 points) 3.4 Describe maintenance step (4 points) 3.5 Describe Termination step (1...

  • Hello this is my java sorting algorithm program i need all of my errors corrected so...

    Hello this is my java sorting algorithm program i need all of my errors corrected so I can run it. thank you!! import java.util.Scanner;    public class SortingAlogs { public static void main(String[]args){ int array [] = {9,11,15,34,1}; Scanner KB = new Scanner(System.in); int ch; while (true) { System.out.println("1 Bubble sort\n2 Insertion sort\n3 Selection sort\n"); ch = KB.nextInt(); if (ch==1)    bubbleSort(array); if (ch==2)    insertion(array); if (ch==3)    Selection(array); if (ch==4) break;    print(array);    System.out.println(); }    }...

  • Hi! 1. I need some help with sorting string in a text file. My goal is...

    Hi! 1. I need some help with sorting string in a text file. My goal is to 1 shift left strings for string.length time. I was able to do that successfully. However, I am also trying to sort, using insertion sort , the resulting shifts. I store all shifts in a vector (or is it better to use an array?!) , and try to sort them that way, but my output is just the shifted strings but not sorted. Can...

  • no other details are given, its asking for the invariant programming language: Java Question 3. [15...

    no other details are given, its asking for the invariant programming language: Java Question 3. [15 marks in total Consider the following code fragment with missing statements at ????. Assume that A is a nonempty array of integers and has length N. Assume that it has already been initialised. Refer to this code in parts (a-e) below. A [0] int count 1; int i- 1 while (i< NI1 count> N/2 ) if (xAi]) count++ int x else ???? i+ if...

  • I need to program 3 and add to program 2 bellows: Add the merge sort and...

    I need to program 3 and add to program 2 bellows: Add the merge sort and quick sort to program 2 and do the same timings, now with all 5 sorts and a 100,000 element array. Display the timing results from the sorts. DO NOT display the array. ____________________>>>>>>>>>>>>>>>>>>>>___________________________ (This is program 2 code that I did : ) ---->>>>>> code bellow from program 2 java program - Algorithms Write a program that randomly generates 100,000 integers into an array....

  • Here is the recursion tree for the above: I need to know the formula that counts...

    Here is the recursion tree for the above: I need to know the formula that counts the nodes in that recursion tree. An example: the recursion tree for a selection sort with an array of 4 in the worst case scenario looks like this: That is the type of solution I am looking for. Please determine the formula to count the number of nodes in the recursion tree for the insertion sort with an array size of 5 (n=5) where...

  • Question 2 Consider the following algorithm Fun that takes array A and key Kas Fun(AO,...,n -...

    Question 2 Consider the following algorithm Fun that takes array A and key Kas Fun(AO,...,n - 1], K) count = 0 for i = 0 ton - 1 do for j = i +1 to n - 1 do if A[i] + A[j] == K then count = count +1 end if end for end for return count What is the best case time complexity of the above algorithm?! (log(n)) O(1) (n) (na) Previous o H H 9

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT