Bubble sorting in progress 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] &gt; 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 ) -&gt;  ( 1 5 4 2 8 ) // Here, algorithm compares the first two elements, 
                                   // and swaps since 5 &gt; 1.
2: ( 1 5 4 2 8 ) -&gt;  ( 1 4 5 2 8 ) // Swap since 5 &gt; 4
3: ( 1 4 5 2 8 ) -&gt;  ( 1 4 2 5 8 ) // Swap since 5 &gt; 2
4: ( 1 4 2 5 8 ) -&gt;  ( 1 4 2 5 8 ) // Now, since these elements are already in order 
                                   // (8 &gt; 5), algorithm does not swap them.

Second Pass
5: ( 1 4 2 5 8 ) -&gt;  ( 1 4 2 5 8 )
6: ( 1 4 2 5 8 ) -&gt;  ( 1 2 4 5 8 ) // Swap since 4 &gt; 2
7: ( 1 2 4 5 8 ) -&gt;  ( 1 2 4 5 8 )
8: ( 1 2 4 5 8 ) -&gt;  ( 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 ) -&gt;  ( 1 2 4 5 8 )
10:( 1 2 4 5 8 ) -&gt;  ( 1 2 4 5 8 )
11:( 1 2 4 5 8 ) -&gt;  ( 1 2 4 5 8 )
12:( 1 2 4 5 8 ) -&gt;  ( 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
JavaScript (ES6), 102 82 80 86 80 bytes

Bug fix and 1 byte saved thanks to @edc65

(a,m)=&gt;eval("for(i=0;m;a[j=i-1]&gt;(b=a[i])?a[a[i]=a[j],j]=b:0)1/a[++i]?m--:i=0;a")


Recursion may not be is definitely not is probably the best approach, but I'm sticking with a loop for now.

Try it out:



f=(a,m)=&gt;eval("for(i=0;m;a[j=i-1]&gt;(b=a[i])?a[a[i]=a[j],j]=b:0)1/a[++i]?m--:i=0;a")
Enter your numbers:&lt;br&gt;
&lt;input id=A rows=10 value="5 1 4 2 8"&gt;&lt;br&gt;
Enter the number of steps:&lt;br&gt;
&lt;input type="number" min=0 id=B rows=10 value="1"&gt;&lt;br&gt;
&lt;button onclick="C.innerHTML=f((A.value.match(/-?\d+/g)||[]).map(Number),B.value)"&gt;Run&lt;/button&gt;&lt;br&gt;
&lt;pre id=C&gt;&lt;/pre&gt;