28
Data is always stored in a logical way so that it can be accessed efficiently. Ex:A telephone directory The way data is stored is called the structure of the data, hence the term data structures One such data structure is an array. How data is stored?

How data is stored?

  • Upload
    major

  • View
    29

  • Download
    0

Embed Size (px)

DESCRIPTION

How data is stored?. Data is always stored in a logical way so that it can be accessed efficiently. Ex:A telephone directory The way data is stored is called the structure of the data, hence the term data structures One such data structure is an array. What is an Array. - PowerPoint PPT Presentation

Citation preview

Page 1: How data is stored?

Data is always stored in a logical way so that it can be accessed

efficiently.

Ex:A telephone directory

The way data is stored is called the structure of the data, hence the

term data structures

One such data structure is an array.

How data is stored?

Page 2: How data is stored?

What is an Array

• An array is a collection of fixed number of elements of the same type with a memory location allocated to each under a single variable name.

• Also called as Subscripted variables as they use subscripts to designate each element.

• Also called as tables as often data is available in tables under a single table name.

• Examples :roll no, marks ,age, name, matrix.• Array AGE is shown below• Each box has an index which in java and C++ starts with 0

32 45 56 32 12 67 21 34 77 65

0 1 2 3 4 5 6 7 8 9

Page 3: How data is stored?

Array contd….• Dimensioning the variable:

Specifying the number of memory locations to save for an array.

• Static array:The dimension of the array cannot change during

execution.Uses more space as it is assigned at the compile time

itself.• Dynamic array:

The dimension of the array is designated through a variable and can change during execution.

More flexible and use less memory space but more time consuming.

Page 4: How data is stored?

• Element:

Each array memory location is called an element.

It is given a number which is a reference relative to the location of the

first value of the array called the index / element number / subscript in

parentheses

• Base-zero system: The first array element is numbered zero and not one.

• Base-one system: The first array element is numbered one.

Page 5: How data is stored?

Base-zero and Base-one Arrays Base 0

ABase 1A

A(0) 0

A(1) 1

A(2) 2

A(3) 3

A(4) 4

A(5) 5

A(6) 6

A(N) N

.;

.

.

.

.

A(1) 1

A(2) 2

A(3) 3

A(4) 4

A(5) 5

A(6) 6

A(7) 7

A(N) N

.;

.

.

.

.

Page 6: How data is stored?

• Array Definition : type name[size];

• Array Definition Examples

– int A[10]; // array of 10 integers

– char str[80]; // array of 80 characters

– char name[30] // array of 30 characters

– const int MAX_ROWS = 100;

– int x[MAX_ROWS]; // array of MAX_ROWS integers

– double y[10*2]; // array of 20 doubles

Page 7: How data is stored?

ONE DIMENSIONAL ARRAY

• Consists of one column of

memory locations

Parallel arrays: Two or more

arrays with a related data in

each element poisition.

• Eg: consider two english and

maths mark arrays which are

related to a third array called

student name.

101

102

103

104

105

106

107

108

30

54

78

98

70

80

34

45

33

56

80

65

75

58

89

50

Sturollno Eng mark Math mark

Page 8: How data is stored?

Entering data into an array

• It is called loading an array.

• The number of elements in array is 10

• The counter of loop (R ) allows the computer to increase the element number by 1 each time a piece of data is entered into a memory location

• The computer can find a particular element in the array by using the reference number index represented by R

• R = loop counter • A(R ) = element R in the A

array

Read

R1 10

1

EnterA(R )

R

B

Algorithm

Loop : R = 1 to 10 Enter A( R)Loop end

Page 9: How data is stored?

Accumulating the elements of array• You may need to

sum the elements of an array

• Initialise the sum variable which contains the total to zero

• The instruction Sum = Sum + A(R ) tells the computer to add the valve of element A(R ) to the old valve of the sum (Sum) and to store the result into sum memory location (Sum)

R1 10

1

Sum = Sum + A(R )

R

B

CalcAlgorithmLoop : R = 1 to 10 sum = Sum + A( R)Loop end•The number of elements in array is 10•The counter of loop (R ) allows the computer to increase the element number by 1 each time a piece of data is entered into a memory location•Sum = Sum of the elements of AA(R ) = element R in the array A

Page 10: How data is stored?

Printing an array

• The number of elements in array is 10

• The counter of loop (R ) allows the computer to increase the element number by 1 each time a piece of data is printed.

• The computer can find a particular element in the array by using the reference number index represented by R

• R = loop counter • A(R ) = element R in the A

array

Read

R1 10

1

PrintA(R )

R

B

Algorithm

Loop : R = 1 to 10 Print A( R)Loop end

Page 11: How data is stored?

Column0 Column1 Column2 Column3

Row0

Row1

Row2

x[0][0] x[0][1] x[0][2] x[0][3]

x[1][0] x[1][1] x[1][2] x[1][3]

x[2][0] x[2][1] x[2][2] x[2][3]

Two-dimensional arraysA two dimensional array is a block of memory locations associated with a single memory variable and designated by row and columns numbers.

For e.g int x[3][4]

Page 12: How data is stored?

Loading a Two-Dimensional ArrayRead

R1 3

1

C

R

C 1 4

1

1 2 3 4

5 6 7 8

9 10 11 12

B

Enter A(R,C)

1

2

3

1 2 3 4R

C

R-RowC-Column

Page 13: How data is stored?

A

Print Column headings

R

1

1NR

Print Row headings

R

1

1NC

Print A(R,C)W/O CURSOR RETURN

C

RETURNCURSOR

R

B

PRINTING A TWO DIMENSIONAL ARRAY

R-ROW

