L4_Sorting_s02 - CGN 3421 - Computer Methods Gurley Lecture...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
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 x 1 with x 2 ? Will this work?? x 1 := x 2 x 2 := x 1 Why not? x 1 := x 2 ==> x 1 = 3 x 2 := x 1 ==> x 2 = 3 What we need is some place to temporarily store one or the other temp := x 1 x 1 := x 2 x 2 := temp temp := x 1 ==> , x 1 = x 2 ==> , x 2 = temp ==> , mission accomplished. what would pseudo-code look like? ! ! " # $ ! " ! # $ "#$% ! $ ! ! " # $ ! $ ! " " # $ ! $ ! " ! # $
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CGN 3421 - Computer Methods Gurley Lecture 4 - Example Algorithm Development page 40 of 46 1) identify the variables to swap 2) save the contents of #1 to a temporary location 3) replace contents of #1 with that of #2 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 #2 selection sort - re-order contents of a vector from low to high, or high to low.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 8

L4_Sorting_s02 - CGN 3421 - Computer Methods Gurley Lecture...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online