Sorting algorithms/Bubble sort: Difference between revisions

→‎{{header|CMake}}: Add 12th CMake example -- but only for lists of integers.
(→‎{{header|CMake}}: Add 12th CMake example -- but only for lists of integers.)
Line 414:
(println (bubble-sort [10 9 8 7 6 5 4 3 2 1]))</lang>
 
=={{header|CMake}}==
Only for lists of integers.
 
<lang cmake># bubble_sort(var [value1 value2...]) sorts a list of integers.
function(bubble_sort var)
list(LENGTH ARGN last)
math(EXPR last "${last} - 1") # last = last index of ARGN
set(again YES)
while(again)
set(again NO)
math(EXPR last "${last} - 1") # Decrement last index.
foreach(index RANGE 0 ${last}) # Loop for each index.
math(EXPR index_plus_1 "${index} + 1")
list(GET ARGN ${index} a) # a = ARGN[index]
list(GET ARGN ${index_plus_1} b) # b = ARGN[index + 1]
if(a GREATER ${b}) # If a > b...
list(REMOVE_AT ARGN ${index_plus_1}) # ...then swap a and b
list(INSERT ARGN ${index} ${b}) # inside ARGN.
set(again YES)
endif()
endforeach(index)
endwhile()
set(${var} "${ARGN}" PARENT_SCOPE)
endfunction(bubble_sort)</lang>
 
<lang cmake>bubble_sort(result 33 11 44 22 66 55)
message(STATUS "${result}")</lang>
 
<pre>-- 11;22;33;44;55;66</pre>
 
=={{header|COBOL}}==
Anonymous user