Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: " 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."— Presentation transcript:

1  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?

2 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 32455632126721347765 0 1 2 3 4 5 6 7 8 9

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

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

5 Base-zero and Base-one Arrays Base 0 A Base 1 A 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.;..;.......

6 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

7 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

8 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 R 110 1 Enter A(R ) R B Algorithm Loop : R = 1 to 10 Enter A( R) Loop end

9 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) R 110 1 Sum = Sum + A(R ) R B Calc Algorithm Loop : 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 A  A(R ) = element R in the array A

10 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 R 110 1 Print A(R ) R B Algorithm Loop : R = 1 to 10 Print A( R) Loop end

11 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 arrays A 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]

12 Loading a Two-Dimensional Array Read R 1 3 1 C R C 1 4 1 1234 5678 9101112 B Enter A(R,C ) 1 2 3 1234 R C R-Row C-Column

13 A Print Column headings R1R1 1 NR Print Row headings R1R1 1 NC Print A(R,C) W/O CURSOR RETURN C RETURN CURSOR R B PRINTING A TWO DIMENSIONAL ARRAY R-ROW NR-NUMBER OF ROWS C=COLUMN NC=NUMBER OF COLUMNS

14 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

15 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 30 Algorithm Enter rollno Marks=mathmark[roll no] Print marks End mathmark 123456789123456789

16 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

17 Algorithm and Flowchart for Sequential Search Algorithm 1.R = 1 2.Read SearchName 3.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 While SearchName<> Array(R ) and R<=N R= R+1 If R > N Print Array(R ) Print Element Not Found T F F T B

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

19 Binary Search Vs Linear Search Linear SearchBinary 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.

20 ALGORITHM LB=Lower boundary UB=Upper boundary N=list R=current record position S=search value 1.LB=1 2.UB=N 3.Flag=0 4. 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

21 Flow Chart Binary Search LB=1 UB=N Flag=0 While Flag=0 R=INT((LB+UB)/2) IF A(R)=S Flag=1 B B A C If S>A(R) UB=R-1LB=R+1 If LB>UB Flag=2 B T F F T C F T F

22 A If Flag=2 Print record “R” Print “record not found” T F Exit

23 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

24 Selection Exchange Sort Algorithm 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.

25 CONTD……. ALGORITHM Swap(*A,*B) R=A A=B B=R EXIT Swap(*A,*B) R=A A=B B=R Exit

26 Bubble Sort Flag=1 E=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-1 While End Exit 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.

27 Bubble Sort Flag=1 E=N-1 While Flag=1 Flag=0 1 E A J 1 A IF A(J)>A(J+1) J E=E-1 Swap A(J),A(J+1) Flag=1 T F Exit F Flowchart For Bubble Sort

28 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 End Exit 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.


Download ppt " 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."

Similar presentations


Ads by Google