Create a function or program that takes two inputs:
- A list of integers that shall be sorted (less than 20 elements)
- A positive integer,
N
, saying how many comparisons you should take
The function shall stop, and output the resulting list of integers after N
comparisons. If the list is fully sorted before N
comparisons are made, then the sorted list should be outputted.
The Bubble sort algorithm is well known, and I guess most people know it. The following Pseudo-code and animation (both from the linked Wikipedia-article) should provide the necessary details:
procedure bubbleSort( A : list of sortable items )
n = length(A)
repeat
swapped = false
for i = 1 to n-1 inclusive do
/* if this pair is out of order */
if A[i-1] > A[i] then
/* swap them and remember something changed */
swap( A[i-1], A[i] )
swapped = true
end if
end for
until not swapped
end procedure
The animation below shows the progress:
An example (taken directly from the linked Wikipedia article) shows the steps when sorting the list: ( 5 1 4 2 8 )
:
First Pass
1: ( 5 1 4 2 8 ) -> ( 1 5 4 2 8 ) // Here, algorithm compares the first two elements,
// and swaps since 5 > 1.
2: ( 1 5 4 2 8 ) -> ( 1 4 5 2 8 ) // Swap since 5 > 4
3: ( 1 4 5 2 8 ) -> ( 1 4 2 5 8 ) // Swap since 5 > 2
4: ( 1 4 2 5 8 ) -> ( 1 4 2 5 8 ) // Now, since these elements are already in order
// (8 > 5), algorithm does not swap them.
Second Pass
5: ( 1 4 2 5 8 ) -> ( 1 4 2 5 8 )
6: ( 1 4 2 5 8 ) -> ( 1 2 4 5 8 ) // Swap since 4 > 2
7: ( 1 2 4 5 8 ) -> ( 1 2 4 5 8 )
8: ( 1 2 4 5 8 ) -> ( 1 2 4 5 8 )
Now, the array is already sorted, but the algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.
Third Pass
9: ( 1 2 4 5 8 ) -> ( 1 2 4 5 8 )
10:( 1 2 4 5 8 ) -> ( 1 2 4 5 8 )
11:( 1 2 4 5 8 ) -> ( 1 2 4 5 8 )
12:( 1 2 4 5 8 ) -> ( 1 2 4 5 8 )
Test cases:
Format: Number of comparisons (N): List after N comparisons
Input list:
5 1 4 2 8
Test cases:
1: 1 5 4 2 8
2: 1 4 5 2 8
3: 1 4 2 5 8
4: 1 4 2 5 8
5: 1 4 2 5 8
6: 1 2 4 5 8
10: 1 2 4 5 8
14: 1 2 4 5 8
Input list:
0: 15 18 -6 18 9 -7 -1 7 19 19 -5 20 19 5 15 -5 3 18 14 19
Test cases:
1: 15 18 -6 18 9 -7 -1 7 19 19 -5 20 19 5 15 -5 3 18 14 19
21: -6 15 18 9 -7 -1 7 18 19 -5 19 19 5 15 -5 3 18 14 19 20
41: -6 9 -7 15 -1 7 18 18 -5 19 19 5 15 -5 3 18 14 19 19 20
60: -6 -7 -1 9 7 15 18 -5 18 19 5 15 -5 3 18 14 19 19 19 20
61: -6 -7 -1 7 9 15 18 -5 18 19 5 15 -5 3 18 14 19 19 19 20
81: -7 -6 -1 7 9 15 -5 18 18 5 15 -5 3 18 14 19 19 19 19 20
119: -7 -6 -1 -5 7 9 15 5 15 -5 3 18 14 18 18 19 19 19 19 20
120: -7 -6 -1 -5 7 9 15 5 15 -5 3 18 14 18 18 19 19 19 19 20
121: -7 -6 -1 -5 7 9 5 15 15 -5 3 18 14 18 18 19 19 19 19 20
122: -7 -6 -1 -5 7 9 5 15 15 -5 3 18 14 18 18 19 19 19 19 20
123: -7 -6 -1 -5 7 9 5 15 -5 15 3 18 14 18 18 19 19 19 19 20
201: -7 -6 -5 -1 -5 3 5 7 9 14 15 15 18 18 18 19 19 19 19 20
221: -7 -6 -5 -5 -1 3 5 7 9 14 15 15 18 18 18 19 19 19 19 20
- Yes, built-in Bubble sort algorithms are permitted.
- No, you can not assume only positive integers, or unique integers.
- The sorting must be in the order described above. You can't start at the end of the list