array / array operations

36
1 Array / Array Operations Array / Array Operations

Upload: alaric

Post on 12-Jan-2016

80 views

Category:

Documents


0 download

DESCRIPTION

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. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Array / Array Operations

11

Array / Array OperationsArray / Array OperationsArray / Array OperationsArray / Array Operations

Page 2: Array / Array Operations

22

Single variables

Read the rainfall for the 12 month in a year:

Read n1

Read n2

…Read n12

n1

n2

n12

Page 3: Array / Array Operations

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

Page 4: Array / Array Operations

44

Attribute

Functions

Array / Struct / Class

Array Struct Class

Page 5: Array / Array Operations

55

class

Rainfall for the 12 month of a year:

class Rainfall {private:

data[12]public:

Rainfall(…)setRainfall(…)getRainfall(month)sum( )average( )getMax( )getMin( )…

}

Page 6: Array / Array Operations

66

Search Methods

- Linear search- Stepwise search- Binary search

Page 7: Array / Array Operations

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

Page 8: Array / Array Operations

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

Page 9: Array / Array Operations

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

Page 10: Array / Array Operations

1010

2

1N

Aavg

Linear searchAccess number

NAmax Maximum access number :

Average access number :

x

Page 11: Array / Array Operations

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

Page 12: Array / Array Operations

1212

Stepwise searchAccess number 1

xx

NAmax Maximum access number :

x

Page 13: Array / Array Operations

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

Page 14: Array / Array Operations

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

Page 15: Array / Array Operations

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

Page 16: Array / Array Operations

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

Page 17: Array / Array Operations

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

Page 18: Array / Array Operations

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

Page 19: Array / Array Operations

1919

Search methods [1/2]

Linear search

Stepwise search

Binary search1

2ln

ln

NAavg

1 NAavg

2

1N

Aavg

x

x

x

Page 20: Array / Array Operations

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

Page 21: Array / Array Operations

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.

Page 22: Array / Array Operations

2222

Sorting methods

- Bubblesort- Bucketsort- Mergesort- Shellsort- Quicksort- ...

Page 23: Array / Array Operations

2323

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

*

*

*

*

Page 24: Array / Array Operations

2424

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]

Page 25: Array / Array Operations

2525

3

5

2

7

9

3

5

2

7

9

3

5

2

7

9

3

2

5

7

9

*

Bubblesort - Example [3]

Page 26: Array / Array Operations

2626

3

2

5

7

9

3

2

5

7

9

2

3

5

7

9

2

3

5

7

9

*

Bubblesort - Example [4]

Page 27: Array / Array Operations

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

Page 28: Array / Array Operations

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

Page 29: Array / Array Operations

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

Page 30: Array / Array Operations

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

Page 31: Array / Array Operations

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

Page 32: Array / Array Operations

3232

Insert / Delete in an array

2

3

7

9

4

2

3

4

7

9

2

3

7

9

2

7

9

Insert

Delete

Page 33: Array / Array Operations

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

Page 34: Array / Array Operations

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

Page 35: Array / Array Operations

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)

Page 36: Array / Array Operations

3636

ENDENDENDEND