-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBubblesort.m
More file actions
30 lines (26 loc) · 855 Bytes
/
Bubblesort.m
File metadata and controls
30 lines (26 loc) · 855 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
function [array, index] = Bubblesort(vector)
% Functie bubblesort simpla care primeste un vector ce va fi sortat
% Scoate ca output vectorul sortat ( array ) si indexul cu nodurile
% aflam lungimea vectorului si formam vectorul de noduri
n = length(vector);
nod = 1:n;
% implementam algoritm Bubblesort
for i= 1:n
for j= 1:(n-1)
% aplicam sortarea in ordine descrescatoare
if vector(j) < vector(j+1)
% aux schimba elementele din vector
aux = vector(j);
vector(j) = vector(j+1);
vector(j+1) = aux;
% temp schimba elementele din vectorul de noduri
temp = nod(j);
nod(j) = nod(j+1);
nod(j+1) = temp;
end
end
end
% setam output urile
array = vector;
index = nod;
end