Sorting algorithms/Bubble sort: Difference between revisions

Content added Content deleted
(BBC BASIC solution replaced)
Line 237: Line 237:


=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==
The Bubble sort is very inefficient for 99% of cases. This subroutine uses a couple of 'tricks' to try and mitigate the inefficiency to a limited extent.
The Bubble sort is very inefficient for 99% of cases. This routine uses a couple of 'tricks' to try and mitigate the inefficiency to a limited extent. Note that the array index is assumed to start at zero.
<lang bbcbasic>DEF PROC_BubbleSort(Size%)
<lang bbcbasic> DIM test(9)
test() = 4, 65, 2, -31, 0, 99, 2, 83, 782, 1

PROCbubblesort(test(), 10)
I%=Size%+1
FOR i% = 0 TO 9
REPEAT
PRINT test(i%) ;
I%-=1
NEXT
LastChange%=2
FOR J% = 2 TO I%
PRINT
END
IF data%(J%-1) > data%(J%) THEN
SWAP data%(J%-1),data%(J%)
LastChange%=J%
DEF PROCbubblesort(a(), n%)
ENDIF
LOCAL i%, l%
REPEAT
NEXT J%
l% = 0
I%=LastChange%
FOR i% = 1 TO n%-1
UNTIL I% = 2
IF a(i%-1) > a(i%) THEN

SWAP a(i%-1),a(i%)
ENDPROC</lang>
l% = i%
ENDIF
NEXT
n% = l%
UNTIL l% = 0
ENDPROC</lang>
'''Output:'''
<pre>
-31 0 1 2 2 4 65 83 99 782
</pre>


=={{header|C}}==
=={{header|C}}==