Computers and Technology

Proving the correctness of the cocktail shaker sorting algorithm: for (int i = 0; i < (n+1)/2; i++){ for (int j = i; j < n-i-1; j++) if (a[j] > a[j+1]) swap(a[j],a[j+1]); for (int j = n-i-2; j > i; if (a[j] < a[j-1]) swap(a[j],a[j-1]); } (a) the input to the algorithm is an array of n integers that are unsorted a and after the algorithm runs the array will be sorted. state the loop invariant of the outer loop of the cocktail shaker algorithm. (it may be to trace the algorithm on small input to see what it does.) (b) prove the loop invariant using induction over the number of iterations, i. e. i may be used. (c) show how the correctness of the loop invariant shows the correctness of the algorithm.

answer
Answers: 1

Similar questions

Предмет
Business, 19.11.2019 06:31, yuvallevy14
Stable and unstable sorting 5 points; individual-only sorting algorithms can be applied to records of data, where each record consists of multiple fields. in such contexts, the sorting algorithm orders the records according to one of the fields, and the value of that field is referred to as the key of the record. a sorting algorithm is stable if it preserves the order of elements with equal keys. for example, given the following array: {32, 12a, 4, 12b, 39, 19} where 12a and 12b represent records that both have a key of 12, a stable sorting algorithm would produce the following sorted array: {4, 120, 12b, 19, 32, 39} note that 12a comes before 12b, just as it did in the original, unsorted array. insertion sort is an example of a stable sorting algorithm. stability can be useful if you want to sort on the basis of two different fields-for example, if you want records sorted by last name and then, within a given last name, by first name. you could accomplish this in two steps: (1) use any sorting algorithm to sort the records by first name, and (2) use a stable sorting algorithm to sort the records by last name. because the second algorithm is stable, it would retain the order of records with the same last name, and thus those records would remain sorted by first name. by contrast, an unstable sorting algorithm may end up reversing the order of elements with equal keys. for example, given the same starting array shown above, an unstable sorting algorithm could produce either of the following arrays: {4, 120, 12b, 19, 32, 39} {4, 126, 12a, 19, 32, 39} selection sort is an example of an unstable sorting algorithm. construct an example of an input array containing two elements with equal keys whose order is reversed by selection sort. explain why selection sort would result in an unstable sorting algorithm while insertion sort would result in a stable sort on the exact same array.
Answers: 2
Do you know the correct answer?
Proving the correctness of the cocktail shaker sorting algorithm: for (int i = 0; i < (n+1)/2;...

Questions in other subjects:

Konu
Mathematics, 20.07.2021 20:20