Beruflich Dokumente
Kultur Dokumente
CONTENTS
- Linear arrays - Traversal - Insertion and deletion - Sort: Bubble sort - Linear search and binary search - Multidimensional arrays - Pointers - Records
Intro
A data structure is linear if it elements form a sequence or linear list. Two basic ways of representing linear structures in memory:
Arrays: sequential memory locations, Linked lists: using pointers or links.
Linear Arrays
Arrays are usually easy to traverse, search and sort and used to store relatively permanent collections of data. A linear array is a list of a finite number n of similar data elements
The elements are referenced by a index set of n consecutive numbers 1,2,3,,n. The elements are stored in successive memory locations.
The number K in A[K] is called a subscript or index and A[K] is a subscripted variable. Example: Let A be a 6-element array of integers.
247 1 56 2 429 3 135 4 87 5 156 6
This can be accomplished by traversing A, e.g. by accessing and processing (visiting) each element exactly once. Algorithm: For K=1 To N: Apply PROCESS to A[K]. Exit.
Prof. L. von Dobschtz Data Structures and Algorithms 6
(Pointer)
Linear Search
Suppose A is a linear array with n elements. The most intuitive way to search for a given ITEM in A is to compare ITEM with each element of A one by one. This method, which traverses A sequentially to locate ITEM, is called linear or sequential search. To simplify, ITEM is first assigned to A[N+1]. This way the search must eventually succeed. The complexity is (average case): f(n)=(1+2+3+...+n)/n=(n+1)/2 (worst case: f(n)=n+1.)
Prof. L. von Dobschtz Data Structures and Algorithms 12
Binary Search
Suppose A is an array which is sorted in increasing numerical order or, equivalently, alphabetically. Then there is an extremely efficient searching algorithm, called binary search. The algorithm applied to A works as follows: During each stage of the algorithm, the search for ITEM is reduced to a segment of elements of A. The variables BEG, END and MID denote, respectively, the beginning, end and middle locations of a segment in A.
14
(Search is unsuccessful)
This means that one must essentially use a sorted array to hold the data. Keeping data in a sorted array is very expensive when there are many insertions and deletions. In such situations a different data structure may be used.
17
Two-Dimensional Arrays
Linear arrays are also called one-dimensional arrays, since each element is referenced by a single subscript. Most programming languages allow two- to three-dimensional (or even more) arrays, i.e. where elements are referenced by two or more subscripts. A 2-dimensional mxn array A is a collection of mxn data elements such that
Each element is specified by a pair of integers J, K called subscripts, with the property 1<J<m and 1<K<n. The elements of A with the subscripts J and K will be denoted by AJ,K or A[J,K].
Two-dimensional arrays are called Matrices in mathematics and tables in business applications.
Prof. L. von Dobschtz Data Structures and Algorithms 18
19
Example
Column-major order:
A Subscript
247 (1,1) 135 (2,1) 25 (3,1) 129 (4,1) 56 (1,2) 87 (2,2)
Row-major order:
A Subscript
247 (1,1) 56 (1,2) 429 (1,3) 135 (2,1) 87 (2,2) 156 (2,3)
20
21
1 4 9 11
Anna Anton Bernd Berta Berthold Billy Bruno Carl Conny $$$
22
Pointer Arrays
Suppose one wants to print only the names in the Lth department, where the value of L is part of the input: Set FIRST:=DEPT[L] and LAST:=DEPT[L+1]-1. Repeat for K=First to LAST: Write: Member[K]. Exit.
Note: The variables FIRST and LAST are used mainly for notational convenience.
23
Records
Collections of data are frequently organized into a hierarchy of fields, records and files. A record is a collection of related data items called a field or attribute and a file is a collection of similar records. Each data item may be a group item composed of subitems or an elementary item (indecomposable). A record differs from a linear array:
It may have different data types. The data items are indexed by attribute names.
Name
Address
E No.
Last
First
ZIP
City
Street
Age
Salary
25
28 33 24 27 31
26
Richard Steven
3 0 2
4 0 1
David Lisa
Mark John
27