Computer Science 2 2-4-2015 Sorting.

Slides:



Advertisements
Similar presentations
Chapter 12 Sorting and searching. This chapter discusses n Two fundamental list operations. u Sorting u Searching n Sorted lists. n Selection/bubble sort.
Advertisements

Sorting CMSC 201. Sorting In computer science, there is often more than one way to do something. Sorting is a good example of this!
CS107 Introduction to Computer Science Lecture 5, 6 An Introduction to Algorithms: List variables.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Computer Science 101 Introduction to Sorting. Sorting One of the most common activities of a computer is sorting data Arrange data into numerical or alphabetical.
Lecture #9: Sorting Algorithms خوارزميات الترتيب Dr. Hmood Al-Dossari King Saud University Department of Computer Science 22 April 2012.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Computer Science 1 How do you store a bunch of similar stuff?
Array Review Selection Sort Get out your notes.. Learning Objectives Be able to dry run programs that use arrays Be able to dry run programs that use.
Computer Science 2 Arrays of Records. First Record Program Input an unknown number of Team Names, School names, and Total scores. (While loop) –Output:
Searching and Sorting Copyright Prentice Hall (with additions / modifications by Evan Korth)
Copyright Prentice Hall Modified by Sana odeh, NYU
Lesson 5-15 AP Computer Science Principles
CMSC201 Computer Science I for Majors Lecture 24 – Sorting
Design and Analysis of Algorithms
Array Review Selection Sort
Last Class We Covered Data representation Binary numbers ASCII values
Computer Science Dynamics.
Computer Science 2 Hashing
Computer Science 2 Outline/Learning Objectives for the day:
How do you store a bunch of similar stuff?
Chapter 8 Arrays Objectives
Computer Science II First With files.
Computer Science II First With files.
Sorts on the AP Exam Insertion Sort.
Computer Science 2 Arrays of Records.
Computer Science 2 Heaps.
Computer Science 2 Review the Bubble Sort
Continue on the Array of Records program
Insertion Sort Quiz on Thursday.
Computer Science 2 Arrays of Records.
Computer Science 2 Getting an unknown # of …. Into an array.
Dry run Fix Random Numbers
Computer Science Dynamics.
Repeat Day2 Dry Run Second Repeat Program
Insertion Sort.
Computer Science 1 1/19/2011 On the record (paper) write the following
Computer Science 2: Trees
Computer Science 2 Arrays of Records.
How do you store a bunch of similar stuff?
Computer Science 2 Hashing.
Computer Science Sorting Pre-Test Discussion/program
Deleting from a binary tree
Computer Science 2 Tally Arrays 2/4/2016.
CS 2 Records 2/22/2018.
Chapter 8 Arrays Objectives
How can you make a guessing game?
Computer Science II Second With files.
Sorting Develop a sorting algorithm
Continue with Life, Magic and Catch -up
Computer Science 2 More Trees.
Computer Science 1 while
Computer Science 1 For..do loop Dry Run Take notes on the For loop
How do you store a bunch of similar stuff?
More Trees 5/9/2-017 Be able to dry run a program that uses trees
Computer Science I: Get out your notes.
Continue on the Valentines program
Computer Science 2 Queue.
Time to finish first stack program
Array Review Selection Sort
Program: Mini Phone Book 2/11/2016
Insertion Sort.
CS 2 2/25/2015 File Exists?.
Computer Science 1 while
Dry Run Fix it Write a program
Computer Science
A bit of Review Review, Dry Run, Program.
Insertion and Shell Sorts
Computer Science II First With files.
Presentation transcript:

Computer Science 2 2-4-2015 Sorting

Goals Dry run review Develop a process (pseudo code) and code to put a bunch of things in order. Write a program that will sort an array of records.

Dry Run!! for count:= 5 downto 1 do writeln(data[count].a:5, data[count].b:5:2); for count:= 1 to 4 do begin with data[count] do a := a + b; b := a*b; end; for count:= 1 to 5 do writeln(a,b:5:2) end. program Sample2; type rectype = record of a:integer; b:real; end; arraytype = array[1..5] of rectype; var data: arraytype; count:integer; begin for count := 1 to 5 do data[count].a:= 2* count; data[count].b:= count -2; for count:= 1 to 5 do data[count].b:= data[count].b +data[count].a;

Sorting How do you put things in order? (Low to high) Pseudo-Code Develop a sorting algorithm

So… How should this information be organized? Sorted by school? Sorted by name? Sorted by type of gift? Today’s goal. Be able to sort an array of records using one of the sorts we have seen previously.

Bubble Sort Low High 8 2 6 9 21 1 Speed: O(n2) Stability: Stable How it works: Check- Switch http://www.dat.ruc.dk/~keld/algoritmik_e99/Applets/Chap08/Bubble/BubbleSort.html Show it after each pass Low High 8 2 6 9 21 1

Bubble Sort: To sort by name {This is just the code for Bubble Sort} for pass:= 1 to (size -1) do begin for check:= 1 to size-1 do if TheArray[check] >TheArray[check+1] then dummy:= TheArray[check]; TheArray[check]:= TheArray[check+1]; TheArray[check+1]:= dummy; end; end;{Of the for loop}

Array of Record Sorting Assignment 10 name/phone numbers Get add the names and numbers Set it up as a menu (with the while loop) Show All Sort by Name Find Phone Number (Get a name and find the corresponding number) Add a person (Push) Quit After the user searches for one name, let them search for other names, until they are done. Push: Modify it to hold an unknown number of names. Push: Develop a faster method to find the person taking advantage of the data being in order.