DOC PREVIEW
TRINITY CSCI 1320 - Sorting

This preview shows page 1-2-3 out of 10 pages.

Save
View full document
Premium Document
Do you want full access? Go Premium and unlock all 10 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Sorting 3 25 2011 Opening Discussion Do you have any questions about the IcP Minute essay comments How to read the Scala API What would happen with an infinite loop that opens windows Move with click Click and drag Changing fonts How do I feel about Spurs Timmy Motivation There are many reasons that you might want the data you are working with to be in a particular order If nothing else humans often like seeing things in certain orders It turns out that ordered data can be beneficial for the computer as well Putting things in order by some value is called sorting Methods of Sorting If I ask you to sort a bunch of items how would you go about doing it Describe your approach How does it vary for different types or configurations of objects O n Sorts 2 We are going to look at three different sorting techniques today These sorts all do work that is proportional to the square of the number of elements That isn t good for large collections but the sorts are fairly simple to write These work in place so we use arrays Each involves an inner loop that reorders things and an outer loop that makes the inner one happen over and over Bubble Sort Inner loop Compare adjacent elements and swap them if they are out of order Outer loop Repeat n 1 times or until no swaps are done The latter option is called a flagged bubble sort Selection Sort This is often called a min sort or a max sort depending on how you write it I ll describe a min sort here Inner loop Outer loop Find the smallest element and SWAP it into position if not already there Repeat n 1 times so all elements are in the right place Does only O n swaps but still O n 2 comparisons Insertion Sort Inner loop Outer loop Take the next element and shift it down to the right spot Run through all the elements starting with the second This sort is actually a bit faster factor of 2 on random data It is really efficient on nearly sorted data Watching Them Work One advantage of doing graphics before sorting is that we can write code to visualize what is happening when we sort numbers with these sorts Let s write this code and watch our sorts work Minute Essay Show me what would happen after each iteration of the inner loop if we min sort these values 4 7 1 3 8 2 The IcP is on Monday


View Full Document

TRINITY CSCI 1320 - Sorting

Documents in this Course
Functions

Functions

10 pages

Functions

Functions

10 pages

Graphics

Graphics

10 pages

Graphics

Graphics

11 pages

Loops

Loops

4 pages

Loops

Loops

3 pages

Strings

Strings

9 pages

Functions

Functions

10 pages

Loops

Loops

11 pages

Graphics

Graphics

11 pages

Graphics

Graphics

12 pages

Sorting

Sorting

11 pages

Arrays

Arrays

10 pages

Loops

Loops

18 pages

Load more
Download Sorting
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Sorting and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Sorting and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?