Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision Next revision | Previous revision | ||
| aa:lab:07 [2020/11/12 12:37] roxana_elena.stiuca added 3rd and 4th parts | aa:lab:07 [2020/11/23 23:12] (current) claudiu.dorobantu | ||
|---|---|---|---|
| Line 8: | Line 8: | ||
| { | { | ||
| int i, j, temp; | int i, j, temp; | ||
| - | for(i = 0; i < n; i++) | + | for(i = 0; i < n - 1; i++) | 
| { | { | ||
| - | for(j = 0; j < n-i-1; j++) | + | for(j = 0; j < n - i - 1; j++) | 
| { | { | ||
| - | if( arr[j] > arr[j+1]) | + | if(arr[j] > arr[j + 1]) | 
| { | { | ||
| // swap the elements | // swap the elements | ||
| temp = arr[j]; | temp = arr[j]; | ||
| - | arr[j] = arr[j+1]; | + | arr[j] = arr[j + 1]; | 
| - | arr[j+1] = temp; | + | arr[j + 1] = temp; | 
| } | } | ||
| } | } | ||
| Line 23: | Line 23: | ||
| } | } | ||
| - | **1.2** Based on the following code for Insertion sort, analyze the algorithm's time complexity: | + | **1.2** Based on the following code for Selection sort, analyze the algorithm's time complexity: | 
| - | void insertionSort(int arr[], int n)  | + | void selectionSort(int arr[], int n)  | 
| {  | {  | ||
| - | int i, key, j; | + | int i, j, min_idx, temp; | 
| - | for (i = 1; i < n; i++) | + | for (i = 0; i < n - 1; i++) | 
| {  | {  | ||
| - | key = arr[i]; | + | min_idx = i; | 
| - | j = i - 1; | + | for (j = i + 1; j < n; j++) | 
| - | + | if (arr[j] < arr[min_idx]) | |
| - | /* Move elements of arr[0..i-1], that are  | + | min_idx = j; | 
| - | greater than key, to one position ahead  | + | |
| - | of their current position */ | + | // swap the min element with the first element | 
| - | while (j >= 0 && arr[j] > key) | + | temp = arr[min_idx]; | 
| - | { | + | arr[min_idx] = arr[i]; | 
| - | arr[j + 1] = arr[j]; | + | arr[i] = temp; | 
| - | j = j - 1; | + | |
| - | }  | + | |
| - | arr[j + 1] = key; | + | |
| }  | }  | ||
| } | } | ||
| Line 48: | Line 45: | ||
| Consider the following search algorithm of a value v in a sorted array arr:  | Consider the following search algorithm of a value v in a sorted array arr:  | ||
| - | int search (int arr[], int lo, int hi, int v){ | + | int search(int arr[], int lo, int hi, int v) | 
| - | int mid = (lo+hi)/2;  | + | { | 
| + | int mid = (lo + hi) / 2;  | ||
| if (arr[mid] == v) | if (arr[mid] == v) | ||
| return mid; | return mid; | ||
| Line 59: | Line 57: | ||
| return search(arr, mid, hi, v); | return search(arr, mid, hi, v); | ||
| } | } | ||
| - |  | + | |
| **2.1** Determine the recurrence for the search algorithm above. | **2.1** Determine the recurrence for the search algorithm above. | ||
| Line 71: | Line 69: | ||
| Consider the following modification to 'search' (which may not make sense in practice): | Consider the following modification to 'search' (which may not make sense in practice): | ||
| - |  | + | |
| - | int search (int arr[], int lo, int hi, int v){ | + | int search(int arr[], int lo, int hi, int v) | 
| + | { | ||
| if (!is_sorted(arr, lo, hi)) | if (!is_sorted(arr, lo, hi)) | ||
| return -1; | return -1; | ||
| - | //rest just the same | + | int mid = (lo + hi) / 2; | 
| + | if (arr[mid] == v) | ||
| + | return mid; | ||
| + | if (lo == hi) | ||
| + | return -1; | ||
| + | if (arr[mid] > v) | ||
| + | return search(arr, lo, mid, v); | ||
| + | else | ||
| + | return search(arr, mid, hi, v); | ||
| } | } | ||
| - |  | + | |
| **3.1** In what time can we check if an array is sorted? | **3.1** In what time can we check if an array is sorted? | ||
| Line 86: | Line 93: | ||
| ==== 4. Atypical situations when solving recurrences ==== | ==== 4. Atypical situations when solving recurrences ==== | ||
| - | **4.1** Substitution method does not work. Solve T(n) = 2T(n/2)+1. | + | **4.1** Substitution method does not work. Solve $math[T(n) = 2 T(] $math[n \over 2] $math[) + 1]. | 
| - | **4.2** Un-balanced trees. Solve T(n) = T(n/4) + T(3n/4) + n via the tree and substitution methods. | + | **4.2** Un-balanced trees. Solve via the tree and substitution methods: $math[T(n) = T(] $math[n \over 4] $math[) + T(] $math[3n \over 4] $math[) + n]. | 
| - | **4.3** Solve T(n) = 2T(n/2) + n^2. | + | **4.3** Solve $math[T(n) = 2 T(] $math[n \over 2] $math[) + n ^ 2]. | 
| **4.4** Look up the Hanoi towers problem. Determine the number of steps necessary to solve it. | **4.4** Look up the Hanoi towers problem. Determine the number of steps necessary to solve it. | ||
| **4.5** Consider the following algorithm: | **4.5** Consider the following algorithm: | ||
| - | + | ||
| - | int fibo(int n){ | + | int fibo(int n) | 
| + | { | ||
| if (n <= 1) | if (n <= 1) | ||
| return n; | return n; | ||
| - | return fibo(n-1) + fibo(n-2); | + | return fibo(n - 1) + fibo(n - 2); | 
| } | } | ||
| - | |||
| - | Determine its complexity with regard to the *size* of the input. | ||
| - | **4.6** Solve T(n) = sqrt(n) * T(sqrt(n)) + n. | + | Determine its complexity with regard to the size of the input. | 
| + | |||
| + | **4.6** Solve $math[T(n) = \sqrt n * T(\sqrt n) + n]. | ||