64
Week 6 Part 2 Kyle Dewey

Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Embed Size (px)

Citation preview

Page 1: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Week 6 Part 2Kyle Dewey

Page 2: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Overview

•Array application: sorting

•Basic structs

•TA Evaluations

Page 3: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Sorting

Page 4: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Sorting

•Given some list of items in any given order, put them in sorted order

•List of numbers ordered by <=

•List of words in lexicographic order

•List of plane flights in order by cost

Page 5: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Sorting

•5, 3, 7, 2: (by <=)

•2, 3, 5, 7

•5, 3, 7, 2: (by >=)

•7, 5, 3, 2

•“moo”, “cow”, “bull” (lexicographic)

•“bull”, “cow”, “moo”

Page 6: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Sorting Hard Drives

Maker Capacity Price RatingWarrant

y

Seagate 500 GB $80 4 / 5 3 years

Seagate 500 GB $150 5 / 5 5 years

Hitachi 750 GB $75 2 / 5 1 year

Page 7: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

The Point

•The same items can have different ways of being compared

Page 8: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Thought Exercise

•Given a bunch of integers, devise 3 unique ways to sort them by <= (no code!)

•If they end up being sorted in the end, the method is valid

6 2 4 1 0 9 7

Page 9: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Relevance to Us

•A lot of work has gone into sorting things

•Wikipedia page on sorting: 33 different methods

•There are more

•Some generally more efficient than others, some more efficient given data that looks a certain way (such as “nearly sorted”)

Page 10: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Relevance to C

•There is a good chunk of code that goes into even the simpler ones

•Usually involve lots of array operations

6 2 4 1 0 9 7

Page 11: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

•Basic idea: in an array of length N...

•Find the minimal element and swap it so that it’s in position 0

•Then, from position 1 and on, find the minimal element and replace it so it’s in position 1

•Keep doing this

Page 12: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

6 2 4 1 0 9 7

Swap Min

Recorded Minimum Position: 0Recorded Minimum: 6

0 1 2 3 4 5 6

Page 13: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

6 2 4 1 0 9 7

Swap Min

Recorded Minimum Position: 1Recorded Minimum: 2

0 1 2 3 4 5 6

Page 14: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

6 2 4 1 0 9 7

Swap Min

Recorded Minimum Position: 1Recorded Minimum: 2

0 1 2 3 4 5 6

Page 15: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

6 2 4 1 0 9 7

Swap Min

Recorded Minimum Position: 3Recorded Minimum: 1

0 1 2 3 4 5 6

Page 16: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

6 2 4 1 0 9 7

Swap Min

Recorded Minimum Position: 4Recorded Minimum: 0

0 1 2 3 4 5 6

Page 17: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

6 2 4 1 0 9 7

Swap Min

Recorded Minimum Position: 4Recorded Minimum: 0

0 1 2 3 4 5 6

Page 18: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

6 2 4 1 0 9 7

Swap Min

Recorded Minimum Position: 4Recorded Minimum: 0

0 1 2 3 4 5 6

Page 19: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

0 2 4 1 6 9 7

Swap Min

Recorded Minimum Position: 4Recorded Minimum: 0

0 1 2 3 4 5 6

Page 20: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

0 2 4 1 6 9 7

Swap Min

Recorded Minimum Position: 1Recorded Minimum: 2

0 1 2 3 4 5 6

Page 21: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

0 2 4 1 6 9 7

Recorded Minimum Position: 1Recorded Minimum: 2

0 1 2 3 4 5 6

Swap Min

Page 22: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

0 2 4 1 6 9 7

Recorded Minimum Position: 3Recorded Minimum: 1

0 1 2 3 4 5 6

Swap Min

Page 23: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

0 2 4 1 6 9 7

Recorded Minimum Position: 3Recorded Minimum: 1

0 1 2 3 4 5 6

Swap Min

Page 24: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

0 2 4 1 6 9 7

Recorded Minimum Position: 3Recorded Minimum: 1

0 1 2 3 4 5 6

Swap Min

Page 25: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

0 2 4 1 6 9 7

Recorded Minimum Position: 3Recorded Minimum: 1

0 1 2 3 4 5 6

Swap Min

Page 26: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

0 1 4 2 6 9 7

Recorded Minimum Position: 3Recorded Minimum: 1

0 1 2 3 4 5 6

Swap Min

Page 27: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

0 1 4 2 6 9 7

Recorded Minimum Position: 2Recorded Minimum: 4

0 1 2 3 4 5 6

Swap Min

Page 28: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

0 1 4 2 6 9 7

Recorded Minimum Position: 3Recorded Minimum: 3

0 1 2 3 4 5 6

Swap Min

Page 29: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

0 1 4 2 6 9 7

Recorded Minimum Position: 3Recorded Minimum: 3

0 1 2 3 4 5 6

Swap Min

Page 30: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

0 1 4 2 6 9 7

Recorded Minimum Position: 3Recorded Minimum: 3

0 1 2 3 4 5 6

Swap Min

Page 31: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

0 1 4 2 6 9 7

Recorded Minimum Position: 3Recorded Minimum: 3

0 1 2 3 4 5 6

Swap Min

Page 32: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

0 1 2 4 6 9 7

