Computer Science 2 Getting an unknown # of …. Into an array. Looking at another type of sort.
Learning Objectives Review Bubble Sort Be able to put an unknown number of items into an array. Understand how a Selection Sort Works Be able to write a program that uses the selection sort.
Bubble Sort Speed = Stability = How it works =
Warm up Show the following after each pass of the Bubble Sort High Low 20 10 8 14 61 90
How can you store an unknown number of Song Titles into an array? Type Songtype = array[1..100] of string; Var Songs:songtype; numberOfSongs:integer;
Pseudo Code Use a while loop to get the information (Semantics of a while loop!) In the condition part of the while loop also check to make sure the user does not go beyond the number of elements in the array. Include a count variable to keep track of how many items were added to the array.
Code
Another Sort Selection Sort https://www.youtube.com/watch?v=bkf3idGw8xw Speed:_________ Stability: _____________ How it works: _____________
Showing it after each pass Low High 8 30 22 15 2 45 3 High Low 10 14 3 26 15 31 7
Selection Sort: Code {*** This is just the sorting code } for pass:= 1 to size-1 do begin mark:=pass; for check:= pass to size do if numbers[check] < numbers[mark] then mark:=check; {Mark} dummy:=numbers[mark]; {Switch} numbers[mark]:=numbers[pass]; numbers[pass]:= dummy; end; {Of the for pass loop}
Program Options Song Sort: Input an unknown number of songs (but less than 100) Show out of order Sort Show in order Push: Let the user also enter the Style of music, and be able to sort by style. Name age sort Input: An unknown number of names and ages Output: The names and corresponding ages in a chart sorted by age. Push: Let the user decide if they want to sort by age or name, then sort and show the chart. Stats are great Input:9 integers Output: The mean, median, mode and range of the values Push: Let the user enter an unknown number of scores, but less than 100 and calculate the above