Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Sorting problem: a. Find the smallest number. Let this smallest number occupy

ID: 3759091 • Letter: 1

Question

1. Sorting problem: a. Find the smallest number. Let this smallest number occupy a1 by exchanging a1 with this smallest number. b. Repeat the above step on the remaining numbers. That s, find the second smallest number and let It occupy a2. C. Continue the process until the largest number is found. Design an algorithm to resolve this sorting problem.

Explanation / Answer

bingo(array A) { This procedure sorts in ascending order. } begin max := length(A)-1; { The first iteration is written to look very similar to the subsequent ones, but without swaps. } nextValue := A[max]; for i := max - 1 downto 0 do if A[i] > nextValue then nextValue := A[i]; while (max > 0) and (A[max] = nextValue) do max := max - 1; while max > 0 do begin value := nextValue; nextValue := A[max]; for i := max - 1 downto 0 do if A[i] = value then begin swap(A[i], A[max]); max := max - 1; end else if A[i] > nextValue then nextValue := A[i]; while (max > 0) and (A[max] = nextValue) do max := max - 1; end; end;