1 array / array operations. 2 single variables read the rainfall for the 12 month in a year: read n...
TRANSCRIPT
22
Single variables
Read the rainfall for the 12 month in a year:
Read n1
Read n2
…Read n12
n1
n2
n12
33
Array
Read the rainfall for the 12 month in a year:
FOR month := 1 TO 12read rainfall[month]
ENDFOR
rainfall
rainfall[2]
FOR month := january TO decemberread rainfall[month]
ENDFOR
1
2
3
12
55
class
Rainfall for the 12 month of a year:
class Rainfall {private:
data[12]public:
Rainfall(…)setRainfall(…)getRainfall(month)sum( )average( )getMax( )getMin( )…
}
77
Linear search - Strategy
No need for sorted array.
Search from the beginning to the end of the arrayuntil the searched record is foundor we find out the the searched record does not exist.
Searched record
x
88
Linear searchAlgorithm 1
x
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 := 0n := 1WHILE (nr = 0) AND (n<=max) DOIF sid = array[n] THENnr := n
ELSEn := n + 1
ENDIFENDWHILEReturn nr
99
Linear searchAlgorithm 2
x
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 := 1WHILE (n <= max) AND (sid <> array[n]) DOn := n + 1
ENDWHILEIF n <= max THENnr := n
ELSEnr := 0
Return nr
1111
Stepwise search - Strategy
The array must be sortered.
Searching in step from the beginninguntil we have come sufficiently far.Searching in the last step.
Searched record
x
1313
Stepwise searchAccess number 2
1222
1
2
1
x
x
NxxN
Aavg
Nxx
NA
x
NA
x
N
dx
dAA
avg
gjgj
avg
02
1
20
0 2
1
2
2'
3''
2'
Nxopt
1122
Nx
x
NAavg
Averageaccess number :
Optimal steplength :
Averageaccess numberby optimal steplength :
x
1414
Stepwise searchAlgorithm
x
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:=firstWHILE (first < last) AND (array[first] < sid) DO // step
previous := first first := min(first+x,last)ENDWHILEi := firstIF sid <> array[i] THEN i := previous
WHILE (i < first) AND (array[i] < sid) DO // lineari := i + 1
ENDWHILEENDIFIF sid = array[i] THEN nr := iENDIFReturn nr
1515
Binary search - Strategy
The array must be sorted.
Divide into two equal partsog search further in actual half.Search until searched record is foundor we have only one record left.
x
1616
Binary searchAccess number
Number of records in the array : N
After halving 1 time : N/2 = N/21 records leftAfter halving 2 times : N/4 = N/22 records leftAfter halving 3 times : N/8 = N/23 records left
After halving A times : N/2A records left
2ln
lnlog
12
2
NNA
N
max
Amax
12ln
ln1log1 2max
NNAAavg
Maximum acccess number :
Average access number :
x
1717
Binary searchAlgorithm 1 x
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 2WHILE (sid <> array[mid]) AND (first < last) DO
IF sid < array[mid] THEN last := mid - 1
ELSEfirst := mid + 1
ENDIFmid := (first + last) DIV 2
ENDWHILEIF sid = array[mid] THEN nr := midENDIFReturn nr
1818
Binary searchAlgorithm 2 x
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 2IF first > last THEN
Return 0ELSEIF sid = array[mid] THEN
Return midELSEIF sid < array[mid] THEN
Return BinSearch(array,first,mid-1,sid)ELSE
Return BinSearch(array,mid+1,last,sid)ENDIF
1919
Search methods [1/2]
Linear search
Stepwise search
Binary search1
2ln
ln
NAavg
1 NAavg
2
1N
Aavg
x
x
x
2020
Search methods [2/2]
Linear search
Stepwise search
Binary searchx
x
x
12ln
ln
NAavg
1 NAavg
2
1N
Aavg 1000000avgA
1415avgA
20avgA
N = 2 millions
2121
Sorting
Often there is a need to have the data elementsin a given arrangement or structure both because of quicker processing methodsand for future applications.
One kind of arrangement is sorting.
2727
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 := truej := 1WHILE exchage DOexchange := falseFOR i:=1 TO n-j DOIF array[i] > array[i+1] THENexchange := truex := array[i]array[i] := array[i+1]array[i+1] := x
ENDIFENDFORj := j + 1
ENDWHILE
2828
Bubblesort - Algorithm 2
3
2
5
7
9
Nilsen
Olsen
Hansen
Knutsen
Persen
arrayId arrayDt
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 := truej := 1WHILE exchange DOexchange := falseFOR i:=1 TO n-j DOIF arrayId[i] > arrayId[i+1] THENexchange := truex:=arrayId[i] arrayId[i]:= arrayId[i+1] arrayId[i+1]:= xy:=arrayDt[i] arrayDt[i]:= arrayDt[i+1] arrayDt[i+1]:= y
ENDIFENDFORj := j + 1
ENDWHILE
2929
Bubblesort - Algorithm 3
3
2
5
7
9
Nilsen
Olsen
Hansen
Knutsen
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 := truej := 1WHILE exchange DOexchange := falseFOR i:=1 TO n-j DOIF array[i].id > array[i+1].id THENexchange := truex:=array[i] array[i]:=array[i+1] array[i+1]:= x
ENDIFENDFORj := j + 1
ENDWHILE
3030
Order Array
By sorting of big arrays (or many arryas), our bubblesort canimply moving of big datasets.We can make an improvement by means of an order array.The order array we read sequentially from the beginningand indicate in which order the array(s) should be accessed.
Before sorting After sorting
NavnOrden
Alfsen
Persen
Hansen
Nilsen
4
3
2
1
NavnOrden
Alfsen
Persen
Hansen
Nilsen
3
1
2
4
3131
Bubblesort - Order Array
NavnOrden
Alfsen
Persen
Hansen
Nilsen
4
3
2
1
NavnOrden
Alfsen
Persen
Hansen
Nilsen
3
1
2
4
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 := truei := 1WHILE exchange DOexchange := falseFOR i:=1 TO n-j DOIF array[order[i]] > array[order[i+1]] THENx := order[i]order[i] := order[i+1]order[i+1] := x
ENDIFENDFORj := j + 1
ENDWHILE
3333
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 THENflag := false
ELSEflag:= truei := n - 1WHILE i >= pos DO
tab[i+1] := tab[i]i := i - 1
ENDWHILEtab[pos] := newRecordn := n + 1
ENDIF
3434
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 := posWHILE i < n-1 DO array[i] := array[i+1]i := i + 1
ENDWHILEn := n - 1
3535
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)