Array Array Operations 1 Single variables Read the
Array / Array Operations 1
Single variables Read the rainfall for the 12 month in a year: Read n 1 Read n 2 … Read n 12 n 1 n 2 n 12 2
Array Read the rainfall for the 12 month in a year: rainfall 1 FOR month : = 1 TO 12 read rainfall[month] ENDFOR 2 3 rainfall[2] FOR month : = january TO december read rainfall[month] ENDFOR 12 3
Array / Struct / Class Attribute Functions Array Struct Class 4
class Rainfall for the 12 month of a year: class Rainfall { private: data[12] public: Rainfall(…) set. Rainfall(…) get. Rainfall(month) sum( ) average( ) get. Max( ) get. Min( ) … } 5
Search Methods - Linear search - Stepwise search - Binary search 6
Linear search - Strategy Searched record No need for sorted array. Search from the beginning to the end of the array until the searched record is found or we find out the searched record does not exist. 7
Linear search Algorithm 1 Lin. Search /* /* /* (array, max, sid) Linear search for a given record in an array, returning the position of the searched record. If no record found, then returning 0. array : The array to search max : Maximum number of elements in the array sid : The value of the searched record nr : = 0 n : = 1 WHILE (nr = 0) AND (n<=max) DO IF sid = array[n] THEN nr : = n ELSE n : = n + 1 ENDIF ENDWHILE Return nr */ */ */ 8
Linear search Algorithm 2 Lin. Search (arary, max, sid) /* /* /* Linear search for a given record in an array returning the position of the searched record. If the record does not exist, then returning 0. array : The array to search max : The maximum number of elements in the arary sid : The value of the searched record n : = 1 WHILE (n <= max) AND (sid <> array[n]) DO n : = n + 1 ENDWHILE IF n <= max THEN nr : = n ELSE nr : = 0 Return nr */ */ */ 9
Linear search Access number Maximum access number : Average access number : 10
Stepwise search - Strategy Searched record The array must be sortered. Searching in step from the beginning until we have come sufficiently far. Searching in the last step. 11
Stepwise search Access number 1 Maximum access number : 12
Stepwise search Access number 2 Average access number : Optimal steplength : Average access number by optimal steplength : 13
Stepwise search Algorithm Step. Seach (array, max, sid) /* Stepwise search for given record in an array /* returning the postition of the searched record. /* If the record does not exist, then returning 0. /* arary : The array to search /* max : Maksimum number of elements in the array /* sid : The value of the searched record nr: =0 x: =Sqrt(max) first: =1 last: =max WHILE (first < last) AND (array[first] < sid) DO previous : = first : = min(first+x, last) ENDWHILE i : = first IF sid <> array[i] THEN i : = previous WHILE (i < first) AND (array[i] < sid) DO i : = i + 1 ENDWHILE ENDIF IF sid = array[i] THEN nr : = i ENDIF Return nr */ */ */ previous: =first // step // linear 14
Binary search - Strategy The array must be sorted. Divide into two equal parts og search further in actual half. Search until searched record is found or we have only one record left. 15
Binary search Access number Number of records in the array : N After halving 1 time : After halving 2 times : After halving 3 times : N/2 = N/21 records left N/4 = N/22 records left N/8 = N/23 records left After halving A times : N/2 A records left Maximum acccess number : Average access number : 16
Binary search Algorithm 1 Bin. Search (array, max, sid) /* Binary search for given record in an array /* returning the position of searched record. /* If the record does not exist, then returning 0. /* array : The array to search /* max : The maximum number of elements in the array /* sid : The value of the searched record */ */ */ nr: =0 first: =1 last: =max mid: =(first+last) DIV 2 WHILE (sid <> array[mid]) AND (first < last) DO IF sid < array[mid] THEN last : = mid - 1 ELSE first : = mid + 1 ENDIF mid : = (first + last) DIV 2 ENDWHILE IF sid = array[mid] THEN nr : = mid ENDIF Return nr 17
Binary search Algorithm 2 Bin. Search (tab, forst, sid) /*Binary search for a given record in an array /*returning the position of the searched record. /*If the record does not exist, then returning 0. /*Recursive search. /* array : The array to search /* first : Index of the first element /* last : Index of the last element /* sid : The value of the searched record mid: =(first+last) DIV 2 IF first > last THEN Return 0 ELSEIF sid = array[mid] THEN Return mid ELSEIF sid < array[mid] THEN Return Bin. Search(array, first, mid-1, sid) ELSE Return Bin. Search(array, mid+1, last, sid) ENDIF */ */ 18
Search methods [1/2] Linear search Stepwise search Binary search 19
Search methods [2/2] N = 2 millions Linear search Stepwise search Binary search 20
Sorting Often there is a need to have the data elements in a given arrangement or structure both because of quicker processing methods and for future applications. One kind of arrangement is sorting. 21
Sorting methods - Bubblesort Bucketsort Mergesort Shellsort Quicksort. . . 22
Bubblesort - Example [1] 9 9 * 3 3 9 5 5 5 9 7 7 9 2 3 3 5 5 5 7 7 2 2 * * 2 * 9 23
Bubblesort - Example [2] 3 3 3 5 5 5 7 7 2 2 2 9 9 * 7 9 24
Bubblesort - Example [3] 3 3 5 5 5 2 2 7 7 9 9 * 5 25
Bubblesort - Example [4] 3 3 * 2 2 3 3 2 2 5 5 7 7 9 9 26
Bubblesort - Algorithm 1 3 2 5 7 9 Bubble. Sort (array, n) /* /* /* Bubblesort of an array */ array : The array to be sorted */ n : Maximum number of elements in the array */ exchange : = true j : = 1 WHILE exchage DO exchange : = false FOR i: =1 TO n-j DO IF array[i] > array[i+1] THEN exchange : = true x : = array[i] : = array[i+1] : = x ENDIF ENDFOR j : = j + 1 ENDWHILE 27
array. Id Bubblesort - Algorithm 2 3 2 5 7 9 array. Dt Nilsen Olsen Hansen Knutsen Persen Bublle. Sort (array. Id, array. Dt, n) /* /* Bubblesort array. Id : array. Dt : n : of an array Array containing the sorting key The array(s) containing the rest of data Maximum number of elements in the array exchange : = true j : = 1 WHILE exchange DO exchange : = false FOR i: =1 TO n-j DO IF array. Id[i] > array. Id[i+1] THEN exchange : = true x: =array. Id[i]: = array. Id[i+1] y: =array. Dt[i]: = array. Dt[i+1] ENDIF ENDFOR j : = j + 1 ENDWHILE */ */ array. Id[i+1]: = x array. Dt[i+1]: = y 28
3 Nilsen 2 Olsen 5 Hansen 7 Knutsen 9 Persen Bubblesort - Algorithm 3 id dt Bubble. Sort (array, n) /* /* /* Bubblesort of an array */ array : The array to be sorted */ n : Maximum number of elements in the array */ exchange : = true j : = 1 WHILE exchange DO exchange : = false FOR i: =1 TO n-j DO IF array[i]. id > array[i+1]. id THEN exchange : = true x: =array[i]: = array[i+1] ENDIF ENDFOR j : = j + 1 ENDWHILE array[i+1]: = x 29
Order Array By sorting of big arrays (or many arryas), our bubblesort can imply moving of big datasets. We can make an improvement by means of an order array. The order array we read sequentially from the beginning and indicate in which order the array(s) should be accessed. Before sorting After sorting 30
Bubblesort - Order Array Bubble. Sort (array, order, n) /* /* Bubblesort of an aray with order array */ array : The array to be sorted */ order : Order array */ n : Maximum number of elements in the array */ exchange : = true i : = 1 WHILE exchange DO exchange : = false FOR i: =1 TO n-j DO IF array[order[i]] > array[order[i+1]] THEN x : = order[i] : = order[i+1] : = x ENDIF ENDFOR j : = j + 1 ENDWHILE 31
Insert / Delete in an array Insert 2 2 3 3 7 9 4 4 7 9 Delete 2 2 3 7 7 9 9 32
Insert into an array - Algorithms Insert (array, max, n, new. Record, pos, flag) /*Insert a new /* array /* max /* new. Record /* pos /* flag record in an array : The array : Maximum number of elements in the array : The first free position in the array : New record to insert into the array : Position to insert into the array : Return true if insert okay IF n > max THEN flag : = false ELSE flag : = true i : = n - 1 WHILE i >= pos DO tab[i+1] : = tab[i] i : = i - 1 ENDWHILE tab[pos] : = new. Record n : = n + 1 ENDIF */ */ 33
Delete in an array - Algorithm Delete (array, n, pos) /* Delete a record in an array */ /* array : The array */ /* n : The first free position in the array */ /* pos : The delete position in the array */ i : = pos WHILE i < n-1 DO array[i] : = array[i+1] i : = i + 1 ENDWHILE n : = n - 1 34
Fetch in an array - Algoritme Fetch (array, n, pos, record) /* Fetch and /* array /* n : /* pos : /* record : then delete a record from an array */ : The array */ The first free position in the array */ The delete position in the array */ Return the fetched record */ record : =array[pos] Delete(array, n, pos) 35
END 36
- Slides: 36