1 / 36

Array / Array Operations

Array / Array Operations. 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. Array. Read the rainfall for the 12 month in a year:. rainfall. 1. 2. rainfall[2]. FOR month := 1 TO 12 read rainfall[month] ENDFOR. 3.

mehaffey
Download Presentation

Array / Array Operations

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Array / Array Operations

  2. Single variables Read the rainfall for the 12 month in a year: Read n1 Read n2 … Read n12 n1 n2 n12

  3. Array Read the rainfall for the 12 month in a year: rainfall 1 2 rainfall[2] FOR month := 1 TO 12 read rainfall[month] ENDFOR 3 FOR month := january TO december read rainfall[month] ENDFOR 12

  4. Array / Struct / Class Attribute Functions Array Struct Class

  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. Search Methods - Linear search - Stepwise search - Binary search

  7. 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 the searched record does not exist.

  8. Linear searchAlgorithm 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. Linear searchAlgorithm 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 <= max) AND (sid <> array[n]) DO n := n + 1 ENDWHILE IF n <= max THEN nr := n ELSE nr := 0 Return nr

  10. Linear searchAccess number Maximum access number : Average access number :

  11. 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.

  12. Stepwise searchAccess number 1 Maximum access number :

  13. Stepwise searchAccess number 2 Average access number : Optimal steplength : Average access number by optimal steplength :

  14. Stepwise searchAlgorithm 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. 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. Binary searchAccess number Number of records in the array : N After halving 1 time : N/2 = N/21 records left After halving 2 times : N/4 = N/22 records left After halving 3 times : N/8 = N/23 records left After halving A times : N/2A records left Maximum acccess number : Average access number :

  17. Binary searchAlgorithm 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. Binary searchAlgorithm 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. Search methods [1/2] Linear search Stepwise search Binary search

  20. Search methods [2/2] N = 2 millions Linear search Stepwise search Binary search

  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. Sorting methods • - Bubblesort • Bucketsort • Mergesort • - Shellsort • - Quicksort • - ...

  23. Bubblesort - Example [1] 9 9 3 3 3 3 * 3 3 9 5 5 5 * 5 5 5 9 7 7 * 7 7 7 7 9 2 * 2 2 2 2 2 9

  24. Bubblesort - Example [2] 3 3 3 3 3 5 5 5 5 5 7 7 7 7 2 * 2 2 2 2 7 9 9 9 9 9

  25. Bubblesort - Example [3] 3 3 3 3 5 5 5 2 * 2 2 2 5 7 7 7 7 9 9 9 9

  26. Bubblesort - Example [4] 3 3 2 2 * 2 2 3 3 5 5 5 5 7 7 7 7 9 9 9 9

  27. 3 Bubblesort - Algorithm 1 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. arrayId arrayDt Nilsen 3 Bubblesort - Algorithm 2 Olsen 2 Hansen 5 Knutsen 7 Persen 9 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. 3 Nilsen Bubblesort - Algorithm 3 2 Olsen 5 Hansen 7 Knutsen 9 Persen id dt 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. 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

  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. Insert / Delete in an array 2 2 3 3 Insert 4 7 4 9 7 9 2 2 3 7 Delete 7 9 9

  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. 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. 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. END

More Related