Sie sind auf Seite 1von 27

Arrays, Records and Pointers

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.

Operations performed on linear structures:


Traversal and search, Insertion and deletion, Sorting and merging.
Prof. L. von Dobschtz Data Structures and Algorithms 2

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 n of elements is called the length or size of the array.

Prof. L. von Dobschtz

Data Structures and Algorithms

Linear Arrays (ctd.)


Choosing the name A for the array the elements of A are denoted
A1, A2, A3, , An by subscript notation A[1], A[2], A[3], , A[N] by parenthesis notation

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

Prof. L. von Dobschtz

Data Structures and Algorithms

Representation of linear Arrays in Memory


Let A be a linear array in the memory of the computer (a sequence of addressed locations) then LOC(A[K]) is the address of the element A[K] The computer needs only to keep track of the address of the first element of A[K], the base address of A: Base(A). Then the computer calculates the address of any element of A by: LOC(A[K]) = Base(A) + w(K- lower bound)+1 were w is the number of words per memory cell for the array.

Prof. L. von Dobschtz

Data Structures and Algorithms

Traversing linear Arrays


Let A be a collection of data elements stored in the computer. Suppose we want to
Print the contents of each element or Count the number of elements with a given property.

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

Inserting and Deleting


Let A be a collection of data elements stored in the computer. Inserting is adding another element to the collection of A and deleting is removing one of the elements from A. Inserting or deleting at the end of the array is easy. If we need to insert/delete an element in the middle of the array then half of the elements have to be moved down/up to new locations. The following algorithm a) Inserts a data element ITEM into the Kth position in a linear array
A, b) Deletes the Kth element from A and assigns it to a variable ITEM.

Prof. L. von Dobschtz

Data Structures and Algorithms

Inserting and Deleting (ctd.)


Algorithm Insert: INSERT(A,N,K,ITEM) Repeat for J=N to K: Set A[J+1]:=A[J]. Set A[K]:=ITEM. Set N:=N+1. Exit. Exit. Algorithm Delete: DELETE(A,N,K,ITEM) Set ITEM:=A[K]. Repeat for J=K to N-1: Set A[J]:=A[J+1]. Set N:=N-1.

Prof. L. von Dobschtz

Data Structures and Algorithms

Sorting: Bubble Sort


Let A be a collection of data elements stored in the computer. Sorting refers to the operation of rearranging the elements of A so they are in increasing order, i.e. A[1] < A[2] < A[3] < < A[N] Sorting is not trivial at all. Sorting efficiently may become quite complicated A simple sorting algorithm is known as the bubble sort. The time required to execute the bubble sort algorithm is proportional to n2, where n is the number of input items.

Prof. L. von Dobschtz

Data Structures and Algorithms

Bubble Sort (ctd.)


Algorithm Bubble Sort: BUBBLE(A,N) Repeat for K=1 to N-1: Set PTR:=1. Repeat while PTR<N-K: If A[PTR]>A[PTR+1] then: Interchange A[PTR] and A[PTR+1]. Set PTR:=PTR+1. Exit.
Prof. L. von Dobschtz Data Structures and Algorithms 10

(Pointer)

Searching: Linear Search


Let A be a collection of data elements stored in the computer. Suppose a specific ITEM of information is given. Searching refers to the operation of finding the location LOC of ITEM in A. The search is said to be successful if ITEM does appear in A and unsuccessful otherwise. There are many different searching algorithms, e.g.
Linear search, Binary search, Others.
Prof. L. von Dobschtz Data Structures and Algorithms 11

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

Linear Search (ctd.)


Algorithm linear search*: LINEAR(A,N,ITEM,LOC) Set A[N+1]:=ITEM. Set LOC:=1. Repeat while A[LOC]<>ITEM: Set LOC:=LOC+1. If LOC=N+1, then: Set LOC:=0. Exit.
*Assuming that ITEM occurs only once in A.
Prof. L. von Dobschtz Data Structures and Algorithms 13

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.

Prof. L. von Dobschtz

Data Structures and Algorithms

14

Binary Search (ctd.)


Algorithm binary search: BINARY(A,N,ITEM,LOC) Set BEG:=1, END:=N, MID:=INT((BEG+END)/2). Repeat while BEG<END and A[MID]<>ITEM: If ITEM<A[MID], then: Set END:=MID-1. Else: Set BEG:=MID+1. Set MID:=INT((BEG+END)/2). If A[MID]=ITEM, then: Set LOC:=MID. Else: Set LOC:=0. Exit.
Prof. L. von Dobschtz Data Structures and Algorithms 15

(Search is unsuccessful)

Complexity of Binary Search Algorithm


The complexity is measured by the number f(n) of comparisons to locate ITEM in A. Each comparison reduces the sample size in half. Hence in the worst case 2f(n)>n or f(n)=log2n. Suppose A contains 1 Mill. elements then 210=1024>1000 and 220>10002=1000000. Accordingly, it requires only about 20 comparisons to find the location of an item in an array with 1 Mill. elements.
Prof. L. von Dobschtz Data Structures and Algorithms 16

Limitations of Binary Search


The binary search algorithm requires two conditions:
The list must be sorted and One must have direct access to the middle element in any sublist.

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.

Prof. L. von Dobschtz

Data Structures and Algorithms

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

Representation of Two-Dimensional Arrays in Memory


Let A be a 2-dimensional mxn array. The array will be represented in memory by a block of mxn sequential memory locations. Specifically, the programming language will store A either
Column by column (column-major order) or Row by row (row-major order).

Prof. L. von Dobschtz

Data Structures and Algorithms

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)

Prof. L. von Dobschtz

Data Structures and Algorithms

20

General Multidimensional Arrays


General multidimensional arrays are defined analogously. An n-dimensional m1 x m2 x .... x mn array A is a collection of m1 m2 ..... mn data elements. Each element is specified by a list of n integers (subscripts K1, K2 , ....., Kn) with the property that 1<=Ki<=mi for all i. The element of A with the subscripts K1, K2 , ....., Kn will be denoted by A[K1, K2 , ....., KN].

Prof. L. von Dobschtz

Data Structures and Algorithms

21

Pointers and Pointer Arrays


Let A be an array. A variable P is called a pointer if P points to an element in A. An array PTR is called a pointer array if each element of PTR is a pointer. MEMBER Example: Membership list. Adolf
Dept. A Dept. B Dept. C Adolf Bernd Carl Anna Berta Conny Anton Berthold Billy Bruno
DEPT

1 4 9 11

Prof. L. von Dobschtz

Data Structures and Algorithms

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.

Prof. L. von Dobschtz

Data Structures and Algorithms

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.

The data items in a record form a hierarchical structure.


Prof. L. von Dobschtz Data Structures and Algorithms 24

Example: Employee Record


Employee

Name

Address

E No.

Last

First

ZIP

City

Street

Age

Salary

Prof. L. von Dobschtz

Data Structures and Algorithms

25

Representation of Records in Memory


File storage in parallel arrays
E# 001 002 003 004 005 006 NAME FIRST_NAME AGE SEX .. XXX

Brown Cohen Davis Evans Green

John Paul Mary Linda Mark

28 33 24 27 31

Male Male Female Female Male

Prof. L. von Dobschtz

Data Structures and Algorithms

26

Records with Variable Length


NAME FATHER NO PTR ..SIBLING

Adams Bailey Clark Donovan Evans

Richard Steven

3 0 2

4 0 1

David Lisa

Mark John

Jane William Donald

Prof. L. von Dobschtz

Data Structures and Algorithms

27

Das könnte Ihnen auch gefallen