Question

Assume that the inner loop works correctly. Using a loop-invariant proof for the outer loop, formally...

Assume that the inner loop works correctly. Using a loop-invariant proof for the outer loop, formally prove that your pseudo-code correctly sorts the given array. Be sure that your loop invariant and proof cover the initialization, maintenance, and termination conditions

Given code:

/**
 * a[0:n-1] is an array of n elements.
 * temp is a variable to facilitate exchange.
 */
BubbleSort(a, n)
Begin
  for k = 1 to n-1 increment by 1 do  // where, k is for the pass
    for j = 0 to n – k – 1 increment by 1 do  // this is for comparison
      if a[j] > a[j+1] then  
        Set temp = a[j];
        Set a[j] = a[j+1];
        Set a[j+1] = temp;
      endif
    endfor
  endfor
End

Homework Answers

Answer #1

Loop invariant for outer loop:
before ith iteration the elements in list from index n-k to n-1 are sorted
Initialization:before the first iteration k=1, which is last element since only one element it is sorted
hence true, thus invariant satisfied
Maintenance: since inner loop works correctly, this can be easily verified by checking the invariant after each iteration of outer loop, in this case it is satisfied
Termination:(step to prove correctness)
when the loop terminates, k = n-1 , since from maintenance we know that list will be sorted from n-k to n-1
so, agian loop invariant is satisfied, this means list is sorted

Know the answer?
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
Similar Questions
For each pseudo-code function below (after the next ==== line), write a useful loop invariant capturing...
For each pseudo-code function below (after the next ==== line), write a useful loop invariant capturing correctness for the main loop in each of the following programs and briefly argue initialization, preservation, and termination. EXAMPLE PROBLEM: //Function to return the max of an array A Maximum(array of integers A) Local integer integer m m=0 for i = 1 to n if A[i] > m then m = A[i] end function Maximum EXAMPLE SOLUTION: The loop invariant is m = max(0,...
*********I need question 6 answered which is from question 5 which is ********* Question 5 :...
*********I need question 6 answered which is from question 5 which is ********* Question 5 : Program Correctness I (1 point) Use the loop invariant (I) to show that the code below correctly computes n P−1 k=0 2k (this sum represents the sum of the first n even integers where n ≥ 1). Algorithm 1 evenSum(int n) 1: p = 2(n − 1) 2: i = n − 1 3: while i > 0 do 4: //(I) p = nP−1...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using...
Question: I get a Segmentation fault error sometimes when I addElementBack or print. Am I using pointers correctly and deleting memory properly? #ifndef DYNAMICARRAY_H #define DYNAMICARRAY_H #include <cstdlib> #include <iostream> using namespace std; // Node class class Node { int data; Node* next; Node* prev; public: Node(); Node(int); void SetData(int newData) { data = newData; }; void SetNext(Node* newNext) { next = newNext; }; void SetPrev(Node* newPrev) { prev = newPrev; }; int getData() { return data; }; Node* getNext()...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's...
Consider the C program (twoupdate) to demonstrate race condition. In this assignment, we will implement Peterson's algorithm to ensure mutual exclusion in the respective critical sections of the two processes, and thereby eliminate the race condition. In order to implement Peterson's Algorithm, the two processes should share a boolean array calledflagwith two components and an integer variable called turn, all initialized suitably. We will create and access these shared variables using UNIX system calls relating to shared memory – shmget,...