290 likes | 446 Views
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.
E N D
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 • 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 0 1 2 3 4 5 6 7 8 9
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.
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.
Base-zero and Base-one Arrays Base 0 A Base 1 A A(0) 0 A(1) 1 A(1) 1 A(2) 2 A(2) 2 A(3) 3 A(3) 3 A(4) 4 A(4) 4 A(5) 5 A(5) 5 A(6) 6 A(6) 6 A(7) 7 . . . . . . .; . .; . A(N) N A(N) N
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
ONE DIMENSIONAL ARRAY Engmark Sturollno Math mark • 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.
Entering data into an array Read • 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 Algorithm Loop : R = 1 to 10 Enter A( R) Loop end • R • 10 • 1 Enter A(R ) R B
Accumulating the elements of array 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 • 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 • 10 • 1 Sum = Sum + A(R ) R B
Printing an array Read • 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 Algorithm Loop : R = 1 to 10 Print A( R) Loop end • R • 10 • 1 Print A(R ) R B
Column3 Column2 Column0 Column1 Row0 Row1 Row2 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]
Loading a Two-Dimensional Array Read C 1 2 3 4 R • R • 3 • 1 1 2 C 1 4 1 3 Enter A(R,C) R-Row C-Column C R B
A Print Column headings R 1 PRINTING A TWO DIMENSIONAL ARRAY 1 NR Print Row headings R 1 1 NC R-ROW NR-NUMBER OF ROWS C=COLUMN NC=NUMBER OF COLUMNS Print A(R,C) W/O CURSOR RETURN C RETURN CURSOR R B
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
Table Look-up Technique mathmark • 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 1 2 3 4 5 6 7 8 9 • Algorithm • Enter rollno • Marks=mathmark[rollno] • Print marks • End
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
Algorithm R = 1 Read SearchName 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 Algorithm and Flowchart for Sequential Search R = 1 While SearchName<> Array(R ) and R<=N F T R= R+1 If R > N F T Print Element Not Found Print Array(R ) B
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.
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 ALGORITHM 5. If Flag=2 Then Print “record Not found” Else Print record R 6.Exit
Flow Chart Binary Search C LB=1 UB=N If S>A(R) Flag=0 F T While Flag=0 F B A UB=R-1 LB=R+1 R=INT((LB+UB)/2) If LB>UB F IF A(R)=S F C T T Flag=2 Flag=1 B B
A If Flag=2 F T Print “record not found” Print record “R” Exit
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
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.
CONTD……. Swap(*A,*B) ALGORITHM Swap(*A,*B) R=A A=B B=R EXIT R=A A=B B=R Exit
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. Bubble Sort
Bubble Sort A Flag=1 IF A(J)>A(J+1) T Swap A(J),A(J+1) E=N-1 Flag=1 F While Flag=1 F J Exit Flag=0 E=E-1 1 E J 1 FlowchartForBubbleSort A
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. 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