A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Selection Sort in COBOL page! Here, you'll find the source code for this program as well as a description of how the program works.
identification division.
program-id. selection-sort.
data division.
working-storage section.
01 max-entries constant as 1000.
01 argument-string pic x(32768).
01 current-token pic x(128).
01 scan-ptr binary-long.
01 sort-table.
05 list-size binary-long value 0.
05 entry-value pic s9(9) occurs 0 to max-entries
depending on list-size.
01 work-vars.
05 i binary-long.
05 j binary-long.
05 min-index binary-long.
05 temp pic s9(9).
01 output-formatting.
05 display-num pic ---------9.
05 separator pic x(2) value ", ".
procedure division.
main.
perform validate-input
perform parse-input
perform execute-sort
perform display-results
goback.
validate-input.
accept argument-string from argument-value
if argument-string = spaces
perform show-usage
end-if.
parse-input.
move 1 to scan-ptr
perform until scan-ptr > length of argument-string
move spaces to current-token
unstring argument-string delimited by ","
into current-token
with pointer scan-ptr
end-unstring
if function trim(current-token) not = spaces
if function test-numval(function trim(current-token)) <> 0
perform show-usage
end-if
add 1 to list-size
if list-size > max-entries
perform show-usage
end-if
move function numval(current-token)
to entry-value(list-size)
end-if
end-perform
if list-size < 2
perform show-usage
end-if.
execute-sort.
perform varying i from 1 by 1 until i >= list-size
move i to min-index
compute j = i + 1
perform varying j from j by 1 until j > list-size
if entry-value(j) < entry-value(min-index)
move j to min-index
end-if
end-perform
if min-index not = i
move entry-value(i) to temp
move entry-value(min-index) to entry-value(i)
move temp to entry-value(min-index)
end-if
end-perform.
display-results.
perform varying i from 1 by 1 until i > list-size
if i > 1
display separator with no advancing
end-if
move entry-value(i) to display-num
display function trim(display-num) with no advancing
end-perform
display space.
show-usage.
display
'Usage: please provide a list of at least two integers '
'to sort in the format "1, 2, 3, 4, 5"'
stop run.
Selection Sort in COBOL was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.