Sorting algorithms/Bubble sort: Difference between revisions

no edit summary
m (→‎{{header|NetRexx}}: Fix output)
No edit summary
Line 1,979:
<lang octave>v = [9,8,7,3,1,100];
disp(bubblesort(v));</lang>
 
=={{header|ooRexx}}==
===Reimplementation of [[#NetRexx|NetRexx]]===
{{trans|NetRexx}}
This version exploits the &quot;Collection Classes&quot; and some other features of the language that are only available in Open Object Rexx.
<lang ooRexx>/* Rexx */
Do
placesList = sampleData()
call show placesList
say
sortedList = bubbleSort(placesList)
call show sortedList
say
 
return
End
Exit
 
-- -----------------------------------------------------------------------------
bubbleSort:
procedure
Do
il = arg(1)
sl = il~copy
 
listLen = sl~size
loop i_ = 1 to listLen
loop j_ = i_ + 1 to listLen
cmpi = sl[i_]
cmpj = sl[j_]
if cmpi > cmpj then do
sl[i_] = cmpj
sl[j_] = cmpi
end
end j_
end i_
return sl
End
Exit
 
-- -----------------------------------------------------------------------------
show:
procedure
Do
al = arg(1)
 
loop e_ over al
say e_
end e_
return
End
Exit
 
-- -----------------------------------------------------------------------------
sampleData:
procedure
Do
placesList = .array~of( ,
"UK London", "US New York", "US Boston", "US Washington", ,
"UK Washington", "US Birmingham", "UK Birmingham", "UK Boston" ,
)
 
return placesList
End
Exit
 
</lang>
;Output
<pre style="height: 20ex; overflow: scroll;">
UK London
US New York
US Boston
US Washington
UK Washington
US Birmingham
UK Birmingham
UK Boston
 
UK Birmingham
UK Boston
UK London
UK Washington
US Birmingham
US Boston
US New York
US Washington
 
</pre>
===Translation of Pseudocode===
This version is a direct implementation of this task's pseudocode.
<lang ooRexx>/* Rexx */
Do
placesList = sampleData()
call show placesList
say
sortedList = bubbleSort(placesList)
call show sortedList
say
 
return
End
Exit
 
-- -----------------------------------------------------------------------------
bubbleSort:
procedure
Do
il = arg(1)
sl = il~copy
itemCount = sl~size
 
loop label c_ until \hasChanged
hasChanged = isFalse()
itemCount = itemCount - 1
loop i_ = 1 to itemCount
if sl[i_] > sl[i_ + 1] then do
t_ = sl[i_]
sl[i_] = sl[i_ + 1]
sl[i_ + 1] = t_
hasChanged = isTrue()
end
end i_
end c_
 
return sl
End
Exit
 
-- -----------------------------------------------------------------------------
show:
procedure
Do
al = arg(1)
 
loop e_ over al
say e_
end e_
return
End
Exit
 
-- -----------------------------------------------------------------------------
sampleData:
procedure
Do
placesList = .array~of( ,
"UK London", "US New York", "US Boston", "US Washington", ,
"UK Washington", "US Birmingham", "UK Birmingham", "UK Boston" ,
)
 
return placesList
End
Exit
 
-- -----------------------------------------------------------------------------
isTrue: procedure
return (1 == 1)
 
-- -----------------------------------------------------------------------------
isFalse: procedure
return \isTrue()
</lang>
 
===Classic [[REXX|Rexx]] Implementation===
A more &quot;traditional&quot; implementation of version 1 using only Rexx primitive constructs. This version has been tested with the ''Open Object Rexx'' and ''Regina'' Rexx interpreters and could equally have been exhibited with the [[#REXX|Rexx]] solution.
<lang ooRexx>/* Rexx */
Do
placesList. = ''
sortedList. = ''
call sampleData
call bubbleSort
 
do i_ = 1 to placesList.0
say placesList.i_
end i_
say
 
do i_ = 1 to sortedList.0
say sortedList.i_
end i_
say
 
return
End
Exit
 
/* -------------------------------------------------------------------------- */
bubbleSort:
procedure expose sortedList. placesList.
Do
/* Copy list */
do !_ = 0 to placesList.0
sortedList.!_ = placesList.!_
end !_
 
listLen = sortedList.0
do i_ = 1 to listLen
do j_ = i_ + 1 to listLen
if sortedList.i_ > sortedList.j_ then do
!_ = sortedList.j_
sortedList.j_ = sortedList.i_
sortedList.i_ = !_
end
end j_
end i_
return
End
Exit
 
/* -------------------------------------------------------------------------- */
sampleData:
procedure expose placesList.
Do
! = 0
! = ! + 1; placesList.0 = !; placesList.! = "UK London"
! = ! + 1; placesList.0 = !; placesList.! = "US New York"
! = ! + 1; placesList.0 = !; placesList.! = "US Boston"
! = ! + 1; placesList.0 = !; placesList.! = "US Washington"
! = ! + 1; placesList.0 = !; placesList.! = "UK Washington"
! = ! + 1; placesList.0 = !; placesList.! = "US Birmingham"
! = ! + 1; placesList.0 = !; placesList.! = "UK Birmingham"
! = ! + 1; placesList.0 = !; placesList.! = "UK Boston"
 
return
End
Exit
</lang>
 
=={{header|Oz}}==
Anonymous user