CS1321: Introduction to Programming - PowerPoint PPT Presentation

1 / 43
About This Presentation
Title:

CS1321: Introduction to Programming

Description:

We can insert an item after the last item in our list... then insert the new one in front of the old one. Otherwise, just use recursion to insert the new ... – PowerPoint PPT presentation

Number of Views:30
Avg rating:3.0/5.0
Slides: 44
Provided by: Mon685
Category:

less

Transcript and Presenter's Notes

Title: CS1321: Introduction to Programming


1
CS1321Introduction to Programming
  • Georgia Institute of Technology
  • College of Computing
  • Breakout 05
  • Inserting into an Ordered List

2
Ordered Lists
We want to have TA structure instances stored in
a list in ascending order based on salary.
3
Lets start here given a list of TAs that is
already sorted. You want to add one new TA to
that list.
4
Insertion
Given the following list
There are basically 3 areas to insert a new value
5
Insertion
We can insert an item before the first item in
our list Thats pretty easy
6
Insertion
We can insert an item after the last item in our
list This takes a little more work, but is still
pretty easy, conceptually.
7
Insertion
Or, we can insert an item somewhere in the middle
of our list This one takes a little thought to
understand
8
Tasks
  • If our list of sorted TAs is empty, we need to be
    able to add our new TA to that empty list
  • If our list is not empty, does the new TA have a
    lower salary than the current TA on the list?
  • If so, then insert the new one in front of the
    old one.
  • Otherwise, just use recursion to insert the new
    TA somewhere in the rest of the list!

9
A bit of Abstraction
Lets abstract away that little testing to see if
one TAs salary is less than anothers. Contrac
t paid-less? ta-record ta-record -gt
boolean definition (define (paid-less? TAone
TAtwo) (lt (ta-record-salary TAone)
(ta-record-salary TAtwo)))
10
The Insertion Function
(define (insert new-ta ta-list)
  • Lets remember what we have to do
  • If the list is empty, just cons the new TA on
  • Otherwise, find a place where the new
  • TA is should go. Insert the TA there.

11
The Insertion Function
(define (insert new-ta ta-list) (cond
(empty? ta-list) (cons new-ta empty)
12
The Insertion Function
(define (insert new-ta ta-list) (cond
(empty? ta-list) (cons new-ta empty) else
(cond (paid-less? new-ta (first
ta-list))
If the new teaching assistant is paid less than
the existing TA, is this the proper place to
insert the new one?
13
The Insertion Function
(define (insert new-ta ta-list) (cond
(empty? ta-list) (cons new-ta empty) else
(cond (paid-less? new-ta (first
ta-list)) (cons new-ta ta-list)
Yes. Just cons the new TA onto the existing
list.
14
The Insertion Function
(define (insert new-ta ta-list) (cond
(empty? ta-list) (cons new-ta empty) else
(cond (paid-less? new-ta (first
ta-list)) (cons new-ta ta-list)
Else???
15
The Insertion Function
(define (insert new-ta ta-list) (cond
(empty? ta-list) (cons new-ta empty) else
(cond (paid-less? new-ta (first
ta-list)) (cons new-ta ta-list)
Else insert it later
16
The Insertion Function
(define (insert new-ta ta-list) (cond
(empty? ta-list) (cons new-ta empty) else
(cond (paid-less? new-ta (first
ta-list)) (cons new-ta ta-list)
else (cons (first ta-list)
(insert new-ta (rest ta-list))))))
17
Does this work?
empty
bubba
bob
billy
6.5
7.5
5.5
300
300
300
18
Does this work?
empty
bubba
bob
billy
6.5
7.5
5.5
300
300
300
bryan
4.5
300
19
Does this work?
empty
bubba
bob
billy
6.5
7.5
5.5
300
300
300
bryan
4.5
300
20
Does this work?
empty
bubba
bob
billy
6.5
7.5
5.5
300
300
300
bryan
4.5
300
21
Does this work?
empty
bubba
bob
billy
6.5
7.5
5.5
300
300
300
bryan
7.0
300
22
Does this work?
empty
bubba
bob
billy
This isnt right (cons first of the list onto
recursive call with rest of the list)
6.5
7.5
5.5
300
300
300
bryan
7.0
300
23
Does this work?
empty
bubba
bob
billy
6.5
7.5
5.5
300
300
300
bryan
NO! Keep going! consing as we go
7.0
300
24
Does this work?
empty
bubba
bob
billy
6.5
7.5
5.5
300
300
300
bryan
7.0
300
25
Does this work?
empty
bubba
bob
billy
6.5
7.5
5.5
300
300
300
bryan
7.0
300
26
And to cut a long story short
bubba
bob
billy
6.5
7.5
5.5
300
300
300
empty
bryan
8.0
300
27
Our Task
The Insertion Sort. It will take in an unordered
list of TAs, and produce a list of the same TAs,
but with the TAs placed in the list in ascending
order based on TA salary.
28
So How Do We Work this?
Lets look at our template again (define
(process-list-of-tas in-list) (cond (empty?
in-list) else (first
in-list) (process-list-of-tas (rest
in-list)) ))
29
Lets fill in the function based on the template
The easy parts (define (sort myList) (cond
(empty? myList) empty else (first
myList) (sort (rest myList)) ))
30
Nows the sticky part
What do we do if our list is not empty?
(else (first myList) (sort
(rest myList))
What do we know?
  • We can only deal with the first item directly
  • Were writing a function sort which will sort a
    list

31
What else?
Well, we just wrote a function insert which has
the ability to insert a single ta-record into a
list of sorted ta-records and maintain the sorted
order of the list So if we trust the recursive
call to handle the sorting of the rest of the
list
32
We find our solution
(define (sort myList) (cond (empty? myList)
empty else (insert (first myList)
(sort (rest myList)))))
Wait a sec ... Where's the rest of it?
33
Congratulations!
Youve just written insertion-sort! (We could
also test it with an insertion-sort on a list
that only takes numbers ) More examples read
chapter 12!
34
Intermezzo II
35
Hooray! Were no longer beginners!
You are no longer beginners. Its time for you
to step up to the next level of scheme
Intermediate
36
So whats changed?
Click here for A Full Comparison Between Language
Levels The biggest impact this switch in
language has is when describing lists
37
Before and After
38
Intermediate Mode Lets Us Abbreviate
No longer do we have to type in (cons a
(cons b (cons z empty))) to produce a list. We
now assume you understand a list is composed of
cons cells. So we abbreviate
(list a b z)
39
(list
)
a
b
c
40
We could go even further
Intermediate Student gives us the ability to
shorten our input even further using the quote.
In previous examples, quote was only used to say
interpret the next value literally. a is the
symbol a. Now, the combination ( allows us to
say What follows will be a list of items. Quote
where applicable.
41
Which makes this
(cons (cons a (cons b empty)) (cons (cons (cons
c (cons d empty)) empty) (cons f empty)))
As easy as saying this
'((a b) ((c d)) f)
42
(No Transcript)
43
Do my old functions still work?
Yes (first ltlistgt) (rest ltlistgt)
(empty? ltlistgt) (cons? ltcons cellgt)
(list? ltlistgt)
Write a Comment
User Comments (0)
About PowerShow.com