A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Selection Sort in Octave page! Here, you'll find the source code for this program as well as a description of how the program works.
function selection_sort()
%input validation
usage = 'Usage: please provide a list of at least two integers to sort in the format "1, 2, 3, 4, 5"';
arg_list = argv();
nargin = length(arg_list);
if nargin == 0
%if there was no input
disp(usage);
return;
end
array_string = arg_list{1};
array_size = sum(array_string == ',') + 1;
if array_size < 2
disp(usage);
return;
end
%build array
array = str2num(array_string);
if length(array) ~= array_size || any(mod(array, 1) ~= 0)
disp(usage);
return;
end
%selection sort in ascending order
sorted = [];
while ~isempty(array)
x = min(array);
sorted = [ sorted array(array==x) ];
array(array==x) = [];
end
%convert to string
result_string = num2str(sorted);
%replace space with ', '
result_string = regexprep(result_string, '\s+', ', ');
disp(result_string);
end
Selection Sort in Octave 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.