Sorting algorithms/Bubble sort: Difference between revisions

Content added Content deleted
No edit summary
Line 2,908: Line 2,908:


local fn BubbleSort( array as CFMutableArrayRef ) as CFArrayRef
local fn BubbleSort( array as CFMutableArrayRef ) as CFArrayRef
NSUInteger i, x, y, count = len(array)
'~'1
BOOL swapped = YES
NSUInteger i, x, y, count = len(array)
BOOL swapped = YES
while (swapped)

while (swapped)
swapped = NO
for i = 1 to count -1
swapped = NO
x = fn NumberIntegerValue( array[i-1] )
for i = 1 to count -1
x = fn NumberIntegerValue( array[i-1] )
y = fn NumberIntegerValue( array[i] )
y = fn NumberIntegerValue( array[i] )
if ( x > y )
MutableArrayExchangeObjects( array, (i-1), i )
if ( x > y )
swapped = YES
MutableArrayExchangeObjects( array, (i-1), i )
end if
swapped = YES
next
end if
wend
next
wend
end fn = array
end fn = array


Line 2,931: Line 2,930:
array = fn MutableArrayWithCapacity(0)
array = fn MutableArrayWithCapacity(0)
for i = 0 to 20
for i = 0 to 20
MutableArrayAddObject( array, fn NumberWithInteger( rnd(100) ) )
MutableArrayAddObject( array, fn NumberWithInteger( rnd(100) ) )
next
next


Line 2,939: Line 2,938:
NSLog( @"\n-----------------\nUnsorted : Sorted\n-----------------" )
NSLog( @"\n-----------------\nUnsorted : Sorted\n-----------------" )
for i = 0 to 20
for i = 0 to 20
NSLog( @"%8ld : %-8ld", fn NumberIntegerValue( unsortedArray[i] ), fn NumberIntegerValue( sortedArray[i] ) )
NSLog( @"%8ld : %-8ld", fn NumberIntegerValue( unsortedArray[i] ), fn NumberIntegerValue( sortedArray[i] ) )
next
next


Line 2,973: Line 2,972:
30 : 97
30 : 97
</pre>
</pre>









=={{header|g-fu}}==
=={{header|g-fu}}==