In the previous blog, we spelled out the bubble sort algorithm for putting an array of numbers in an ascending order. In this post, I am posting the matlab program. It is better to download the program as single quotes in the pasted version do not translate properly when pasted into a mfile editor of MATLAB or see the html version for clarity and sample output.
%% PUTTING AN VECTOR OF NUMBERS IN AN ASCENDING ORDER?
% Language : Matlab 2007a
% Authors : Autar Kaw
% Last Revised : November 8, 2009
% Abstract: This program shows you how to put a vector
% of numbers in an ascending order using the bubble sort method
clc
clear all
disp(‘This program shows the bubble sort method’)
disp(‘to put a vector of numbers in an ‘)
disp(‘ascending order’)
disp(‘Matlab 2007a’)
disp(‘Authors : Autar Kaw’)
disp(‘Last Revised : November 8, 2009’)
disp(‘http://nm.mathforcollege.com’)
disp(‘ ‘)
%% INPUTS
% The vector of numbers
disp (‘INPUTS’)
disp(‘Input the vector of numbers’)
A=[18 7 6 15 4 13];
disp(A)
%% SOLUTION
% Number of entries, n
n=length(A);
% making (n-1) passes
for j=1:1:n-1
% comparing each number with the next and swapping
for i=1:1:n-1
if A(i)>A(i+1);
% temp is a variable where the numbers are kept
% temporarily for the switch
temp=A(i);
A(i)=A(i+1);
A(i+1)=temp;
end
end
end
%% OUTPUT
disp(‘ ‘)
disp (‘OUTPUT’)
disp (‘The ascending matrix is’)
disp(A)
_______________________________________________________
This post is brought to you by Holistic Numerical Methods: Numerical Methods for the STEM undergraduate at http://nm.mathforcollege.com, the textbook on Numerical Methods with Applications available from the lulu storefront, and the YouTube video lectures available at http://nm.mathforcollege.com/videos and http://www.youtube.com/numericalmethodsguy
Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.
thaaaannnnnnnnnnnkkkkkkkkkkss
thaaaannnnnnnnnnnkkkkkkkkkkss
hi this is shekar
plz give matlab code for the shear sorting algorithm ………………..plzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
hi this is shekar
plz give matlab code for the shear sorting algorithm ………………..plzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
tranks from brazil !
tranks from brazil !
how to make we input in command window? not in editor
how to make we input in command window? not in editor
this is for array of distinct element only
meaning there should be no repeating elements
this is for array of distinct element only
meaning there should be no repeating elements
It will work for nondistinct elements as well.