ΔΟΜΕΣ ΔΕΔΟΜΕΝΩΝ & ΑΛΓΟΡΙΘΜΟΙ ΕΡΓΑΣΤΗΡΙΟ Κωδικός Θ: ΤΠ3001, Κωδικός Ε: ΤΠ3101 (ΜΕΥ/Υ) Ώρες (Θ - Ε): 4-2 Προαπαιτούμενα: Δρ. ΒΙΔΑΚΗΣ ΝΙΚΟΣ
ΕΡΓΑΣΤΗΡΙΟ 5 Ταξινόμηση με 1. Insertion Sort 2. Selection Sort
Insertion Sort Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 3
Introduction Is similar to bubble sort. Is more efficient than bubble as the number of element comparisons are less compared to bubble sort. Compares the value until all prior elements are lesser than the compared value. Is a good choice for small or nearly-sorted arrays. More efficient algorithms are: quick, heap, or merge. Positive feature of insertion sorting 1. Simple to implement 2. Efficient on (quite) small or nearly sorted arrays 3. Adaptive (performance adapts to the initial order of elements) 4. Stable (insertion sort retains relative order of the same elements) 5. In-place (requires constant amount of additional space) 6. Online (new elements can be added during the sort). Complexity The complexity is O(n) at best case of an already sorted array and O(n2) at worst case. Insertion Sort Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 4
Insertion Sort Insertion sort pseudocode (recall) InsertionSort(A) **sort A[1..n] in place for j 2 to n do key A[j] **insert A[j] into sorted sublist A[1..j 1] i j 1 while (i > 0 and A[i] > key) do A[i+1] A[i] i i 1 A[i+1] key becomes Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 5
Insertion Sort Code Description Insertion sort algorithm somewhat resembles selection sort. Array is imaginary divided into two parts sorted one and unsorted one. At the beginning, sorted part contains first element of the array and unsorted one contains the rest. At every step, algorithm takes first element in the unsorted part and inserts it to the right place of the sorted one. When unsorted part becomes empty, algorithm stops. Sketchy, insertion sort algorithm step looks like this: becomes Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 6
Insertion Sort visual paradigm 1 Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 7
Insertion Sort visual paradigm 1 (cont.) Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 8
Insertion Sort visual paradigm 2 ("Sifting down" using swaps) This approach writes sifted element to temporary position many times. Next paradigm eliminates those unnecessary writes Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 9
Insertion Sort visual paradigm 3 (Sifting instead of swapping) is the most commonly used modification of the insertion sort Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 10
Insertion Sort visual paradigm 4 Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 11
Insertion Sort example code 1 void insertionsort(int[] arr) { int i, j, newvalue; for (i = 1; i < arr.length; i++) { newvalue = arr[i]; j = i; while (j > 0 && arr[j - 1] > newvalue) { arr[j] = arr[j - 1]; j--; arr[j] = newvalue; Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 12
Insertion Sort example code 2 in Java public class InsertionSort{ public static void main(string a[]){ int i; int array[] = {12,9,4,99,120,1,3,10; System.out.println("\n\n RoseIndia\n\n"); System.out.println(" Selection Sort\n\n"); System.out.println("Values Before the sort:\n"); for(i = 0; i < array.length; i++) System.out.print( array[i]+" "); System.out.println(); insertion_srt(array, array.length); System.out.print("Values after the sort:\n"); for(i = 0; i <array.length; i++) System.out.print(array[i]+" "); System.out.println(); System.out.println("PAUSE"); Example code taken from: http://www.roseindia.net/java/beginners/arrayexamples/index.shtml Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 13
Insertion Sort example code 3 in C++ void insertionsort(int arr[], int length) { int i, j, tmp; for (i = 1; i < length; i++) { j = i; while (j > 0 && arr[j - 1] > arr[j]) { tmp = arr[j]; arr[j] = arr[j - 1]; arr[j - 1] = tmp; j--; Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 14
Analisys Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 array[ ] = {12, 9, 4, 99, 120, 1, 3, 10, n = 8; i = 1; i < n; 1 < 8 true enters the for j = i; j = 1; Insertion Sort B = array[j]; B = array[1] = 9; ((1>0) && (12 >9)) true enters the while array[j] = array[j-1] array[1] = array[0] = 12; j--; j =0; ((0>0) && (#** > 9)) false not in while array[j] = B array[0] = 9; public static void insertion_srt(int array[], int n){ for (int i = 1; i < n; i++){ int j = i; int B = array[i]; while ((j > 0) && (array[j-1] > B)){ array[j] = array[j-1]; j--; array[j] = B; new array[ ] = {9, 12, 4, 99, 120, 1, 3, 10; Example code taken from: http://www.roseindia.net/java/beginners/arrayexamples/index.shtml Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 15
Analisys Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 from previus run: array[ ] = {9, 12, 4, 99, 120, 1, 3, 10; i = 2; i < n; 2 < 8 true enters the for j = i; j = 2; Insertion Sort B = array[j]; B = array[2] = 4; ((2>0) && (12 >4)) true enters the while array[j] = array[j-1] array[2] = array[1] = 12; j--; j =1; ((1>0) && (9 > 4)) true enters the while array[j] = array[j-1] array[1] = array[0] = 9; j--; j = 0; ((0>0) && (9 > 4)) false not in while public static void insertion_srt(int array[], int n){ for (int i = 1; i < n; i++){ int j = i; int B = array[i]; while ((j > 0) && (array[j-1] > B)){ array[j] = array[j-1]; j--; array[j] = B; array[j] = B array[0] = 4; new array[ ] = {4, 9, 12, 99, 120, 1, 3, 10; Example code taken from: http://www.roseindia.net/java/beginners/arrayexamples/index.shtml Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 16
Analisys Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 from previus run: array[ ] = {4, 9, 12, 99, 120, 1, 3, 10; i = 3; i < n; 3 < 8 true enters the for Insertion Sort j = i; j = 3; B = array[j]; B = array[3] = 99; public static void insertion_srt(int array[], int n){ for (int i = 1; i < n; i++){ int j = i; int B = array[i]; ((3>0) && (12 > 99)) false not in while while ((j > 0) && (array[j-1] > B)){ array[j] = array[j-1]; j--; array[j] = B; array[j] = B array[3] = 99; new array[ ] = {4, 9, 12, 99, 120, 1, 3, 10; same as before Example code taken from: http://www.roseindia.net/java/beginners/arrayexamples/index.shtml Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 17
Analisys Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 from previus run: array[ ] = {4, 9, 12, 99, 120, 1, 3, 10; i = 4; i < n; 4 < 8 true enters the for Insertion Sort j = i; j = 4; B = array[j]; B = array[4] = 120; public static void insertion_srt(int array[], int n){ for (int i = 1; i < n; i++){ int j = i; int B = array[i]; ((4>0) && (99 >120)) false not in while while ((j > 0) && (array[j-1] > B)){ array[j] = array[j-1]; j--; array[j] = B; array[j] = B array[4] = 120; new array[ ] = {4, 9, 12, 99, 120, 1, 3, 10; same as before Example code taken from: http://www.roseindia.net/java/beginners/arrayexamples/index.shtml Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 18
Analisys Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 from previus run: array[ ] = {4, 9, 12, 99, 120, 1, 3, 10; i = 5; i < n; 5 < 8 true enters the for j = i; j = 5; B = array[j]; B = array[5] = 1; Insertion Sort public static void insertion_srt(int array[], int n){ ((5>0) && (120 > 1)) true enters the while array[j] = array[j-1] array[5] = array[4] = 120; j--; j =4; ((4>0) && (99 > 1)) true enters the while array[j] = array[j-1] array[4] = array[3] = 99; j--; j =3; ((3>0) && (12 > 1)) true enters the while array[j] = array[j-1] array[3] = array[2] = 12; j--; j =2; ((2>0) && (9 > 1)) true enters the while array[j] = array[j-1] array[2] = array[1] = 9; j--; j =1; ((1>0) && (4 > 1)) true enters the while array[j] = array[j-1] array[1] = array[0] = 4; j--; j =0; ((0>0) && (4 > 1)) false not in while array[j] = B array[0] = 1; new array[ ] = {1,4, 9, 12, 99, 120, 3, 10; for (int i = 1; i < n; i++){ int j = i; int B = array[i]; while ((j > 0) && (array[j-1] > B)){ array[j] = array[j-1]; j--; array[j] = B; Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 19
Analisys Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 from previus run: array[ ] = {1,4, 9, 12, 99, 120, 3, 10; i = 6; i < n; 6 < 8 true enters the for j = i; j = 6; B = array[j]; B = array[6] = 3; Insertion Sort public static void insertion_srt(int array[], int n){ ((6>0) && (120 > 3)) true enters the while array[j] = array[j-1] array[6] = array[5] = 120; j--; j =5; ((5>0) && (99 > 3)) true enters the while array[j] = array[j-1] array[5] = array[4] = 99; j--; j =4; ((4>0) && (12 > 3)) true enters the while array[j] = array[j-1] array[4] = array[3] = 12; j--; j =3; ((3>0) && (9 > 3)) true enters the while array[j] = array[j-1] array[3] = array[2] = 9; j--; j =2; ((2>0) && (4 > 3)) true enters the while array[j] = array[j-1] array[2] = array[1] = 4; j--; j =1; ((1>0) && (1 > 3)) false not in while array[j] = B array[1] = 3; new array[ ] = {1, 3, 4, 9, 12, 99, 120, 10; for (int i = 1; i < n; i++){ int j = i; int B = array[i]; while ((j > 0) && (array[j-1] > B)){ array[j] = array[j-1]; j--; array[j] = B; Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 20
Analisys Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 from previus run: array[ ] = {1, 3, 4, 9, 12, 99, 120, 10; i = 7; i < n; 7 < 8 true enters the for j = i; j = 7; B = array[j]; B = array[7] = 10; Insertion Sort public static void insertion_srt(int array[], int n){ ((7>0) && (120 > 10)) true enters the while array[j] = array[j-1] array[7] = array[6] = 120; j--; j =6; ((6>0) && (99 > 10)) true enters the while array[j] = array[j-1] array[6] = array[5] = 99; j--; j =5; ((5>0) && (12 > 10)) true enters the while array[j] = array[j-1] array[5] = array[4] = 12; j--; j =4; ((4>0) && (9 > 10)) false not in while for (int i = 1; i < n; i++){ int j = i; int B = array[i]; while ((j > 0) && (array[j-1] > B)){ array[j] = array[j-1]; j--; array[j] = B; array[j] = B array[4] = 10; new array[ ] = {1, 3, 4, 9,10, 12, 99, 120; Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 21
Insertion Sort Analisys Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 public static void insertion_srt(int array[], int n){ i = 8; i < n; 8 < 8 false not in for After 8 runs of the for loop (which are as many as the elements in the array) the array is finaly sorted. The while statement runs: 16 times. SORTED ARRAY: {1, 3, 4, 9,10, 12, 99, 120; for (int i = 1; i < n; i++){ int j = i; int B = array[i]; while ((j > 0) && (array[j-1] > B)){ array[j] = array[j-1]; j--; array[j] = B; Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 22
Selection Sort Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 23
Selection Sort Introduction The idea of the algorithm is quite simple. The array is imaginary divided into two parts - sorted one and unsorted one. At the beginning, sorted part is empty, while unsorted one contains whole array. Find the minimum value in the array swap it first position In next step leave the first value and find the minimum value within remaining values Then swap it with the value of minimum index position Sort the remaining values by using same steps. Selection sort is probably the most intuitive sorting algorithm to invent.. Complexity The complexity in worst-case, average-case, and best-case run-time of Θ(n2), assuming that comparisons can be done in constant time. Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 24
Selection Sort Working of the algorithm Say we have an array unsorted A[0],A[1],A[2]... A[n-1] and A[n] as input. Then the following steps are followed by selection sort algorithm to sort the values of an array. (Say we have a key index_of_min that indicate the position of minimum value) 1. Initially variable index_of_min=0; 2. Find the minimum value in the unsorted array. 3. Assign the index of the minimum value into index_of_min variable. 4. Swap minimum value to first position. 5. Sort the remaining values of array (excluding the first value). Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 25
Selection Sort visual paradigm 1 (using the minimum value) Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 26
Selection Sort visual paradigm 2 (using the maximum value) Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 27
Selection Sort example code 1 in Java public void selectionsort(int[] arr) { int i, j, minindex, tmp; int n = arr.length; for (i = 0; i < n - 1; i++) { minindex = i; for (j = i + 1; j < n; j++) if (arr[j] < arr[minindex]) minindex = j; if (minindex!= i) { tmp = arr[i]; arr[i] = arr[minindex]; arr[minindex] = tmp; Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 28
Selection Sort example code 2 in Java public class selectionsort{ public static void main(string a[]){ int i; int array[] = {12,9,4,99,120,1,3,10; System.out.println("\n\n RoseIndia\n\n"); System.out.println(" Selection Sort\n\n"); System.out.println("Values Before the sort:\n"); for(i = 0; i < array.length; i++) System.out.print( array[i]+" "); System.out.println(); selection_srt(array, array.length); System.out.print("Values after the sort:\n"); for(i = 0; i <array.length; i++) System.out.print(array[i]+" "); System.out.println(); System.out.println("PAUSE"); Example code taken from: http://www.roseindia.net/java/beginners/arrayexamples/index.shtml Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 29
Selection Sort example code 2 (cont.) in Java public static void selection_srt(int array[], int n){ for(int x=0; x<n; x++){ int index_of_min = x; for(int y=x; y<n; y++){ if(array[index_of_min]<array[y]){ index_of_min = y; int temp = array[x]; array[x] = array[index_of_min]; array[index_of_min] = temp; Example code taken from: http://www.roseindia.net/java/beginners/arrayexamples/index.shtml Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 30
C++ void selectionsort(int arr[], int n) { int i, j, minindex, tmp; for (i = 0; i < n - 1; i++) { minindex = i; for (j = i + 1; j < n; j++) if (arr[j] < arr[minindex]) minindex = j; if (minindex!= i) { tmp = arr[i]; arr[i] = arr[minindex]; arr[minindex] = tmp; Selection Sort example code 3 in C void selection_sort(int num[size]) { int i, j, min; for (i = 0; i < (SIZE-1); i++) { min = i; for (j = (i+1); j < SIZE; j++) { if(num[j] < num[min]) { min = j; if (i!= min) { int swap = num[i]; num[i] = num[min]; num[min] = swap; Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 31
Selection Sort example code 3 in Visual Basic Sub selection_sort(num() As Integer) Dim i, j, min As Integer For i = 0 to num().length - 1 min = i For j = i+1 to num().length If num(j) < num(min) Then min = j End If Next If i <> min Then Dim swap As Integer = num(i) num(i) = num(min) num(min) = swap End If Next End Sub Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 32
Selection Sort time taken n Milliseconds 10,000 772 20,000 3,051 30,000 6,846 40,000 12,188 50,000 19,015 60,000 27,359 Obtained with a Pentium processor, 1.2 GHz, Java 5.0, Linux Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 33
Selection Sort Code Analisys Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 array[ ] = {12, 9, 4, 99, 120, 1, 3, 10, n = 8; public static void selection_srt(int array[], int n){ x = 0 x = 0; x < n 0 < 8 true, enters for y = x y = 0; index_of_max = x y < n 0 < 8 true, enters for index_of_max = 0; array[inder_of_max] < array [y] 12 < 12 false, not in if y++ y = 1; y < n 1 < 8 true, enters for array[index_of_max] < array [y] 12 < 9 false, not in if y++ y = 2; y < n 2 < 8 true, enters for array[index_of_max] < array [y] 12 < 4 false, not in if y++ y = 3; y < n 3 < 8 true, enters for array[index_of_max] < array [y] 12 < 99 true, enters if index_of_max= y index_of_max = 3 y++ y = 4; y < n 4 < 8 true, enters for array[index_of_max] < array [y] 99 < 120 true, enters if index_of_max = y index_of_max = 4 y++ y = 5; y < n 5 < 8 true, enters for array[index_of_max] < array [y] 120 < 1 false, not in if CONTINUED IN NEXT PAGE for(int x=0; x<n; x++){ int index_of_min = x; for(int y=x; y<n; y++){ if(array[index_of_min]<array[y]){ index_of_min = y; int temp = array[x]; array[x] = array[index_of_min]; array[index_of_min] = temp; Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 34
Continued Selection Sort Code Analisys Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 public static void selection_srt(int array[], int n){ y++ y = 6; y < n 6 < 8 true, enters for array[inder_of_max] < array [y] 120 < 3 false, not in if y++ y = 7; y < n 7 < 8 true, enters for array[index_of_max] < array [y] 120 < 10 false, not in if y++ y = 8; y < n 8 < 8 false, not in for So after 8 runs, the for loop stops and we have: index_of_max = 4 and x = 0; temp = array[0] temp = 12 array[x] = array[4] array[0] = 120 array[index_of_max] = temp array[4] = 12 new array[ ] = {120, 9, 4, 99, 12, 1, 3, 10 for(int x=0; x<n; x++){ int index_of_min = x; for(int y=x; y<n; y++){ if(array[index_of_min]<array[y]){ index_of_min = y; int temp = array[x]; array[x] = array[index_of_min]; array[index_of_min] = temp; Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 35
Selection Sort Code Analisys Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 array[ ] = {120, 9, 4, 99, 12, 1, 3, 10 public static void selection_srt(int array[], int n){ x++ x = 1; x < n 1 < 8 true, enters for y = x y = 1; index_of_max = x y < n 1 < 8 true, enters for index_of_max = 1; array[inder_of_max] < array [y] 9 < 9 false, not in if y++ y = 2; y < n 2 < 8 true, enters for array[index_of_max] < array [y] 9 < 4 false, not in if y++ y = 3; y < n 3 < 8 true, enters for array[index_of_max] < array [y] 9 < 99 true, enters if index_of_max = y index_of_max = 3 y++ y = 4; y < n 4 < 8 true, enters for array[index_of_max] < array [y] 99 < 12 false, not in if for(int x=0; x<n; x++){ int index_of_min = x; for(int y=x; y<n; y++){ if(array[index_of_min]<array[y]){ index_of_min = y; int temp = array[x]; array[x] = array[index_of_min]; array[index_of_min] = temp; y++ y = 5; y < n 5 < 8 true, enters for array[index_of_max] < array [y] 99 < 1 false, not in if y++ y = 6; y < n 5 < 8 true, enters for array[index_of_max] < array [y] 99 < 3 false, not in if CONTINUED IN NEXT PAGE Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 36
Continued Selection Sort Code Analisys Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 public static void selection_srt(int array[], int n){ y++ y = 7; y < n 7 < 8 true, enters for array[index_of_max] < array [y] 99 < 10 false, not in if y++ y = 8; y < n 8 < 8 false, not in for So after 7 runs, the for loop stops and we have: index_of_max = 3 and x = 1; temp = array[1] temp = 9 array[x] = array[index_of_max] array[1] = 99 array[index_of_max] = temp array[3] = 9 new array[ ] = {120, 99, 4, 9, 12, 1, 3, 10 for(int x=0; x<n; x++){ int index_of_min = x; for(int y=x; y<n; y++){ if(array[index_of_min]<array[y]){ index_of_min = y; int temp = array[x]; array[x] = array[index_of_min]; array[index_of_min] = temp; Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 37
Selection Sort Code Analisys Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 array[ ] = {120, 99, 4, 9, 12, 1, 3, 10 public static void selection_srt(int array[], int n){ x++ x = 2; x < n 2 < 8 true, enters for y = x y = 2; index_of_max = x y < n 2 < 8 true, enters for index_of_max = 2; array[inder_of_max] < array [y] 4 < 4 false, not in if y++ y = 3; y < n 3 < 8 true, enters for array[index_of_max] < array [y] 4 < 9 true, enters index_of_max = y index_of_max = 3 y++ y = 4; y < n 4 < 8 true, enters for array[index_of_max] < array [y] 9 < 12 true, enters if index_of_max = y index_of_max = 4 y++ y = 5; y < n 5 < 8 true, enters for array[index_of_max] < array [y] 12 < 1 false, not in if y++ y = 6; y < n 6 < 8 true, enters for array[index_of_max] < array [y] 12 < 3 false, not in if y++ y = 7; y < n 7 < 8 true, enters for array[index_of_max] < array [y] 12 < 10 false, not in if CONTINUED IN NEXT PAGE for(int x=0; x<n; x++){ int index_of_min = x; for(int y=x; y<n; y++){ if(array[index_of_min]<array[y]){ index_of_min = y; int temp = array[x]; array[x] = array[index_of_min]; array[index_of_min] = temp; Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 38
Continued Selection Sort Code Analisys Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 public static void selection_srt(int array[], int n){ y++ y = 8; y < n 8 < 8 false, not in for for(int x=0; x<n; x++){ int index_of_min = x; for(int y=x; y<n; y++){ if(array[index_of_min]<array[y]){ index_of_min = y; So after 6 runs, the for loop stops and we have: index_of_max = 4 and x = 2 temp = array[2] temp = 4 array[x] = array[index_of_max] array[2] = 12 array[index_of_max] = temp array[4] = 4 int temp = array[x]; array[x] = array[index_of_min]; array[index_of_min] = temp; new array[ ] = {120, 99, 12, 9, 4, 1, 3, 10 Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 39
Selection Sort Code Analisys Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 array[ ] = {120, 99, 12, 9, 4, 1, 3, 10 public static void selection_srt(int array[], int n){ x++ x = 3; x < n 3 < 8 true, enters for y = x y = 3; index_of_max = x y < n 3 < 8 true, enters for index_of_max = 3; array[inder_of_max] < array [y] 9 < 9 false, not in if y++ y = 4; y < n 4 < 8 true, enters for array[index_of_max] < array [y] 9 < 4 false, not in if y++ y = 5; y < n 5 < 8 true, enters for array[index_of_max] < array [y] 9 < 1 false, not in if y++ y = 6; y < n 6 < 8 true, enters for array[index_of_max] < array [y] 9 < 3 false, not in if y++ y = 7; y < n 7 < 8 true, enters for array[index_of_max] < array [y] 9 < 10 true, enters if index_of_max = y index_of_max = 7 for(int x=0; x<n; x++){ int index_of_min = x; for(int y=x; y<n; y++){ if(array[index_of_min]<array[y]){ index_of_min = y; int temp = array[x]; array[x] = array[index_of_min]; array[index_of_min] = temp; CONTINUED IN NEXT PAGE Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 40
Continued Selection Sort Code Analisys Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 public static void selection_srt(int array[], int n){ y++ y = 8; y < n 8 < 8 false, not in for for(int x=0; x<n; x++){ int index_of_min = x; for(int y=x; y<n; y++){ if(array[index_of_min]<array[y]){ index_of_min = y; So after 5 runs, the for loop stops and we have: index_of_max = 7 and x = 3 temp = array[3] temp = 9 array[x] = array[index_of_max] array[3] = 10 array[index_of_max] = temp array[7] = 9 int temp = array[x]; array[x] = array[index_of_min]; array[index_of_min] = temp; new array[ ] = {120, 99, 12, 10, 4, 1, 3, 9 Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 41
Selection Sort Code Analisys Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 array[ ] = {120, 99, 12, 10, 4, 1, 3, 9 public static void selection_srt(int array[], int n){ x++ x = 4; x < n 4 < 8 true, enters for y = x y = 4; index_of_max = x y < n 4 < 8 true, enters for index_of_max = 4; array[inder_of_max] < array [y] 4 < 4 false, not in if y++ y = 5; y < n 5 < 8 true, enters for array[index_of_max] < array [y] 4 < 1 false, not in if y++ y = 6; y < n 6 < 8 true, enters for array[index_of_max] < array [y] 4 < 3 false, not in if y++ y = 7; y < n 7 < 8 true, enters for array[index_of_max] < array [y] 4 < 9 true, enters if index_of_max = y index_of_max = 7 y++ y = 8; y < n 8 < 8 false, not in for So after 4 runs, the for loop stops and we have: index_of_max = 7 and x = 4 for(int x=0; x<n; x++){ int index_of_min = x; for(int y=x; y<n; y++){ if(array[index_of_min]<array[y]){ index_of_min = y; int temp = array[x]; array[x] = array[index_of_min]; array[index_of_min] = temp; temp = array[4] temp = 4 array[x] = array[index_of_max] array[4] = 9 array[index_of_max] = temp array[7] = 4 new array[ ] = {120, 99, 12, 10, 9, 1, 3, 4 Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 42
Selection Sort Code Analisys Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 array[ ] = {120, 99, 12, 10, 9, 1, 3, 4 public static void selection_srt(int array[], int n){ x++ x = 5; x < n 5 < 8 true, enters for y = x y = 5; index_of_max = x y < n 5 < 8 true, enters for index_of_max = 5; array[inder_of_max] < array [y] 1 < 1 false, not in if y++ y = 6; y < n 6 < 8 true, enters for array[index_of_max] < array [y] 1 < 3 true, enters if index_of_max = y index_of_max = 6 y++ y = 7; y < n 7 < 8 true, enters for array[index_of_max] < array [y] 3 < 4 true, enters if index_of_max = y index_of_max = 7 y++ y = 8; y < n 8 < 8 false, not in for for(int x=0; x<n; x++){ int index_of_min = x; for(int y=x; y<n; y++){ if(array[index_of_min]<array[y]){ index_of_min = y; int temp = array[x]; array[x] = array[index_of_min]; array[index_of_min] = temp; So after 3 runs, the for loop stops and we have: index_of_max = 7 and x = 5 temp = array[5] temp = 1 array[x] = array[index_of_max] array[5] = 4 array[index_of_max] = temp array[7] = 1 new array[ ] = {120, 99, 12, 10, 9, 4, 3, 1 Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 43
Selection Sort Code Analisys Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 array[ ] = {120, 99, 12, 10, 9, 4, 3, 1 public static void selection_srt(int array[], int n){ x++ x = 6; x < n 6 < 8 true, enters for y = x y = 6; index_of_max = x y < n 6 < 8 true, enters for index_of_max = 6; array[inder_of_max] < array [y] 3 < 3 false, not in if y++ y = 7; y < n 7 < 8 true, enters for array[index_of_max] < array [y] 3 < 1 false, not in if y++ y = 8; y < n 8 < 8 false, not in for for(int x=0; x<n; x++){ int index_of_min = x; for(int y=x; y<n; y++){ if(array[index_of_min]<array[y]){ index_of_min = y; int temp = array[x]; array[x] = array[index_of_min]; array[index_of_min] = temp; So after 2 runs, the for loop stops and we have: index_of_max = 6 and x = 6 temp = array[6] temp = 3 array[x] = array[index_of_max] array[6] = 3 array[index_of_max] = temp array[6] = 3 new array[ ] = {120, 99, 12, 10, 9, 4, 3, 1 Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 44
Selection Sort Code Analisys Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 array[ ] = {120, 99, 12, 10, 9, 4, 3, 1 public static void selection_srt(int array[], int n){ x++ x = 7; x < n 7 < 8 true, enters for y = x y = 7; index_of_max = x y < n 7 < 8 true, enters for index_of_max = 7; array[inder_of_max] < array [y] 1 < 1 false, not in if y++ y = 8; y < n 8 < 8 false, not in for for(int x=0; x<n; x++){ int index_of_min = x; for(int y=x; y<n; y++){ if(array[index_of_min]<array[y]){ index_of_min = y; int temp = array[x]; array[x] = array[index_of_min]; array[index_of_min] = temp; So after 1 run, the for loop stops and we have: index_of_max = 7 and x = 7 temp = array[7] temp = 1 array[x] = array[index_of_max] array[7] = 1 array[index_of_max] = temp array[7] = 1 new array[ ] = {120, 99, 12, 10, 9, 4, 3, 1 Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 45
Selection Sort Code Analisys Array Elements: Start Order = 12, 9, 4, 99, 120, 1, 3, 10 array[ ] = {120, 99, 12, 10, 9, 4, 3, 1 public static void selection_srt(int array[], int n){ This for loop 8 runs 8 times tottaly in this case with the 8 size array[ ]. This for loop runs 36 times tottaly in this case with the 8 size array[ ]. Sorted array[ ] = {120, 99, 12, 10, 9, 4, 3, 1 It s sorted descending. If we wanted the array to be sorted ascending we should change the < comparison symbol to: > inside the if statement in the second for loop. for(int x=0; x<n; x++){ int index_of_min = x; for(int y=x; y<n; y++){ if(array[index_of_min]<array[y]){ index_of_min = y; int temp = array[x]; array[x] = array[index_of_min]; array[index_of_min] = temp; Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 46
Visual Paradigms Selection Sort Animation (with source code line by line visualization Selection Sort in Java Applets Centre Animated Sorting Algorithms: Selection Sort Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 47
Bibliography http://www.algolist.net http://www.roseindia.net Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 48
Ασκήσεις Άσκηση 1 1. Μετατρέψτε τον κώδικα του select ώστε να ταξινομεί χρησιμοποιώντας την μέγιστη τιμή αντί της ελάχιστης (βλέπε slide «Selection Sort visual paradigm 2 using the maximum value»). Άσκηση 2 1. Μετατρέψτε τον κώδικα ενός από τους αλγορίθμους (π.χ. Select) έτσι ώστε να έχετε μια main που να καλεί ανάλογα με την επιλογή σας από την οθόνη όλους τους αλγόριθμους. 2. Για το ίδιο array αριθμών (μεγέθους 500 στοιχείων) εκτελεστέ ταξινόμηση και με τους 4 αλγόριθμους. Συγκρίνετε τον αριθμό των επαναλήψεων που χρειάζεται κάθε αλγόριθμος για να ταξινομήσει το array. 3. Κάντε το ίδιο για ένα array: 1. με μικρό αριθμό elements (1000 elements) 2. με μέτριο αριθμό elements (5000 elements) 3. με μεγάλο αριθμό elements (10000 elements) Μάθημα: Δομές Δεδομένων & Αλγόριθμοι (Εργαστήριο) Καθηγητής: Δρ. Βιδάκης Νίκος Slide 49