Lecture 24: Linear and Bubble Sort Algorithms, Study notes of Computer Science

The notes from a university lecture on searching algorithms, specifically linear search and bubble sort. The lecture covers the implementation of these algorithms, their efficiency analysis, and comparisons between them. The document also includes examples and exercises.

Typology: Study notes

Pre 2010

Uploaded on 08/30/2009

koofers-user-h56
koofers-user-h56 🇺🇸

10 documents

1 / 37

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Previous Lecture:
Acoustic data: frequency computation
Touchtone phone
Today’s Lecture:
Search: Linear Search
Sort: Bubble Sort and Insertion Sort
Efficiency Analysis
Announcements
Prelim 3 will be returned after lecture. Or pick up from
CS1112 consultants in ACCEL during consulting hrs
(today after 4pm)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25

Partial preview of the text

Download Lecture 24: Linear and Bubble Sort Algorithms and more Study notes Computer Science in PDF only on Docsity!

„^ Previous Lecture:

„^ Acoustic data: frequency computation „^ Touchtone phone

„^ Today’s Lecture:

„^ Search: Linear Search „^ Sort: Bubble Sort and Insertion Sort „^ Efficiency Analysis

„^ Announcements

„^ Prelim 3 will be returned after lecture. Or pick up fromCS1112 consultants in ACCEL during consulting hrs(today after 4pm)

April 16, 2009

Lecture 24

2

Searching for an item in a collection

Is the collection organized?What is the organizing scheme?

Indiana Jones and the Raiders of the Lost Ark

April 16, 2009

Lecture 24

% f is index of first occurrence%^

of value x in vector v. % f is -1 if x not found.k= 1;while

k<=length(v) && v(k)~=x k= k + 1; endif

k>length(v)f= -1; % signal for x not found else

f= k; end

April 16, 2009

Lecture 24

%^

Linear Search % f is index of first occurrence%^

of value x in vector v. % f is -1 if x not found.k= 1;while

k<=length(v) && v(k)~=x k= k + 1; endif

k>length(v)f= -1; % signal for x not found else

f= k; end

v x^

April 16, 2009

Lecture 24

%^

Linear Search % f is index of first occurrence%^

of value x in vector v. % f is -1 if x not found.k= 1;while

k<=length(v) && v(k)~=x k= k + 1; endif

k>length(v)f= -1; % signal for x not found else

f= k; end

v x^

April 16, 2009

Lecture 24

%^

Linear Search % f is index of first occurrence%^

of value x in vector v. % f is -1 if x not found.k= 1;while

k<=length(v) && v(k)~=x k= k + 1; endif

k>length(v)f= -1; % signal for x not found else

f= k; end

v x^

What if

v^ is sorted?

April 16, 2009

Lecture 24

The “bubble” process

Compare adjacent values.Swap if “out of order.”

April 16, 2009

Lecture 24

The “bubble” process

Compare adjacent values.Swap if “out of order.”

April 16, 2009

Lecture 24

The “bubble” process

Compare adjacent values.Swap if “out of order.”

April 16, 2009

Lecture 24

The “bubble” process

Compare adjacent values.Swap if “out of order.”

April 16, 2009

Lecture 24

The second “bubble” process

Compare adjacent values.Swap if “out of order.”

April 16, 2009

Lecture 24

The second “bubble” process

Compare adjacent values.Swap if “out of order.”

April 16, 2009

Lecture 24

The second “bubble” process

Compare adjacent values.Swap if “out of order.”

April 16, 2009

Lecture 24

The second “bubble” process

After two bubble processes, thefirst two components aresorted.Repeatedly apply the bubbleprocess to sort the whole array