Selection Sort in Octave

Published on 09 December 2023 (Updated: 09 December 2023)

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.

Current Solution

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.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.