Fondamenti di Informatica
Transcription
Fondamenti di Informatica
Fondamenti di Informatica Sorting Algorithms Prof. Emiliano Casalicchio [email protected] Sorting algorithms n selection sort n Insertion sort ■ incremental algorithms ■ progressively extend a sorted sub-sequence until the collection (vector) is sorted n bubble sort ■ based on multiple scans of the array ■ swap adjacent elements until the collection is sorted 2 Selection sort [ 1 2 5 9 7 6 8] i =1 2 3 4 5 6 7 n Let us suppose the first k elements of a vector (array) are sorted ■ e.g. k=3 n how can we extend the sorting to the k+1 element? ■ Selection sort: pick the minimum of the n-k remaining element and assign it to the k+1 position (with a swap) 3 Selection sort [ 1 2 5 9 7 6 8] i =1 2 3 4 5 6 7 n Let us suppose the first k elements of a vector (array) are sorted n how can we extend the sorting to the k+1 element? ■ Selection sort: k=3 [ 1 2 5 9 7 6 8] i =1 2 3 4 5 6 7 à [1 i =1 2 2 5 3 6 4 7 5 9 6 8] 7 k=4 4 The Selection Sort Algorithm n = length of the sequence 1. Take the k-th element of the sorted subsequence 2. Find the minimum of the n-k unsorted elements 3. Swap the k-th elements with the minimum 4. If k<n go to step 1 5. End 5 Selection sort: how it works (1) 7 2 4 5 3 1 (2) 1 2 4 5 3 7 (3) 1 2 4 5 3 7 (4) 1 2 3 5 4 7 (5) 1 2 3 4 5 7 (6) 1 2 3 4 5 7 (7) 1 2 3 4 5 7 6 The Code function v = SelectionSort( v ) %Sort the vector v using the SelectionSort algorithm %INPUT: a vector of numbers v %OUTPUT: a vector v of number sorted in ascending order for i=1:length(v)-2 m=i; %the k element of the sorted sub%sequence for j=i+1:length(v) %find the minimum of the n-k unsorted %elements if v(j)<v(m) m=j; end end t=v(m); % Swap the k-th elements with the minimum v(m)=v(i); v(i)=t; end end 7 Homework n Modificare il codice in modo tale da ottenere la seguente funzione ■ [v num_comp num_swap] = SelectionSort( v ) n dove ■ num_comp contiene il numero di confronti eseguiti ■ num_swap contiene il numero effettivo di swap eseguiti 8 Insertion sort [ 1 2 5 9 7 6 8] i =1 2 3 4 5 6 7 n Let us suppose the first k elements of a vector (array) are sorted ■ e.g. k=3 n how can we extend the sorting to the k+1 element? ■ Insertion sort: pick the (k+1)-th elements of the array and insert it into the correct position respect to the first k elements 9 Insertion sort [ 1 2 5 9 7 6 8] i =1 2 3 4 5 6 7 n Let us suppose the first k elements of a vector (array) are sorted n how can we extend the sorting to the k+1 element? ■ Insertion sort: k=4 [ 1 2 5 9 7 6 8] i =1 2 3 4 5 6 7 à [1 i =1 2 2 5 3 7 4 9 5 6 6 8] 7 10 The Insertion Sort Algorithm n = length of the sequence 1. Take the (k+1)-th element of the sequence 2. Find the position for the (k+1) element in the sequence 3. If the new position of (k+1) element is less than k+1 then, is needed a shift of the already sorted elements 4. If k<n go to step 1 5. End 11 Insertion sort: how it works (1) 7 2 4 5 3 1 (2) 2 7 4 5 3 1 (3) 2 4 7 5 3 1 (4) 2 4 5 7 3 1 (5) 2 3 4 5 7 1 (6) 1 2 3 4 5 7 12 The code function v = InsertionSort( v ) %Sort the vector v using the InsertionSort algorithm %INPUT: a vector of numbers v %OUTPUT: a vector v of number sorted in ascending order for i=2:length(v) x=v(i); %the k+1 element of the sequence (vector) for j=1:i %find the position for k+1 element in the %sequense if v(j)>x break; end end if j < i %is needed a shift of the already sorted %elements for l=i-1:-1:j v(l+1)=v(l); end v(j)=x; end end 13 function v = InsertionSort( v ) The code %Sort the vector v using the InsertionSort algorithm %INPUT: a vector of numbers v %OUTPUT: a vector v of number sorted in ascending order for i=2:length(v) x=v(i); %the k+1 element of the sequence (vector) for j=1:i %find the position for k+1 element in the %sequense if v(j)>x break; end end if j < i %is needed a shift of the already sorted %elements for l=i-1:-1:j v(l+1)=v(l); end v(j)=x; end end (1) 7 2 4 5 3 1 (2) 2 7 4 5 3 1 (3) 2 4 7 5 3 1 (4) 2 4 5 7 3 1 (5) 2 3 4 5 7 1 (6) 2 3 4 5 7 1 14 Debug n Using debugging tools we can observe the sequence of algorithm steps and how the sorted sequence is created 15 Homework n Modificare il codice in modo tale da ottenere la seguente funzione ■ [v num_comp num_shift] = InsertionSort( v ) n dove ■ num_comp contiene il numero di confronti eseguiti ■ num_shift contiene il numero effettivo di shift eseguiti 16 Homework n Eseguire gli algoritmi InsertionSort e SelectionSort su di un insieme di 15 vettori generati casualmente e confrontare i risultati, ovvero: num_comp e num_shift n ad es: for i=1:15 v=rand(1,10) [~ num_comp_IS(i) num_shift_IS(i)]=InsertionSort(v) [~ num_comp_SS(i) num_swap_SS(i)]=SelectionSort(v) end 17 Bubble sort n based on multiple scans of the array n on each scan, couple of adjacent elements are compared and swapped (if needed) n if no swap are operated, the array is sorted 18 Bubble sort (1) 7 2 2 7 4 4 5 4 2 4 4 5 3 7 1 7 1 7 3 (4) (5) 5 1 4 2 4 2 4 7 5 3 2 (3) 3 3 2 1 7 5 (2) 3 1 5 5 7 5 7 2 3 2 3 3 1 5 7 4 1 4 1 4 5 7 1 3 2 1 3 4 5 7 1 2 1 2 3 4 5 7 19 the algorithm function v = BubbleSort( v ) %Sort the vector v using the BubbleSort algorithm %INPUT: a vector of numbers v %OUTPUT: a vector v of number sorted in ascending order for i=1:length(v)-1 %up to n-1 scans (1) swap=false; for j=2:length(v)-(i-1) %do swap if v(j-1)>v(j) x=v(j); v(j)=v(j-1); v(j-1)=x; swap=true; end (2) end if ~swap break; end end end 7 2 2 7 4 4 5 7 3 4 2 4 4 5 3 1 7 1 7 5 1 4 7 5 3 2 1 7 5 2 3 3 1 5 5 7 5 7 20 Homework n Modificare il codice in modo tale da ottenere la seguente funzione ■ [v num_comp num_swap] = BubbleSort( v ) n dove ■ num_comp contiene il numero di confroni eseguiti ■ num_swap contiene il numero effettivo di swap eseguiti 21 Homework n Implementare gli algoritmi di ordinamento con il costrutto while n Gli algoritmi visti fino ad ora effettuano un ordinamento crescente. ■ Progettare ed implementare un algoritmo di ordinamento decrescente che utilizzi la tecnica dell’insertion sort ■ Progettare ed implementare un algoritmo di ordinamento decrescente che utilizzi la tecnica del selection sort 22