0% found this document useful (0 votes)
3 views

003-Bubble Sort Algorithm-V2

Uploaded by

teufackwamba
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

003-Bubble Sort Algorithm-V2

Uploaded by

teufackwamba
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

BUBBLE SORT

1
Bubble Sort
 A simple sorting algorithm.
 Repeatedly steps through the list to be sorted, compares each pair of
adjacent items and swaps them if they are in the wrong order. The pass
through the list is repeated until no swaps are needed, which indicates that
the list is sorted.
 If there are n elements to be sorted then, the process mentioned above
should be repeated n-1 times to get required result (i.e., n-1 passes). But, for
better performance, in second pass, last and second last elements are not
compared because, the proper element is automatically placed at last after
first pass. Similarly, in third pass, last and second last and second last and
third last elements are not compared and so on.

2
Bubble Sort
 The algorithm, which is a comparison sort, is named for
the way smaller elements "bubble" to the top of the list.
 Although the algorithm is simple, it is slow.
 However is useful to understand the improved mechanism of
others more efficient sort algorithms

3
Step-by-step example
 Let us take the array of numbers
 “-2, 45, 0, 11, -9",
 and sort the array from lowest to greatest number using bubble sort.
 In each step, elements pointed by are being compared.
 Four passes will be required.
 Maximum number of passes required is always one less the
number of elements.

4
After step 1 you can realize that numbers are not in order, therefore a second pass
is in order. After step 2 numbers are not in order, so a third pass is in order. After
step 3 numbers are not in order, then a fourth pass is needed. In the fourth pass
(i.e., n-1), bingo all numbers get in right place.
5
clc, clear
x=[-2,45,0,11,-9];
disp('Before sorting'); disp(x);
n=numel(x); % [number of elements in x]
ix=n-1; % [number of comparisons]

for j=1:n-1 % n-1 passes


for i=1:ix
Bubble Sort Algorithm % Swap elements in state of 'wrong order'
Sort elements in if x(i+1)<x(i)
ascending or descending T=x(i+1);
x(i+1)=x(i);
order
x(i)=T;
VERSION 1 end
end
ix=ix-1; % Reduce the number of comparisons
end
disp('After sorting'); disp(x);

6
clc, clear
x=[9,1,0,7,2,8,6,5,4,13];
disp('Before sorting'); disp(x);
n=numel(x); % [number of elements in x]
ix=n-1; % [number of comparisons]
for j=1:n-1 % n-1 passes
for i=1:ix
% Swap elements in state of 'wrong order'
Bubble Sort Algorithm if x(i+1)<x(i)
Sort elements in T=x(i+1);
ascending or descending x(i+1)=x(i);
x(i)=T;
order
ixnew=i;
VERSION 2 end
(more efficient) end
ix=ixnew; % Reduces the number of comparisons
end
disp('After sorting'); disp(x);

7
REFERENCES
 http://www.mathworks.com/matlabcentral/fileexchange/45125-sorting-
methods/content/Sorting%20Methods/bubblesort.m

8
function x = bubblesort4(x)
%--------------------------------------------------------------------------
% Syntax: sx = bubblesort(x);
%
% Inputs: x is a vector of length n
% Outputs: sx is the sorted (ascending) version of x
%
Another % Description: This function sorts the input array x in ascending order
Reference % using the bubble sort algorithm
%
% Complexity: O(n) best-case performance
% O(n^2) average-case performance
% O(n^2) worst-case performance
% O(1) auxiliary space
%
% Author: Brian Moore
% [email protected]
continue
% Date: January 5, 2014
%--------------------------------------------------------------------------

9
% Bubble sort version 4
n = length(x);
while (n > 0)
% Iterate through x
nnew = 0;
for i = 2:n % n is decreasing with each pass, until n=0
% Swap elements in state of “wrong order”
if (x(i) < x(i - 1))
x = mySwap(x, i, i - 1); % input whole x and returns new whole x
nnew = i; % keeps track of last item swapped
end
end
n = nnew; % reduces the number of cycles in the next iteration of for loop
end
How the while loop stops?
when n becomes 2, the for loop parameters are i=2:2 and it does only
end one cycle of the inner loop statements. If there is no longer a swap nnew
stays zero from the statement before the for loop and n=nnew makes n=0
then the while loop condition becomes 0>0, (i.e., false), therefore the loop
stops. Brilliant !!
10
mySwap function
function x = mySwap(x, i, j)
% Swap x(i) and x(j)
% Note: In practice, x should be passed by
% reference

val = x(i);
x(i) = x(j);
x(j) = val;
The function mySwap most
end continue be stored before attempts to
run bubblesort4 function
11
Call bubblesort4
>> x=[1,2,3,9,8,7,6,4,5,0]
x=
1 2 3 9 8 7 6 4 5 0
>> bubblesort4(x)
ans =
0 1 2 3 4 5 6 7 8 9

12

You might also like