MATLAB code for bubble sort

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.

% 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
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(‘  ‘)
% The vector of numbers
disp (‘INPUTS’)
disp(‘Input the vector of numbers’)
A=[18  7  6  15  4  13];
% Number of entries, n
% 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

disp(‘  ‘)
disp (‘OUTPUT’)
disp (‘The ascending matrix is’)


This post is brought to you by Holistic Numerical Methods: Numerical Methods for the STEM undergraduate at, the textbook on Numerical Methods with Applications available from the lulu storefront, and the YouTube video lectures available at and

Subscribe to the blog via a reader or email to stay updated with this blog. Let the information follow you.

Author: Autar Kaw

Autar Kaw ( is a Professor of Mechanical Engineering at the University of South Florida. He has been at USF since 1987, the same year in which he received his Ph. D. in Engineering Mechanics from Clemson University. He is a recipient of the 2012 U.S. Professor of the Year Award. With major funding from NSF, he is the principal and managing contributor in developing the multiple award-winning online open courseware for an undergraduate course in Numerical Methods. The OpenCourseWare ( annually receives 1,000,000+ page views, 1,000,000+ views of the YouTube audiovisual lectures, and 150,000+ page views at the NumericalMethodsGuy blog. His current research interests include engineering education research methods, adaptive learning, open courseware, massive open online courses, flipped classrooms, and learning strategies. He has written four textbooks and 80 refereed technical papers, and his opinion editorials have appeared in the St. Petersburg Times and Tampa Tribune.

7 thoughts on “MATLAB code for bubble sort”

  1. hi this is shekar
    plz give matlab code for the shear sorting algorithm ………………..plzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: