Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function x = SHEET3TASK2(x)
- n = numel(x);
- if n == 1
- return
- end
- min_ind = finde_min(x,n);
- if min_ind > 1
- x = tausche(x,1,min_ind);
- end
- x(2:n) = SHEET3TASK2(x(2:n));
- end
- function ind = finde_min(x,n)
- ind = 1;
- ind_value = x(1);
- for k=2:n
- if x(k) < ind_value
- ind = k;
- ind_value = x(k);
- end
- end
- end
- function x = tausche(x,ind1,ind2)
- merke_x_ind1 = x(ind1);
- x(ind1) = x(ind2);
- x(ind2) = merke_x_ind1;
- end
- --------------------
- function [x,indices] = SHEET3TASK2b(x)
- n = numel(x);
- indices = 1:n;
- if n==1
- return
- end
- for k=1:n-1
- min_ind = finde_min(x(k:n),n-k+1);
- %disp(["k=",k, ": ", "min_ind=",min_ind])
- if min_ind > 1
- x = tausche(x,k,min_ind+k-1);
- indices = tausche(indices,k,min_ind+k-1);
- end
- end
- end
- function ind = finde_min(x,n)
- ind = 1;
- ind_value = x(1);
- for k=2:n
- if x(k) < ind_value
- ind = k;
- ind_value = x(k);
- end
- end
- end
- function x = tausche(x,ind1,ind2)
- merke_x_ind1 = x(ind1);
- x(ind1) = x(ind2);
- x(ind2) = merke_x_ind1;
- end
- -----------
- function x_sorted = SHEET3TASK3(x)
- n = numel(x);
- if n<=1
- return
- end
- mitte = floor(n/2);
- links = x(1:mitte);
- links = SHEET3TASK3(links);
- rechts = x(mitte+1:n);
- rechts = SHEET3TASK3(rechts);
- x_sorted = verschmelze(links,rechts);
- end
- function x = verschmelze(links,rechts)
- n_links = numel(links);
- n_rechts = numel(rechts);
- x = zeros(1,n_links+n_rechts);
- k = 1;
- while n_links ~= 0 && n_rechts ~= 0
- if rechts(1) < links(1)
- x(k) = rechts(1);
- rechts = rechts(2:end);
- n_rechts = n_rechts-1;
- else
- x(k) = links(1);
- links = links(2:end);
- n_links = n_links-1;
- end
- k = k+1;
- end
- if n_links == 0 && n_rechts == 1
- x(end-n_rechts+1) = rechts(1:n_rechts);
- end
- if n_rechts == 0 && n_links == 1
- x(end-n_links+1) = links(1:n_links);
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement