Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 2 2-4-2015 Sorting.

Similar presentations


Presentation on theme: "Computer Science 2 2-4-2015 Sorting."— Presentation transcript:

1 Computer Science Sorting

2 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.

3 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;

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

5 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.

6 Bubble Sort Low High 8 2 6 9 21 1 Speed: O(n2) Stability: Stable
How it works: Check- Switch Show it after each pass Low High

7 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}

8 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.


Download ppt "Computer Science 2 2-4-2015 Sorting."

Similar presentations


Ads by Google