Sorting algorithms/Bubble sort: Difference between revisions

no edit summary
m (syntax highlighting fixup automation)
No edit summary
Line 2,899:
<pre>unsort -7 3 -4 -6 4 -1 -2 2 7 0 5 1 -3 -5 6
sort -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7</pre>
 
 
 
=={{header|FutureBasic}}==
Bubble sorting is purely an academic exercise since there are much more efficient native sorting functions in FB.
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
local fn BubbleSort( array as CFMutableArrayRef ) as CFArrayRef
'~'1
NSUInteger i, x, y, count = len(array)
BOOL swapped = YES
 
while (swapped)
swapped = NO
for i = 1 to count -1
x = fn NumberIntegerValue( array[i-1] )
y = fn NumberIntegerValue( array[i] )
if ( x > y )
MutableArrayExchangeObjects( array, (i-1), i )
swapped = YES
end if
next
wend
end fn = array
 
CFMutableArrayRef array
CFArrayRef unsortedArray, sortedArray
NSUInteger i
 
array = fn MutableArrayWithCapacity(0)
for i = 0 to 20
MutableArrayAddObject( array, fn NumberWithInteger( rnd(100) ) )
next
 
unsortedArray = fn ArrayWithArray( array )
sortedArray = fn BubbleSort( array )
 
NSLog( @"\n-----------------\nUnsorted : Sorted\n-----------------" )
for i = 0 to 20
NSLog( @"%8ld : %-8ld", fn NumberIntegerValue( unsortedArray[i] ), fn NumberIntegerValue( sortedArray[i] ) )
next
 
randomize
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
-----------------
Unsorted : Sorted
-----------------
97 : 7
91 : 8
13 : 13
39 : 17
50 : 20
48 : 28
7 : 28
61 : 30
30 : 30
20 : 33
69 : 39
86 : 42
33 : 48
65 : 50
28 : 50
50 : 61
28 : 65
8 : 69
17 : 86
42 : 91
30 : 97
</pre>
 
 
 
 
 
 
 
 
=={{header|g-fu}}==
715

edits