• No results found

Data Structures and Algorithms

N/A
N/A
Protected

Academic year: 2021

Share "Data Structures and Algorithms"

Copied!
16
0
0

Loading.... (view fulltext now)

Full text

(1)

Data Structures and Algorithms

P. Healy CS1-08 Computer Science Bldg. tel: 202727 [email protected] Spring 2020–2021

(2)

Outline

1 Announcements

2 Abstract Data Types (ADTs) Linked Lists

(3)

Announcements

CS4115 support sessions: check email Week06 tute content changed

(4)

Introduction

An ADT is a data entity with operations associated with that entity

The ADT does not specify how the operations are implemented (nor their running times)

Some of the ADTs we will encounter: Lists

Trees Graphs

Implementation of an operation may change but this must not affect a user of the ADT

(5)

Outline

1 Announcements

2 Abstract Data Types (ADTs) Linked Lists

(6)

Main Points

In records may be inserted and

deleted with great frequency using

Storing records in a gives poor performance:

3 while an element can befound using binary search in O(log n)-time

7 toinsert or delete an element, time taken is on average logn +n2 =O(n) if the array is sorted

(7)

Main Points (contd.)

To store data items on a linked list we store each item in a cell, and

each cell contains a pointer to the next cell

b f k

pointer to list

using an unsorted list ADT we can achieve O(1)-time for insertions at the expense of O(n)-time for

however, for sorted lists, insertion- or deletion- or search-time is on avg. n2 =O(n)

b f k

h head of list

(8)

Some Issues with Lists

How do we handle:

insertions before the first element deletion of first element

Weiss’ linked list implementation:

Header (declaration) file: LinkedList.h

(Note ListItr class that akwardly implements iterators) Source (definition) file: LinkedList.cc

(9)

Outline

1 Announcements

2 Abstract Data Types (ADTs) Linked Lists

(10)

Polynomial Arithmetic

Lists of Lists: Student Database System, Sparse Matrices Radix Sort

(11)

Bucket Sort

With n integers in the range 0 . . . m − 1 we can sort them as follows:

create an array, arr, of m initialize each to 0

for each integer, i, of the n integers, bump arr[i]

now run through each index, j, of arr, printing out j arr[j] times

running time of this algorithm is

problems: storage and running time is a function both of

(12)

Sort 1001 integers in range 0 – 9

6 8 0 7 1 4 32 8 5 3 2 4 4 7 0 1 2 3 4 5 6 7 8 9

(13)

Radix Sort

To sort words of length p letters (b = 26 + 1) or integers of length p digits (b = 10) in range 0 . . . bp− 1 it is infeasible to allocate bpbuckets (as we did with single digits)

Instead, use b buckets and make p passes over the data, sorting by the position one to the left each time

To sort integers, b = 10; to sort alphabetic strings, b = 27 During the ith iteration, each bucket will be a linked list of all the items with identical radix in the ith least significant position

At the end of the iteration the buckets are emptied from left to right (lesser elements before greater), and from bottom of bucket to top

This suggests a list data structure for storing the contents of each bucket

(14)

Radix Sort (contd.)

Running time of radix sort is O(p(b + n)) since the algorithm does p bucket sorts

Can sort any set of integers representable in 32 bits in four passes if we use 28buckets where each bucket will hold integers that have the same 8-bit block in one of the four radix positions

Why does radix sort work? Hint: what is the ordering in a bucket after a pass of the algorithm?

(15)

Radix Sort (contd.)

Example

Sort the words of the phrase:

nae the cat is on the ice hat

Preprocessing step:since longest word has 3 letters, pad out all other words with dashes to be 3 “letters” long; a ’-’ precedes an ’a’ in the alphabet

(16)

Radix Sort (contd.)

nae the cat is− on− the ice hat After pass 1: Before pass 3: Before pass 2: After pass 2: nae nae the cat hat is− on−

is− on− the

the ice ice the nae ’−’ ’e’ ’t’ ’a’ ’c’ ’h’ ’n’ ’s’ is− on− nae the the ice

the the on− is− ice

After pass 3:

’h’ ’i’

’c’ ’t’

cat hat ice the

the is− nae on− ’o’ ’n’ cat hat cat hat cat hat Pass 1 2 3

References

Related documents

1) To distinguish between nucleotide (gene) and amino acid (protein) sequences, and understand that genes and proteins have abbreviated names for communication in science. 2)

Building on the marketing analytics in the core part of the program, this course looks at Search Engine Optimization (improving the ranking of web- sites on Google and other

Latour and Woolgar highlighted the importance of material objects in the construction of scientific facts - rats, mice, machines, chemicals, traces of paper coming out of

Conversely, 43.7% of all respondents who misused prescription drugs met criteria for alcohol dependence, problem gambling, and (or) had used illicit drugs in the past year..

That is why PATH * , in close partnership with NEPI, has collaborated since 2003 to increase the quality, safety and efficiency of immunization services.. Much of the work was

This will "hold polluters accountable, including those who disproportionately harm communities of colour and low-income communities; reduce greenhouse gas emissions; bolster

Students can earn credits toward their GSPIA degrees by taking courses in the Graduate School of Public Administration and/or the Graduate School of International Studies at

Reducing early school leaving is amongst the investment priorities of the European Social Fund, which Member States can use to develop policies in line with the integrated