A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Bubble 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 bubble_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
%to keep track of whether any changes have been made on each pass
flag = 1;
while flag == 1
flag = 0;
for i = 1:array_size-1
if array(i) > array(i+1)
temp = array(i+1);
array(i+1) = array(i);
array(i) = temp;
flag = 1;
end
end
end
%convert to string
result_string = num2str(array);
%replace space with ', '
result_string = regexprep(result_string, '\s+', ', ');
disp(result_string);
end
Bubble 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.