Sorting algorithms/Bubble sort: Difference between revisions

BBC BASIC solution replaced
(BBC BASIC solution replaced)
Line 237:
 
=={{header|BBC BASIC}}==
The Bubble sort is very inefficient for 99% of cases. This subroutineroutine 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 DIM test(Size%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%)
DEF LastChange%=JPROCbubblesort(a(), n%)
ENDIF LOCAL i%, l%
REPEAT
NEXT J%
l% = 0
I%=LastChange%
FOR i% = 1 TO n%-1
UNTIL I% = 2
IF data%a(Ji%-1) > data%a(Ji%) THEN
 
SWAP data%a(Ji%-1),data%a(Ji%)
ENDPROC</lang>
l% = i%
ENDIF
NEXT
n% = l%
UNTIL Il% = 20
ENDPROC</lang>
'''Output:'''
<pre>
-31 0 1 2 2 4 65 83 99 782
</pre>
 
=={{header|C}}==