Alan YorinksLecture 7 1 Tonight we will look at:: List ADT Unsorted List Sequential Search ...

38
Alan Yorinks Lectur e 7 1 Lecture 7 Tonight we will look at:: List ADT Unsorted List Sequential Search Selection Sort Sorted List Binary Search

Transcript of Alan YorinksLecture 7 1 Tonight we will look at:: List ADT Unsorted List Sequential Search ...

Page 1: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 1

Lecture 7

Tonight we will look at::

List ADT Unsorted List Sequential Search Selection Sort Sorted List Binary Search

Page 2: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 2

What is a List? A list is a varying-length, linear collection

of homogeneous elements.

linear means each list element (except the first) has a unique predecessor, and each element (except the last) has a unique successor

Page 3: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 3

3 Basic Kinds of ADT Operations

Constructor -- creates a new instance (object) of an ADT

Transformer -- changes the state of one or more of the data values of an instance

Observer -- allows us to observe the state of one or more of the data values of an instance without changing them

Page 4: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 4

ADT List Operations Transformers

Insert Delete SelSort

Observers IsEmpty IsFull Length IsPresent Print

change state

observe state

Page 5: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 5

ADT Unsorted List Data Components

length

data[ 0 . . MAX_LENGTH -1 ]

number of elements in list

array of list elements

Page 6: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 6

IsFull

Length

SelSort

IsPresent

Delete

IsEmpty

Insert

Print

Private data:

length

data [ 0 ] [ 1 ] [ 2 ]

[MAX_LENGTH-1]

Array-based class List

Page 7: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 7

// SPECIFICATION FILE ARRAY-BASED LIST ( list.h ) const int MAX_LENGTH = 50 ; typedef int ItemType ;

class List // declares a class data type { public : // public member functions

List ( ) ; // constructor bool IsEmpty ( ) const ; bool IsFull ( ) const ; int Length ( ) const ; // returns length of list void Insert ( ItemType item ) ; void Delete ( ItemType item ) ; bool IsPresent( ItemType item ) const ; void SelSort ( ); void Print ( ) ;

private : // private data members

int length ; // number of values currently stored ItemType data[MAX_LENGTH] ; } ;

7

Array Based List Header

Page 8: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 8

Sorted and Unsorted Lists

UNSORTED LIST

Elements are placed into the list in no particular order.

SORTED LIST

List elements are in an order that is sorted in some way -- either numerically or alphabetically.

Page 9: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 9

// IMPLEMENTATION FILE ARRAY-BASED LIST ( list.cpp )

#include “list.h” #include <iostream>

using namespace std;

int List :: Length ( ) const { return length ; }

bool List :: IsFull ( ) const

return ( length == MAX_LENGTH ) ; }

Array Based List Implementation

Page 10: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 10

List :: List ( ) // Constructor { length = 0 ; }

void List :: Insert ( ItemType item ) { data [ length ] = item ; length++ ; }

Array Based List ImplementationCon't

Page 11: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 11

Before Inserting 64 into anUnsorted List

length 3

data [ 0 ] 15 [ 1 ] 39

[ 2 ] -90 [ 3 ]

. . .

[MAX_LENGTH-1]

The item willbe placed intothe length location,and length will beincremented.

item 64

Page 12: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 12

After Inserting 64 into anUnsorted List

length 4

data [ 0 ] 15 [ 1 ] 39

[ 2 ] -90 [ 3 ] 64

. . .

[MAX_LENGTH-1]

The item willbe placed intothe length location,and length will beincremented.

item 64

Page 13: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 13

bool List :: IsEmpty ( ) const { return ( length == 0 ) ; }

bool List :: IsPresent ( ItemType item ) const // Searches the list for item, reporting whether it was found { int index = 0 ;

while ( index < length && item != data [ index ] ) index++ ;

return ( index < length ) ; }

Array Based List ImplementationCon't

Page 14: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 14

void List :: Delete ( ItemType item ) //{ int index = 0 ;

while ( index < length && item != data [ index ] ) index++;

