0% found this document useful (0 votes)
15 views

Writing Part

Uploaded by

abdul wahid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Writing Part

Uploaded by

abdul wahid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Part II

Writing Part

Loop Invariant 1: The first loop invariant is that the first "startOfUnsorted" elements of the array are
sorted in ascending order. This is because each iteration of the outer loop selects the smallest element
from the unsorted region (the region starting from "startOfUnsorted" index to the end of the array) and
swaps it with the first element of the unsorted region (at index "startOfUnsorted"). As a result, the first
"startOfUnsorted" elements are always sorted.

Proof:

 Initialization: At the start of the algorithm, "startOfUnsorted" is equal to 0, which means that
the entire array is considered as the unsorted region. Since the loop has not started yet, the first
"startOfUnsorted" elements are considered sorted (which is an empty set). So, the loop
invariant holds.

 Maintenance: During each iteration of the outer loop, the smallest element from the unsorted
region is selected and swapped with the first element of the unsorted region (at index
"startOfUnsorted"). As a result, the first "startOfUnsorted" elements remain sorted in ascending
order, and the loop invariant holds.

 Termination: When the outer loop terminates, "startOfUnsorted" becomes equal to the length
of the array. This means that the entire array is considered sorted, and the first
"startOfUnsorted" elements (which is the entire array) are sorted in ascending order. Hence, the
loop invariant holds.

Loop Invariant 2: The second loop invariant is that "indexOfSmallest" holds the index of the smallest
element in the unsorted region (the region starting from "startOfUnsorted" index to the end of the
array). This is because the inner loop iterates through the unsorted region and updates
"indexOfSmallest" with the index of the smallest element found during each iteration.

Proof:

 Initialization: At the start of the algorithm, "indexOfSmallest" is set to -1, which means that no
smallest element has been found yet. During the first iteration of the inner loop, if the first
element of the unsorted region is smaller than the initial value of "smallestInUnsorted" (which is
the maximum value), then "indexOfSmallest" is updated with the index of the first element and
"smallestInUnsorted" is updated with its value. Hence, the loop invariant holds.

 Maintenance: During each iteration of the inner loop, if the current element of the unsorted
region is smaller than "smallestInUnsorted", then "indexOfSmallest" is updated with the index of
the current element and "smallestInUnsorted" is updated with its value. As a result,
"indexOfSmallest" holds the index of the smallest element in the unsorted region, and the loop
invariant holds.

 Termination: When the inner loop terminates, "indexOfSmallest" holds the index of the smallest
element in the unsorted region. Hence, the loop invariant holds

We can code this pseudocode as follows;

You might also like