-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbubble.m
More file actions
32 lines (29 loc) · 915 Bytes
/
Copy pathbubble.m
File metadata and controls
32 lines (29 loc) · 915 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
% Perform bubble sorting algorithms.
% 5 October 2021.
% BUBBLESORT.A/
% (1) for i = 1 to A:length - 1
% (2) for j = A:length downto i + 1
% (3) if A[j] < A[j - 1]
% (4) exchange A[j] with A[j] - 1"
% Matlab program for implementation of Bubble Sort
% Written by Orhan Ozan Yildiz.
function [array, comp_bubble] = bubble(array)
% Measure the computational time of the method.
tic;
arrayLength = size(array,2);
% for i = 1 to A:length - 1
for i = 1: (arrayLength+1)
% for j = A:length downto i + 1
for j = arrayLength:-1:(i + 1)
% if A[j] < A[j - 1]
if array(j) < array(j - 1)
% exchange A[j] with A[j - 1]"
temp = array(j);
array(j) = array(j - 1);
array(j-1) = temp;
end
end
end
% Read elapsed time from stopwatch.
comp_bubble = toc;
end