Learning Control Structures in Scheme: Bubblesort Algorithm, Study notes of Computer Science

An explanation of the bubblesort algorithm, which is used to sort a list of items using control structures such as sequencing, conditional execution, and bounded iteration. The document also discusses the concept of subroutines and their implementation in scheme.

Typology: Study notes

Pre 2010

Uploaded on 08/19/2009

koofers-user-nd1
koofers-user-nd1 🇺🇸

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
On the Board
Homework 3: due next Monday
First Test: out Wednesday, due following Wednesday
2 hours, take home, 3 or 4 questions
Focus on short programs, as in Homeworks 2 & 3
Why did we buy that textbook?
The last three weeks have been an intense short course in writing small
programs in Scheme. From this point forward, I expect to spend about 2/3
of the time on matters from the book and 1/3 of the time on programming.
Today, we’ll try to reconnect with Chapter 2.
Control Structures
We’ve already talked about this notion. The book mentions sequencing,
which each of you used in Homework 1. It mentions conditional execution,
in the form of an if-then-else construct. While Scheme has such a construct,
we prefer to use its cond structure, which has a flat structure rather than a
hierarchical structure. The book’s third control structure is iteration. In
Scheme, we have seen iteration implemented as structural recursion over
lists. (We’ll look at structural recursion over other inductive domains later
this week.) The book also describes bounded iteration in the form of a loop
over some pre-specified range.
for i 1 to n
do something of value
We will use this construct in class and in homework. We won’t program
this way in Scheme, because such loops are not a natural (integral?) part of
Scheme. (In particular, Scheme provides poor support for arrays. Most of
the examples you will see in the book that use bounded interation also use
vectors (1-dimensional arrays) or arrays.)
COMP 200: Elements of Computer Science
Fall 2004
Lecture 11: September 20, 2004
Reconnecting with the Book
pf3
pf4

Partial preview of the text

Download Learning Control Structures in Scheme: Bubblesort Algorithm and more Study notes Computer Science in PDF only on Docsity!

On the Board Homework 3: due next Monday First Test: out Wednesday, due following Wednesday 2 hours, take home, 3 or 4 questions Focus on short programs, as in Homeworks 2 & 3

Why did we buy that textbook?

The last three weeks have been an intense short course in writing small programs in Scheme. From this point forward, I expect to spend about 2/ of the time on matters from the book and 1/3 of the time on programming. Today, we’ll try to reconnect with Chapter 2.

Control Structures

We’ve already talked about this notion. The book mentions sequencing, which each of you used in Homework 1. It mentions conditional execution, in the form of an if-then-else construct. While Scheme has such a construct, we prefer to use its cond structure, which has a flat structure rather than a hierarchical structure. The book’s third control structure is iteration. In Scheme, we have seen iteration implemented as structural recursion over lists. (We’ll look at structural recursion over other inductive domains later this week.) The book also describes bounded iteration in the form of a loop over some pre-specified range.

for i  1 to n do something of value

We will use this construct in class and in homework. We won’t program this way in Scheme, because such loops are not a natural (integral?) part of Scheme. (In particular, Scheme provides poor support for arrays. Most of the examples you will see in the book that use bounded interation also use vectors (1-dimensional arrays) or arrays.)

COMP 200: Elements of Computer Science Fall 2004 Lecture 11: September 20, 2004

Reconnecting with the Book

An example of bounded iteration — Bubblesort

Chapter presents a simple sorting algorithm as its example to illustrate the combined use of sequencing, conditional execution, and bounded iteration. Bubblesort is one of the simplest sorts we can envision — at least in terms of its implementation. It is known as an “exchange sort” because the fundamental operation in bubblesort is swapping two items that are out of order. The concept is simple: given a list (or vector) of items to sort, the algorithm runs over the items and compares adjacent items. It they are out of order, it swaps them and continues. Large items shuffle toward their end of the array; small items shuffle in the other direction.

Assume a preexisting list of N items, stored in a vector “Keys”. The notation Keys[i] refers to the ith^ element of the vector Keys. for i  1 to N- for j  1 to N- if Keys[j] > Keys[j+1] then swap the contents of Keys[j] & Keys[j+1]

Work a simple example of eight numbers. Point out that it takes (N-1)^2 comparisons and (potentially) swaps. We can make the inner loop shorter each time, cutting the total cost in (roughly) half. We will come back to this point in several weeks when we discuss algorithmic complexity.

Consider a list of numbers that are already in ascending order. The discussion should lead to a version that stops when no swaps occur.

Swapped  true while (swapped) swapped  false for j  1 to N- if Keys[j] > Keys[j+1] then swap Keys[j] & Keys[j+1] swapped  true

Even though it looks as if the while(swapped) loop can run indefinitely, we know from our earlier reasoning that swap can be true at most N-1 times. Thus, the algorithm will halt after at most N iterations of the outer loop; in

Describe the inner loop first. Then work outward to the number of tims that it must run.

Following book’s convention that indentation indicates control. Both these execute under the control of the then clause

TemporaryHome  Keys[j] Keys[j]  Keys[j+1] Keys[j+1]  TemporaryHome

We could write these three lines into the algorithm, indented in an appropriate way (of course) and have a more complete specification. Doing so, however, complicated the expression of the algorithm and makes it harder to read and understand. An alternative approach would be to create a small program (subroutine) called swap that takes two elements of Keys and swaps them.

Swap(a,b) TemporaryHome  Keys[a] Keys[a]  Keys[b] Keys[b]  TemporaryHome

Now, we can replace the line in the algorithm that tells us to swap two keys with an invocation of “Swap” — written “ swap(j,j+1)” — and produce what is clearly a more completely defined algorithm without losing the clarity that comes from using a higher level of abstraction to avoid the details.

The key in choosing a level of abstraction is to ensure that we do not hide algorithmic complexity in one of these abstract operations, such as swap. If the implementation of swap ran over the entire Keys array (for some reason) or did it multiple times, then the use of abstraction would reduce our understanding of the algorithm — in particular, its cost or running time.