// if item found, move last element into item’s place if ( index < length ) { data [ index ] = data [length - 1 ] ; length-- ; } }

Array Based List ImplementationCon't

Page 15: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 15

Deleting 39 from anUnsorted List

index : 0

39 hasnot been matched.

item 39

length 4

data [ 0 ] 15 [ 1 ] 39

[ 2 ] -90 [ 3 ] 64

. .

. [MAX_LENGTH-1]

Page 16: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 16

Deleting 39 from anUnsorted List

Index : 1

item 39

length 4

data [ 0 ] 15 [ 1 ] 39

[ 2 ] -90 [ 3 ] 64

. . .

[MAX_LENGTH-1]

39 hasbeen matched.

Page 17: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 17

Deleting 39 from anUnsorted List

index : 1

item 39

length 4

data [ 0 ] 15 [ 1 ] 64

[ 2 ] -90 [ 3 ] 64

. . .

[MAX_LENGTH-1]

Placed copy oflast list elementinto the position where 39 was before.

Page 18: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 18

Deleting 39 from anUnsorted List

index : 1

item 39

length 3

data [ 0 ] 15 [ 1 ] 64

[ 2 ] -90 [ 3 ] 64

. . .

[MAX_LENGTH-1]

Decremented length.

Page 19: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 19

void List :: Print ( ) { int index ;

for ( index = 0 ; index < length ; index++ ) cout << data [ index ] << endl ; }

Printing the List

Page 20: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 20

Selection Sort Process

examines the entire list to select the smallest element. Then places that element where it belongs (with array subscript 0)

examines the remaining list to select the smallest element from it. Then places that element where it belongs (with array subscript 1)

.

examines the last 2 remaining list elements to select the smallest one. Then places that element where it belongs in the array

Page 21: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 21

Selection Sort Algorithm

FOR passCount going from 0 through length - 2 Find minimum value in data [ passCount . . length-

1 ] Swap minimum value with data [ passCount ] length = 5

data [ 0 ] 40 25 data [ 1 ] 100 100 data [ 2 ] 60 60 data [ 3 ] 25 40 data [ 4 ] 80 80

pass = 0

Page 22: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 22

void List :: SelSort ( ) // Sorts list into ascending order using selection sort { ItemType temp ; int passCount ; int sIndx ; int minIndx ; // index of minimum so far for ( passCount = 0 ; passCount < length - 1 ; passCount++ ) { minIndx = passCount ;

// find index of smallest of data [ passCount . . length-1 ] for ( sIndx = passCount + 1 ; sIndx < length ; sIndx+

+ ) if ( data [ sIndx ] = data [ minIndx ] ) minIndx = sIndx ; temp = data [ minIndx ] ; // swap data [ minIndx ] = data [ passCount ] ; data [ passCount ] = temp ; } }

Selection Sort Algorithm

Page 23: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 23

IsFull

Length

IsPresent

Delete

IsEmpty

Insert

Print

Private data:

lengthdata [ 0 ] [ 1 ] [ 2 ]

[MAX_LENGTH-1]

SortedList

Array-based class SortedList

Page 24: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 24

// SPECIFICATION FILE ARRAY-BASED SORTED LIST ( slist.h ) const int MAX_LENGTH = 50 ; typedef int ItemType ;

class SortedList { public : // public member functions

SortedList ( ) ; // constructor bool IsEmpty ( ) const ; bool IsFull ( ) const ; int Length ( ) const ; // returns length of list void Insert ( ItemType item ) ; void Delete ( ItemType item ) ; bool IsPresent( ItemType item ) const ; void Print ( ) ;

private : // private data members

int length ; // number of values currently stored ItemType data[MAX_LENGTH] ; void BinSearch ( ItemType item, bool& found, int& position ) const ;

} ;

24

Sorted List ADT

Page 25: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 25

Member Functions

Which member function specifications and implementations must change to ensure that any instance of SortedList ADT remains sorted at all times?

Insert

Delete

Page 26: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 26

Insert Algorithm for SortedList ADT

create space for the new item by shifting down all the larger list elements

put the new item in the list

increment length

Page 27: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 27

