90 likes | 109 Views
Vectors, Lists, Sequences. Vectors. Linear sequence s of n elements e rank – number of elements before e in s Vector supports access to elements via their ranks elemAtRank(r) replaceAtRank(r, e) insertAtRank(r, e) removeAtRank(r) size isEmpty. Vector Implementation.
E N D
Vectors • Linear sequence s of n elements e • rank – number of elements before e in s • Vector supports access to elements via their ranks • elemAtRank(r) • replaceAtRank(r, e) • insertAtRank(r, e) • removeAtRank(r) • size • isEmpty
Vector Implementation • Extendable array implementation • when array is full, allocate new array of 2xs the size • copy elements into new array • Efficiency?
Lists • Access/modify by “position” instead of rank • insertBefore(p, e) • insertAfter(p, e)
Sequences • Supports functionality of vector and list • Additional functions • atRank(r) – returns position • rankOf(p) – returns rank
Bubble Sort • Bubble sort • make n passes through a sequence and swap two elements if they are out of order (5 7 2 6 9 3)
Bubble Sort on a Sequence • Algorithm
Iterators • Step through a data structure one element at a time • hasNext() • Input: none Output: Boolean • next() • Input: none Output: Object
Exercises • Give an adapter class to support the Stack interface using the functions of the vector ADT