(Solved) : 4 Let 22 Array Numbers Let S Define Reverse Pair Distinct Indices J 1 2 N Aj E Di Order Q43999691 . . .
![4. Let A = [a], 22, ..., an be an array of numbers. Lets define a reverse as a pair of distinct indices i, j € {1,2,...,n}](https://media.cheggcdn.com/media/e16/e16edfa3-bdf9-426b-aace-b834c5869a0d/php0w6jnh.png)

4. Let A = [a], 22, …, an be an array of numbers. Let’s define a ‘reverse’ as a pair of distinct indices i, j € {1,2,…,n} such that i<j but di > aj; i.e., di and a; are out of order. For example – In the array A = (1, 3, 5, 2, 4, 6), (3, 2), (5, 2) and (5, 4) are the only reverses i.e. the total number of reverses is 3. (b) We say that A is sorted if A has no reverses. Design a sorting algorithm that, on each pass through A, examines each pair of consecutive elements. If a consecutive pair forms a reverse, the algorithm swaps the elements to fir the out of order pair). For instance, if your array A was 4,2.7,3,6,9,10), your first pass should swap 4 and 2, then compare (but not swap) 4 and 7, then swap 7 and 3, then swap 7 and 6, etc. Formulate pseudo-code for this algorithm, using nested for loops. Hint: After the first pass of the outer loop think about where the largest element would be. The second pass can then safely ignore the largest element because it’s already in it’s desired location. You should keep repeating the process for all ele- ments not in their desired spot. Show transcribed image text 4. Let A = [a], 22, …, an be an array of numbers. Let’s define a ‘reverse’ as a pair of distinct indices i, j € {1,2,…,n} such that i aj; i.e., di and a; are out of order. For example – In the array A = (1, 3, 5, 2, 4, 6), (3, 2), (5, 2) and (5, 4) are the only reverses i.e. the total number of reverses is 3.
(b) We say that A is sorted if A has no reverses. Design a sorting algorithm that, on each pass through A, examines each pair of consecutive elements. If a consecutive pair forms a reverse, the algorithm swaps the elements to fir the out of order pair). For instance, if your array A was 4,2.7,3,6,9,10), your first pass should swap 4 and 2, then compare (but not swap) 4 and 7, then swap 7 and 3, then swap 7 and 6, etc. Formulate pseudo-code for this algorithm, using nested for loops. Hint: After the first pass of the outer loop think about where the largest element would be. The second pass can then safely ignore the largest element because it’s already in it’s desired location. You should keep repeating the process for all ele- ments not in their desired spot.
Expert Answer
Answer to 4. Let A = [a], 22, …, an be an array of numbers. Let’s define a ‘reverse’ as a pair of distinct indices i, j € {1,2…
OR