NR-NUMBER OF ROWS

C=COLUMN

NC=NUMBER OF COLUMNS

Page 14: How data is stored?

Multi-Dimensional array

Multidimensional array-Arrays with a third or even a fourth

dimension.

• It can facilitate an understanding of the data ,improve the readability of

algorithms, and facilitate processing since the data can be processed

through the use of three or more nested loops.

•Ex: the parallel array marks for a student in two subjects can be

designated as a three dimensional array.

•R-row subscript

•C-column subscript

•V-volume subscript

•D-depth subscript

Page 15: How data is stored?

Table Look-up Technique

• A common application for

array is using a value to

look up another value in a

table.

• A one-dimensional array

would be used if the

element number can be

utilized as the given value

31

28

31

30

31

31

30

30

Algorithm

• Enter rollno

• Marks=mathmark[rollno]

• Print marks

• End

mathmark

1

2

3

4

5

6

7

8

9

Page 16: How data is stored?

Search Algorithms

• The process of finding an element in an array is called searching an array.

• Different types

Sequential Search

Binary Search

Sequential search (linear search):• If there’s no guarantee about the order of elements in the list i.e for

example, insertions have been under a user’s control.

• Search starts at the first element in the list and continues until either the item is found in the list - or entire list is searched

Page 17: How data is stored?

Algorithm and Flowchart for Sequential Search

Algorithm1. R = 12. Read SearchName3. While

SearchName<>Array(R) AND R<=N

R=R+1 While End

4. If R > N Then Print “Element

Not Found”Else

Print “element found at R position”

5. End

R = 1

WhileSearchName<>

Array(R ) and R<=N

R= R+1

If R > N

Print

Array(R )Print

Element Not Found

TF

F

T

B

Page 18: How data is stored?

Binary Search

• Sequential search is not efficient for large lists as it searches half the list, on average

• Another search algorithm – Binary search• Very fast• But can only be performed on ordered lists• This search algorithm uses the “Divide & Conquer” method to

search the list• First the search item is compared with the middle element of the

list. If the search item is less than the middle element of the list, we restrict the search to the first half of the list; otherwise we search the second half of the list

• most of the array is not searched at all , saving much time - thus BS algorithm is very fast.

Page 19: How data is stored?

Binary Search Vs Linear Search

Linear Search Binary Search

•Array need not be sorted.

•A sequential search of an array looks at the first item, the second item, and so on until it either finds a particular item or determines that the item does not occur in the group•More time consuming when used on large arrays

•A binary search of an array requires that the array be sorted .•It looks first at the middle of the array to determine in which half the desired item can occur. The search repeats this strategy on only this half of the array

•Very effective on large arrays by using divide and conquer policy.

Page 20: How data is stored?

ALGORITHM LB=Lower boundaryUB=Upper boundaryN=listR=current record positionS=search value1.LB=12.UB=N3.Flag=04. While Flag=0 R=Int((LB+UB)/2) If A(R)=S Then Flag=1 Else If S>A(R) Then LB=R+1 Else UB=R-1 If LB>UB Then Flag=2 While End

5. If Flag=2

Then

Print “record

Not found”

Else

Print record R

6.Exit

Page 21: How data is stored?

Flow Chart

Binary Search

LB=1

UB=N

Flag=0

WhileFlag=0

R=INT((LB+UB)/2)

IF A(R)=S

Flag=1

B

B A

C

IfS>A(R)

UB=R-1 LB=R+1

IfLB>UB

Flag=2

B

TF

F

T

C

F

T

F

Page 22: How data is stored?

A

IfFlag=2

Print record “R” Print “record not found”

TF

Exit

Page 23: How data is stored?

Sorting Techniques

Sorting is the process of putting data in alphabetical or numeric order using one key field or a concatenation of two or more fields.

Different types of Sorting Techniques:

Selection Exchange sort

Bubble sort

Shell sort

Page 24: How data is stored?

Selection Exchange SortAlgorithm

Loop: L=1 to N-1

Min=L

Loop :J=L+1 to N

If A(Min)>A(J)

Then

Min=J

Loop End: J

If L>Min

Then

R=A(L)

A(L)=A(Min)

A(Min)=R

Loop End: L

The theory is find the smallest value of the remaining values and switch with the value found in the position of the remaining elements.

Page 25: How data is stored?

CONTD…….ALGORITHM

Swap(*A,*B)

R=A

A=B

B=R

EXIT

Swap(*A,*B)

R=A

A=B

B=R

Exit

Page 26: How data is stored?

Bubble Sort

Flag=1E=N-1 While Flag=1 Flag=0 Loop: J=1 to E If A(J)>A(J+1) Then Swap A(J),A(J+1) Flag=1 Loop End: J E=E-1While EndExit

The theory of the bubble sort is to compare each element with the next element .If the element is larger than the next element , the elements are switched.

Page 27: How data is stored?

Bubble Sort

Flag=1

E=N-1

While Flag=1

Flag=0

1 E

A

J

1

A

IFA(J)>A(J+1)

J

E=E-1

Swap A(J),A(J+1)

Flag=1

T

F

Exit

F

Flowchart For Bubble Sort

Page 28: How data is stored?

G=Int(N/2) While G>0 M=N-G LoopJ:1 to M If A(J)>A(J+G) Then L=J Flag=True While Flag Swap A(L),A(L+G) L=L-G If L>=1 Then If A(L)<=A(L+G) Then Flag=False Else Continue Else Flag=False While End LoopEnd:J G=Int(G/2) While EndExit

SHELL SORT ALGORITHM

The theory of the Shell sort is to compare each element with another element in the data set using a gap to separate the elements.