




Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
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
1 / 8
This page cannot be seen from the preview
Don't miss anything!





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?
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:
Given:
after iteration #1 after iteration #2 after iteration #
iteration #1 (smallest at the top)
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
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( )
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 #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.
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