Download presentation
Presentation is loading. Please wait.
Published byEzra Jacobs Modified over 9 years ago
1
1 Array / Array Operations
2
2 Single variables Read the rainfall for the 12 month in a year: Read n 1 Read n 2 … Read n 12 n1n1 n2n2 n 12
3
3 Array Read the rainfall for the 12 month in a year: FOR month := 1 TO 12 read rainfall[month] ENDFOR rainfall rainfall[2] FOR month := january TO december read rainfall[month] ENDFOR 1 2 3 12
4
4 Attribute Functions Array / Struct / Class ArrayStructClass
5
5 class Rainfall for the 12 month of a year: class Rainfall { private: data[12] public: Rainfall(…) setRainfall(…) getRainfall(month) sum( ) average( ) getMax( ) getMin( ) … }
6
6 Search Methods - Linear search -Stepwise search -Binary search
7
7 Linear search - Strategy 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 the searched record does not exist. Searched record
8
8 Linear search Algorithm 1 LinSearch (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
9
9 Linear search Algorithm 2 LinSearch(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 array[n]) DO n:=n + 1 ENDWHILE IF n <= max THEN nr := n ELSE nr := 0 Return nr
10
10 Linear search Access number Maximum access number: Average access number:
11
11 Stepwise search - Strategy The array must be sortered. Searching in step from the beginning until we have come sufficiently far. Searching in the last step. Searched record
12
12 Stepwise search Access number 1 Maximum access number:
13
13 Stepwise search Access number 2 Average access number: Optimal steplength: Average access number by optimal steplength:
14
14 Stepwise search Algorithm StepSeach(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 previous:=first WHILE (first < last) AND (array[first] < sid) DO// step previous:= first first:= min(first+x,last) ENDWHILE i := first IF sid <> array[i] THEN i := previous WHILE (i < first) AND (array[i] < sid) DO// linear i := i + 1 ENDWHILE ENDIF IF sid = array[i] THEN nr := i ENDIF Return nr
15
15 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.
16
16 Binary search Access number Number of records in the array:N After halving 1 time :N/2 =N/2 1 records left After halving2 times :N/4 =N/2 2 records left After halving3 times :N/8 =N/2 3 records left After halvingA times :N/2 A records left Maximum acccess number: Average access number:
17
17 Binary search Algorithm 1 BinSearch (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
18
18 Binary search Algorithm 2 BinSearch (tab,forst,sist,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 BinSearch(array,first,mid-1,sid) ELSE Return BinSearch(array,mid+1,last,sid) ENDIF
19
19 Search methods [1/2] Linear search Stepwise search Binary search
20
20 Search methods [2/2] Linear search Stepwise search Binary search N = 2 millions
21
21 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.
22
22 Sorting methods - Bubblesort - Bucketsort - Mergesort -Shellsort - Quicksort -...
23
23 Bubblesort - Example [1] 9 3 5 7 2 9 3 5 7 2 3 9 5 7 2 3 5 9 7 2 3 5 7 9 2 3 5 7 2 9 * * * *
24
24 3 5 7 2 9 3 5 7 2 9 3 5 7 2 9 3 5 7 2 9 3 5 2 7 9 * Bubblesort - Example [2]
25
25 3 5 2 7 9 3 5 2 7 9 3 5 2 7 9 3 2 5 7 9 * Bubblesort - Example [3]
26
26 3 2 5 7 9 3 2 5 7 9 2 3 5 7 9 2 3 5 7 9 * Bubblesort - Example [4]
27
27 Bubblesort - Algorithm 1 3 2 5 7 9 BubbleSort (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]:=array[i+1] array[i+1]:=x ENDIF ENDFOR j := j + 1 ENDWHILE
28
28 Bubblesort - Algorithm 2 3 2 5 7 9 Nilsen Olsen Hansen Knutsen Persen arrayIdarrayDt BublleSort (arrayId,arrayDt,n) /*Bubblesort of an array*/ /*arrayId:Array containing the sorting key*/ /*arrayDt:The array(s) containing the rest of data*/ /*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 arrayId[i] > arrayId[i+1] THEN exchange := true x:=arrayId[i] arrayId[i]:=arrayId[i+1] arrayId[i+1]:= x y:=arrayDt[i] arrayDt[i]:=arrayDt[i+1] arrayDt[i+1]:= y ENDIF ENDFOR j := j + 1 ENDWHILE
29
29 Bubblesort - Algorithm 3 3 2 5 7 9 Nilsen Olsen Hansen Knutsen Persen iddt BubbleSort (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]:=array[i+1] array[i+1]:= x ENDIF ENDFOR j := j + 1 ENDWHILE
30
30 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 sortingAfter sorting
31
31 Bubblesort - Order Array BubbleSort (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]:=order[i+1] order[i+1]:=x ENDIF ENDFOR j := j + 1 ENDWHILE
32
32 Insert / Delete in an array 2 3 7 9 4 2 3 4 7 9 2 3 7 9 2 7 9 Insert Delete
33
33 Insert into an array - Algorithms Insert (array,max,n,newRecord,pos,flag) /*Insert a new record in an array*/ /*array:The array*/ /*max:Maximum number of elements in the array*/ /*n:The first free position in the array*/ /*newRecord:New record to insert into the array*/ /*pos:Position to insert into the array*/ /*flag: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]:= newRecord n := n + 1 ENDIF
34
34 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
35
35 Fetch in an array - Algoritme Fetch (array,n,pos,record) /*Fetch and then delete a record from an array*/ /*array:The array*/ /*n:The first free position in the array*/ /*pos:The delete position in the array*/ /*record:Return the fetched record*/ record :=array[pos] Delete(array,n,pos)
36
36 ENDEND
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.