Skip to content

Commit c53b64d

Browse files
committed
Adding Bubble Sort algorithm to sorting
1 parent 2e85284 commit c53b64d

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

algorithms/sorting/bubble_sort.m

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
%% Bubble sort algorithm:
2+
function list = bubble_sort(list)
3+
% function to sort vector 'list' with using the 'Bubble sort' algorithm
4+
% INPUT: 'list' array
5+
% OUTPUT: sorted array
6+
changed = true;
7+
count = numel(list);
8+
while(changed)
9+
changed = false;
10+
count = count - 1;
11+
for index = (1:count)
12+
if(list(index) > list(index+1))
13+
list([index index+1]) = list([index+1 index]); %swap
14+
changed = true;
15+
end
16+
17+
end
18+
end
19+
end
20+

0 commit comments

Comments
 (0)