Lecture 4: Example Algorithm Development - Swapping and Selection Sort, Study notes of Civil Engineering

An explanation of how to swap variables and develop a selection sort algorithm. It includes pseudo-code and mathcad code examples. Part of a computer methods course by gurley.

Typology: Study notes

Pre 2010

Uploaded on 03/10/2009

koofers-user-sey
koofers-user-sey 🇺🇸

10 documents

1 / 8

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CGN 3421 - Computer Methods Gurley
Lecture 4 - Example Algorithm Development page 39 of 46
Lecture 4 - Example Algorithm Development
Another 2 algorithms - swapping and sorting
We saw at the end of Lecture 3 how to develop an algorithm to identify the maximum value out of a vec-
tor, and the location of that value. In this lecture we will combine that idea with the ability to swap values
within an array to create a simple sorting algorithm. Remember that there are already built-in Mathcad
commands to sort a vector. The purpose of this exercise is to learn the basic use of control structures and
algorithm development so that we can create our own more advanced programs later on.
swapping - switch the contents of two variables
Given: but I want i.e. I want to swap the contents of spots 1 and 2
How can I swap the contents of x1 with x2? Will this work??
x1 := x2
x2 := x1
Why not?
x1 := x2 ==> x1 = 3
x2 := x1 ==> x2 = 3
What we need is some place to temporarily store one or the other
temp := x1
x1 := x2
x2 := temp
temp := x1 ==> ,
x1 = x2 ==> ,
x2 = temp ==> ,
mission accomplished.
what would pseudo-code look like?
!
!
"
#
$!
"
!
#
$
"#$% !$ !
!
"
#
$
"#$% !$ !
"
"
#
$
"#$% !$ !
"
!
#
$
pf3
pf4
pf5
pf8

Partial preview of the text

Download Lecture 4: Example Algorithm Development - Swapping and Selection Sort and more Study notes Civil Engineering in PDF only on Docsity!

Lecture 4 - Example Algorithm Development

Another 2 algorithms - swapping and sorting

We saw at the end of Lecture 3 how to develop an algorithm to identify the maximum value out of a vec- tor, and the location of that value. In this lecture we will combine that idea with the ability to swap values within an array to create a simple sorting algorithm. Remember that there are already built-in Mathcad commands to sort a vector. The purpose of this exercise is to learn the basic use of control structures and algorithm development so that we can create our own more advanced programs later on.

swapping - switch the contents of two variables

Given: but I want i.e. I want to swap the contents of spots 1 and 2

How can I swap the contents of x 1 with x 2? Will this work?? x 1 := x 2 x2 := x 1

Why not? x1 := x 2 ==> x 1 = 3 x2 := x 1 ==> x 2 = 3

What we need is some place to temporarily store one or the other

temp := x 1 x1 := x 2 x2 := temp

temp := x 1 ==> ,

x1 = x2 ==> ,

x2 = temp ==> ,

mission accomplished. what would pseudo-code look like?

  1. identify the variables to swap
  2. save the contents of #1 to a temporary location
  3. replace contents of #1 with that of #
  4. replace the contents of #2 with that in the temporary location

From now on, this entire pseudo-code can be a one word command... SWAP variables #1 and #

selection sort - re-order contents of a vector from low to high, or high to low. Combines the previous two algorithms: 1) finding a maximum 2) swapping.

we have student grades

we want to re-order from LOWEST to HIGHEST

Pseudo-code:

  1. Find the location of the lowest value in the entire vector
  2. Swap the contents of that spot with the #1 spot
  3. find the location of the second lowest value in the entire vector a) or...find the lowest value in the entire vector, excluding #
  4. Swap the contents of that spot with the #2 spot
  5. Find the location of the lowest value remaining in the entire vector excluding spots #1 and #2.
  6. Swap the contents of that spot with #
  7. repeat steps 5) and 6), moving down one spot each time Sorting - let’s illustrate how we intend this algorithm to work ...

Given:

after iteration #1 after iteration #2 after iteration #

iteration #1 (smallest at the top)

  • Pseudo-code for iteration #1:
  • Fill in some Mathcad code for iteration #

Pseudo-code assume smallest value in the vector is already in spot 1 (set pointer = 1) compare the remaining values to find minimum (set first = 2) when the actual smallest value is found, swap with what is in spot #

Mathcad code to first iteration

ORIGIN (^) ≡ 1

INPUT VECTOR TO SORT

G

iteration_1 in( ) last ←length in( ) out (^) ←in ptr ← 1 first ← 2

ptr ← k if out (^) k <out (^) ptr

for k (^) ∈first (^) ..last

temp ←out (^1)

out (^1) ←out (^) ptr

out (^) ptr ←temp

out

G_1 (^) :=iteration_1 G( )

G_

OUTPUT OF ITERATION

iteration #2 (smallest remaining in spot 2)

Pseudo-code assume smallest is already in spot 2 (set ptr = 2) compare the remaining values to find minimum (set first = 3) when actual smallest is found, swap with #

Mathcad code

What has changed wince the first iteration? Just the assignment to ‘ptr’ and ‘first’ and the index to ‘out’

iteration_2 in( ) last ←length in( )

out ←in

ptr ← 2

first ← 3

ptr ← k if out k <out ptr

for k ∈first ..last

temp ←out 2

out 2 ←out ptr

out ptr ←temp

out

G_2 :=iteration_2 G_1( )

G_

             

OUTPUT OF ITERATION

iteration #i (smallest remaining value in spot i)

Pseudo-code assume smallest is already in spot i (set ptr = i) compare the remaining values to find minimum (set first = i+1) when actual smallest is found, swap with i

What’s left to do? Now we just have to put the process of performing i iterations into another loop so that iteration 1 is followed by 2, 3, etc. until the sorting is done.

‘i’ must be inside a loop counting from 1 to one less than the length of the vector ( length(G)-1 )

We will take the above algorithm to sort the ith iteration, and put it inside (nest it) of another loop which dictates which iteration we are in. The algorithm will then go through all necessary iterations in a single function call.

iteration_3 ( in) last ← length ( in)

out ← in

ptr ← i

first ← i + 1

ptr ←^ k if out k < out ptr

for k ∈first ..last

temp ← out i

out i ← out ptr

out ptr ← temp

out

i

the selection swap routine in Mathcad

G

             

:= sorter in( ) last ←length in( ) out ←in

ptr (^) ←i first (^) ←i (^) + 1

ptr ← k if out (^) k <out (^) ptr

for k ∈first ..last

temp (^) ←out (^) i

out (^) i ←out (^) ptr

out (^) ptr ←temp

for i ∈ 1 ..last − 1

out

:=

sorter G( )

             

=

all to the right of this line is the stuff we saw on the last page.

here is the outside loop we added to keep track of whic of the ‘i’ iterations we are currently in