Recorded Minimum Position: 3Recorded Minimum: 3

0 1 2 3 4 5 6

Swap Min

Page 33: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

0 1 2 4 6 9 7

Recorded Minimum Position: 3Recorded Minimum: 4

0 1 2 3 4 5 6

Swap Min

Page 34: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

0 1 2 4 6 9 7

Recorded Minimum Position: 3Recorded Minimum: 4

0 1 2 3 4 5 6

Swap Min

Page 35: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

0 1 2 4 6 9 7

Recorded Minimum Position: 3Recorded Minimum: 4

0 1 2 3 4 5 6

Swap Min

Page 36: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

0 1 2 4 6 9 7

Recorded Minimum Position: 3Recorded Minimum: 4

0 1 2 3 4 5 6

Swap Min

Page 37: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

0 1 2 4 6 9 7

Recorded Minimum Position: 4Recorded Minimum: 6

0 1 2 3 4 5 6

Swap Min

Page 38: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

0 1 2 4 6 9 7

Recorded Minimum Position: 4Recorded Minimum: 6

0 1 2 3 4 5 6

Swap Min

Page 39: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

0 1 2 4 6 9 7

Recorded Minimum Position: 4Recorded Minimum: 6

0 1 2 3 4 5 6

Swap Min

Page 40: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

0 1 2 4 6 9 7

Recorded Minimum Position: 4Recorded Minimum: 6

0 1 2 3 4 5 6

Swap Min

Page 41: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

0 1 2 4 6 9 7

Recorded Minimum Position: 5Recorded Minimum: 9

0 1 2 3 4 5 6

Swap Min

Page 42: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

0 1 2 4 6 9 7

Recorded Minimum Position: 6Recorded Minimum: 7

0 1 2 3 4 5 6

Swap Min

Page 43: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

0 1 2 4 6 7 9

Recorded Minimum Position: 6Recorded Minimum: 7

0 1 2 3 4 5 6

Swap Min

Page 44: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

0 1 2 4 6 7 9

Recorded Minimum Position: 6Recorded Minimum: 9

0 1 2 3 4 5 6

Swap Min

Page 45: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Selection Sort

0 1 2 4 6 7 9

Recorded Minimum Position: 6Recorded Minimum: 9

0 1 2 3 4 5 6

Swap Min

Page 46: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Code

Page 47: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Structs

Page 48: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Problem

•We want to represent a phone book

•Each entry has:

•Name

•Phone number

•Address

Page 49: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Question

•Which type(s) is/are appropriate for:

•Name?

•Phone Number?

•Address?

Page 50: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Possible Representation

•Use parallel arrays

•Each array holds one kind of item

•Index N refers to all information for entry #N

char** name;char** address;int* phoneNumber;

Page 51: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Problem

•Poor separation of concerns

•We have to pass around everything related to one person, which is annoying and error prone

void printPerson( char* name, char* address, int phone );

Page 52: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Another Solution

•Use structures, aka. structs

•Put all data relevant to one entry in one place

struct person { char* name; char* address; int phone;};

Page 53: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Structs

struct person { char* name; char* address; int phone;};

void printPerson( struct person p );

Page 54: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Accessing Structs•Use the dot (.) operator

void printPerson( struct person p ) { printf( “Name: %s\n”, p.name ); printf( “Address: %s\n”, p.address ); printf( “Phone: %i\n”, p.phone );}

struct person { char* name; char* address; int phone;};

Page 55: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Modifying Structs•The dot (.) operator can be used

along with assignmentstruct person { char* name; char* address; int phone;};struct person p;p.name = “foo”;p.address = “123 Fake Street”;p.phone = 0123456789

Page 56: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Pointers to Structs

•Structs can also be accessed via pointers

•Can access like so:

struct person p;struct person* pointer = &p;(*p).name = “foo”;(*p).address = (*p).name;(*p).phone = 0123456789

Page 57: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Pointers to Structs

•Structs can also be accessed via pointers

•Can also access with the more readable arrow operator

struct person p;struct person* pointer = &p;p->name = “foo”;p->address = p->name;p->phone = 0123456789

Page 58: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

More on Structs (Only if Time

Permits)

Page 59: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Struct Semantics

•Consider again:

void printPerson( struct person p );

•When structs are passed, the whole thing is copied

•Note that this is a shallow copy

Page 60: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Shallow Copystruct person { char* name; char* address; int phone;};

“Bob” “124 Fake Street”

9876543210namename addressaddress

phone

Page 61: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Shallow Copy

“Bob” “124 Fake Street”

9876543210namename addressaddress

phone

9876543210

phone

namename addressaddress

Page 62: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Questionstruct foo { int x;};void bar( struct foo f ) { f.x = 10;}int main() { struct foo f; f.x = 5; bar( f ); // what’s f.x? return 0;}

Page 63: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Questionstruct foo { char* x;};void bar( struct foo f ) { f.x = “moo”;}int main() { struct foo f; f.x = “cow”; bar( f ); // what’s f.x? return 0;}

Page 64: Week 6 Part 2 Kyle Dewey. Overview Array application: sorting Basic structs TA Evaluations

Structs and Pointers

•Oftentimes programmers will prefer pointers to structs as opposed to just structs

•Avoids extra copying

•Possibly appropriate