0% found this document useful (0 votes)
85 views2 pages

05 Vectors PDF

This document discusses vectors and their implementation using arrays. It describes the vector abstract data type which allows elements to be accessed, inserted, or removed by rank. It then discusses how arrays can be used to implement vectors, including the operations to insert and remove elements and their time complexities of O(n) in the worst cases. It concludes by summarizing the space and time performance of operations on an array-based vector implementation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views2 pages

05 Vectors PDF

This document discusses vectors and their implementation using arrays. It describes the vector abstract data type which allows elements to be accessed, inserted, or removed by rank. It then discusses how arrays can be used to implement vectors, including the operations to insert and remove elements and their time complexities of O(n) in the worst cases. It concludes by summarizing the space and time performance of operations on an array-based vector implementation.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Vectors 4/1/2003 9:03 AM

Outline and Reading


The Vector ADT (§5.1.1)
Vectors Array-based implementation (§5.1.2)

4/1/2003 9:03 AM Vectors 1 4/1/2003 9:03 AM Vectors 2

The Vector ADT Applications of Vectors


The Vector ADT Main vector operations:
extends the notion of „ elemAtRank(int r): returns the

array by storing a element at rank r without


Direct applications
sequence of arbitrary removing it „ Sorted collection of objects (elementary
objects „ replaceAtRank(int r, Object o):
database)
An element can be replace the element at rank r with
accessed, inserted or o Indirect applications
„ insertAtRank(int r, Object o):
removed by specifying
its rank (number of insert a new element o to have „ Auxiliary data structure for algorithms
rank r
elements preceding it) „ Component of other data structures
„ removeAtRank(int r): removes the
An exception is element at rank r
thrown if an incorrect
Additional operations size() and
rank is specified (e.g.,
isEmpty()
a negative rank)
4/1/2003 9:03 AM Vectors 3 4/1/2003 9:03 AM Vectors 4

Array-based Vector Insertion


Use an array V of size N In operation insertAtRank(r, o), we need to make
A variable n keeps track of the size of the vector room for the new element by shifting forward the
(number of elements stored) n − r elements V[r], …, V[n − 1]
Operation elemAtRank(r) is implemented in O(1) In the worst case (r = 0), this takes O(n) time
time by returning V[r]
V
0 1 2 r n
V V
0 1 2 r n 0 1 2 r n
V o
0 1 2 r n
4/1/2003 9:03 AM Vectors 5 4/1/2003 9:03 AM Vectors 6

1
Vectors 4/1/2003 9:03 AM

Deletion Performance
In operation removeAtRank(r), we need to fill the In the array based implementation of a Vector
hole left by the removed element by shifting „ The space used by the data structure is O(n)
backward the n − r − 1 elements V[r + 1], …, V[n − 1]
„ size, isEmpty, elemAtRank and replaceAtRank run in
In the worst case (r = 0), this takes O(n) time O(1) time
„ insertAtRank and removeAtRank run in O(n) time
V o If we use the array in a circular fashion,
0 1 2 r n
insertAtRank(0) and removeAtRank(0) run in
V O(1) time
0 1 2 r n In an insertAtRank operation, when the array
V is full, instead of throwing an exception, we
0 1 2 r n can replace the array with a larger one
4/1/2003 9:03 AM Vectors 7 4/1/2003 9:03 AM Vectors 8

You might also like