void SortedList :: Insert ( ItemType item ) { int index ; // find proper location for new element index = length - 1 ; // starting at bottom of array // shift down values larger than item // to make room for new item

while ( index >= 0 && item < data [ index ] ) { data [ index + 1 ] = data [ index ] ; index-- ; } // insert item into array data [ index ] = item ; length++ ; }

Sorted List Insert Implementation

Page 28: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 28

Delete Algorithm for SortedList ADT

find the position of the element to be deleted from the sorted list

eliminate space occupied by the item being deleted by shifting up all the larger list elements

decrement length

Page 29: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 29

void SortedList :: Delete ( ItemType item ) { bool found ; // true, if item is found int position ; // position of item, if found int index ;

// find location of element to be deleted BinSearch ( item, found, position) ; if ( found ) { // shift up elements that follow deleted item in sorted list

for ( index = position ; index < length + 1 ; index++ ) data [ index ] = data [ index + 1 ] ;

length-- ; }

Sorted List Delete Implementation

Page 30: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 30

Improving Member Function IsPresent for SortedList

Recall that with the unsorted List ADT we examined each list element beginning with data[ 0 ], until we either found a match with item, or we had examined all the elements in the unsorted List.

How can the searching algorithm be improved for SortedList ADT?

Page 31: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 31

Searching for 55 in aSortedList

Item 55

length 4

data [ 0 ] 15 [ 1 ] 39

[ 2 ] 64 [ 3 ] 90

. . .

[MAX_LENGTH-1]

A sequential search for 55 can stop when 64 has beenexamined.

Page 32: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 32

Binary Search in SortedList

Examines the element in the middle of the array. Is it the sought item? If so, stop searching. Is the middle element too small? Then start looking in second half of array. Is the middle element too large? Then begin looking in first half of the array.

Repeat the process in the half of the data that should be examined next.

Stop when item is found, or when there is nowhere else to look and item has not been found.

Page 33: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 33

void SortedList::BinSearch ( ItemType item, bool& found, int& position ) // Searches sorted list for item, returning position of item, if item was found { int middle ; int first = 0; int last = length - 1 ;

found = false ; while ( last >= first && !found ) { middle = ( first + last ) / 2 ; // INDEX OF MIDDLE ELEMENT if ( item < data [ middle ] ) last = middle - 1 ; // LOOK IN FIRST HALF NEXT else if ( item > data [ middle ] ) first = middle + 1 ; // LOOK IN SECOND HALF NEXT else found = true ; // ITEM HAS BEEN FOUND } if ( found ) position = middle ; }

Binary Search Implementation

Page 34: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 34

Trace of Binary Search

data[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

15 26 38 57 62 78 84 91 108 119

Item = 84

first middle last

data[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

15 26 38 57 62 78 84 91 108 119

first middle last

item > data [ middle ] first = middle + 1

item < data [ middle ] last = middle - 1

Page 35: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 35

Trace continued

data[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

15 26 38 57 62 78 84 91 108 119

Item = 84

first, last, middle

data[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

15 26 38 57 62 78 84 91 108 119

first, last middle

item == data [ middle ] found = true

item > data [ middle ] first = middle + 1

Page 36: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 36

Another Binary Search Trace

data[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

15 26 38 57 62 78 84 91 108 119

Item = 45

first middle last

data[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

15 26 38 57 62 78 84 91 108 119

first middle last

item < data [ middle ] last = middle - 1

item > data [ middle ] first = middle + 1

Page 37: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 37

Trace continued

data[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

15 26 38 57 62 78 84 91 108 119

Item = 45

first, middle, last

data[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

15 26 38 57 62 78 84 91 108 119

first, last middle

item < data [ middle ] last = middle - 1

item > data [ middle ] first = middle + 1

Page 38: Alan YorinksLecture 7 1  Tonight we will look at::  List ADT  Unsorted List  Sequential Search  Selection Sort  Sorted List  Binary Search.

Alan Yorinks Lecture 7 38

Trace concludes

data[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

15 26 38 57 62 78 84 91 108 119

Item = 45

last first

first > last found = false