Question: 4. Let A = [a], 22, ..., an be an array of numbers. Let's define a 'reverse' as a pair of distinct indices i, j
![4. Let A = [a], 22, ..., an be an array](https://dsd5zvtm8ll6.cloudfront.net/si.experts.images/questions/2024/09/66f3c84271994_54566f3c841e314a.jpg)

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