강의자료9

412 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
412
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; case EQUAL : found = true ; item = info[ location ] ; break ; } switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; case EQUAL : found = true ; item = info[ location ] ; break ; } switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; case EQUAL : found = true ; item = info[ location ] ; break ; } ound
  • switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; case EQUAL : found = true ; item = info[ location ] ; break ; } switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; case EQUAL : found = true ; item = info[ location ] ; break ; } switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; case EQUAL : found = true ; item = info[ location ] ; break ; } ound
  • switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; case EQUAL : found = true ; item = info[ location ] ; break ; } switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; case EQUAL : found = true ; item = info[ location ] ; break ; } switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; case EQUAL : found = true ; item = info[ location ] ; break ; } ound
  • switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; case EQUAL : found = true ; item = info[ location ] ; break ; } switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; case EQUAL : found = true ; item = info[ location ] ; break ; } switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; case EQUAL : found = true ; item = info[ location ] ; break ; } ound
  • switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; case EQUAL : found = true ; item = info[ location ] ; break ; } switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; case EQUAL : found = true ; item = info[ location ] ; break ; } switch ( item.ComparedTo( info[location] ) ) { case LESS : case GREATER : location++ ; moreToSearch = ( location < length ) ; case EQUAL : found = true ; item = info[ location ] ; break ; } ound
  • 강의자료9

    1. 1. ADT Sorted List ( 정렬 리스트 )
    2. 2. List Definitions <ul><li>Logical Properties of (Linear) Lists </li></ul><ul><ul><li>An ordered collection of homogeneous elements ( 순서가 있는 동형의 원소들의 모임 ) </li></ul></ul><ul><ul><li>Operations </li></ul></ul><ul><ul><li>create the list ( 리스트 생성 ) </li></ul></ul><ul><ul><li>determine whether the list is empty ( 리스트가 비어있는지를 결정 ) </li></ul></ul><ul><ul><li>find the size (length) of the list ( 리스트의 길이 구하기 ) </li></ul></ul><ul><ul><li>find the index of the element ( 원소의 위치 구하기 ) </li></ul></ul><ul><ul><li>retrieve the element at a particular index ( 특정한 위치에 있은 원소 검색 ) </li></ul></ul><ul><ul><li>change the element at a particular index to a different value ( 특정한 위치의 원소를 다른 값을 변경 ) </li></ul></ul><ul><ul><li>insert an element at a particular index ( 특정한 위치에 원소를 삽입 ) </li></ul></ul><ul><ul><li>remove an element at a particular index ( 특정한 위치의 원소를 삭제 ) </li></ul></ul><ul><li>Length ( 길이 ) </li></ul><ul><li>The number of items in a list; the length can vary over time. </li></ul>
    3. 3. List Definitions <ul><li>Logical Properties – another definition </li></ul><ul><ul><li>A collection of homogeneous elements with linear relationship between elements ( 순서가 있는 동형의 원소들의 모임 ) </li></ul></ul><ul><ul><li>operations: </li></ul></ul><ul><ul><li>create the list ( 리스트 생성 ) </li></ul></ul><ul><ul><li>determine whether the list is empty ( 리스트가 비어있는지를 결정 ) </li></ul></ul><ul><ul><li>find the size (length) of the list ( 리스트의 길이 구하기 ) </li></ul></ul><ul><ul><li> r etrieve an i tem with given key value ( 특정한 키의 원소 검색 ) </li></ul></ul><ul><ul><li> i nsert an i tem ( 원소의 삽입 ) </li></ul></ul><ul><li> d elete an i tem with given key value ( 특정한 키의 원소 삭제 ) </li></ul><ul><li>Key ( 키 ) </li></ul><ul><li>The attributes that are used to determine the logical order of the list. </li></ul><ul><li>리스트의 논리적 순서를 정하는데 사용되는 속성들 </li></ul><ul><li>Length ( 길이 ) </li></ul><ul><li>The number of items in a list; the length can vary over time. </li></ul>
    4. 4. List Definitions <ul><li>Unsorted list ( 비정렬 리스트 ) </li></ul><ul><li>A list in which data items are placed in no particular order; </li></ul><ul><ul><li>the only relationship between data elements is the list </li></ul></ul><ul><li>predecessor and successor relationships . </li></ul><ul><li>Sorted list ( 정렬 리스트 ) </li></ul><ul><li>A list that is sorted by the value in the key; </li></ul><ul><ul><li>there is a semantic relationship among the keys of </li></ul></ul><ul><li>the items in the list. </li></ul>
    5. 5. Abstract Data Type (ADT) <ul><li>A data type whose properties (domain and operations) are specified independently of any particular implementation. </li></ul>
    6. 6. ADT Sorted List Operations <ul><li>Transformers </li></ul><ul><ul><li>MakeEmpty </li></ul></ul><ul><ul><li>InsertItem </li></ul></ul><ul><ul><li>DeleteItem </li></ul></ul><ul><li>Observers </li></ul><ul><ul><li>IsFull </li></ul></ul><ul><ul><li>LengthIs </li></ul></ul><ul><ul><li>RetrieveItem </li></ul></ul><ul><li>Iterators </li></ul><ul><ul><li>ResetList </li></ul></ul><ul><ul><li>GetNextItem </li></ul></ul>change state observe state process all
    7. 7. ADT 와 C++ 클래스 <ul><li>C++ 은 클래스 기법을 사용하여 ADT 를 표현 </li></ul><ul><li>C++ 클래스의 구성 </li></ul><ul><ul><li>(1) 클래스 이름 </li></ul></ul><ul><ul><li>(2) 데이타 멤버 </li></ul></ul><ul><ul><li>(3) 멤버 함수 : 연산의 집합 </li></ul></ul>
    8. 8. <ul><li>// SPECIFICATION FILE ( Sorted.h ) </li></ul><ul><li>class SortedType // declares a class data type </li></ul><ul><li>{ </li></ul><ul><li>public : // 8 public member functions </li></ul><ul><li>SortedType ( ) ; </li></ul><ul><li>bool IsFull ( ) const ; </li></ul><ul><li>int LengthIs ( ) const ; // returns length of list </li></ul><ul><li>void RetrieveItem ( ItemType& item, bool& found ) ; </li></ul><ul><li>void InsertItem ( ItemType item ) ; </li></ul><ul><li>void DeleteItem ( ItemType item ) ; </li></ul><ul><li>void ResetList ( ); </li></ul><ul><li>void GetNextItem ( ItemType& item ) ; </li></ul><ul><li>private : // 3 private data members </li></ul><ul><li>// 리스트를 배열로 표현 </li></ul><ul><li>int length ; </li></ul><ul><li>ItemType info[MAX_ITEMS] ; </li></ul><ul><li>int currentPos ; </li></ul><ul><li>} ; </li></ul>struct ItemType { … . KeyType key; … };
    9. 9. Class Interface Diagram SortedType class IsFull LengthIs ResetList DeleteItem InsertItem SortedType RetrieveItem GetNextItem Private data: length info [ 0 ] [ 1 ] [ 2 ] [MAX_ITEMS-1] currentPos
    10. 10. <ul><li>자료의 표현 </li></ul><ul><li>연산 ( 멤버함수 ) 의 구현 </li></ul><ul><ul><li>자료의 표현방법에 따라 멤버함수의 구현이 달라진다 . </li></ul></ul><ul><ul><li>자료를 어떻게 표현하느냐에 따라 멤버함수의 효율성 ( 수행시간 ) 을 결정 </li></ul></ul>구현 ( IMPLEMENTATION )
    11. 11. 배열 구현 <ul><ul><li>자료를 배열로 표현 </li></ul></ul><ul><li>int length ; </li></ul><ul><li>ItemType info[MAX_ITEMS] ; </li></ul><ul><li>int currentPos ; </li></ul>currentPos length info [ 0 ] [ 1 ] [ 2 ] [MAX_ITEMS-1]
    12. 12. Sorted Type Class Interface Diagram SortedType class IsFull LengthIs ResetList DeleteItem InsertItem MakeEmpty RetrieveItem Private data: length info [ 0 ] [ 1 ] [ 2 ] [MAX_ITEMS-1] currentPos GetNextItem
    13. 13. <ul><li>// IMPLEMENTATION FILE ARRAY-BASED LIST ( unsorted.cpp ) </li></ul><ul><li>SortedType::SortedType ( ) </li></ul><ul><li>// Pre: None. </li></ul><ul><li>// Post: List is empty. </li></ul><ul><li>{ </li></ul><ul><li>length = 0 ; </li></ul><ul><li>} </li></ul>멤버 함수 구현
    14. 14. <ul><li>int SortedType::LengthIs ( ) const </li></ul><ul><li>// Pre: List has been initialized. </li></ul><ul><li>// Post: Function value == ( number of elements in list ). </li></ul><ul><li>{ </li></ul><ul><li>return length ; </li></ul><ul><li>} </li></ul><ul><li>bool SortedType::IsFull ( ) const </li></ul><ul><li>// Pre: List has been initialized. </li></ul><ul><li>// Post: Function value == ( list is full ). </li></ul><ul><li>{ </li></ul><ul><li>return ( length == MAX_ITEMS ) ; </li></ul><ul><li>} </li></ul>
    15. 15. Member functions <ul><li>Which member function specifications and implementations must change to ensure that any instance of the Sorted List ADT remains sorted at all times? </li></ul><ul><ul><li>InsertItem </li></ul></ul><ul><ul><li>DeleteItem </li></ul></ul>
    16. 16. InsertItem algorithm forSortedList ADT <ul><li>Find proper location for the new element in the sorted list. </li></ul><ul><li>Create space for the new element by moving down all the list elements that will follow it. </li></ul><ul><li>Put the new element in the list. </li></ul><ul><li>Increment length. </li></ul>
    17. 17. Implementing SortedType me mber function InsertItem // IMPLEMENTATION FILE (sorted.cpp) #include “itemtype.h” // also must appear in client code void SortedType :: InsertItem ( ItemType item ) // Pre: List has been initialized. List is not full. // item is not in list. // List is sorted by key member using function ComparedTo. // Post: item is in the list. List is still sorted. { . . . }
    18. 18. <ul><li>void SortedType :: InsertItem ( ItemType item ) </li></ul><ul><li>{ bool moreToSearch ; </li></ul><ul><li>int location = 0 ; </li></ul><ul><li>// find proper location for new element </li></ul><ul><li>moreToSearch = ( location < length ) ; </li></ul><ul><li>while ( moreToSearch ) </li></ul><ul><li>{ </li></ul><ul><li>if (item.key < info[location].key) </li></ul><ul><li>moreToSearch = false; </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>location++; </li></ul><ul><li>moreToSearch = (location < length); </li></ul><ul><li>} </li></ul><ul><li>// make room for new element in sorted list </li></ul><ul><li>for ( int index = length ; index > location ; index-- ) </li></ul><ul><li> info [ index ] = info [ index - 1 ] ; </li></ul><ul><li>info [ location ] = item ; </li></ul><ul><li>length++ ; </li></ul><ul><li>} </li></ul>I nsertItem
    19. 19. InsertItem void SortedType :: InsertItem ( ItemType item ) { int location = 0 ; // find proper location for new element while ( location < length ) { if (item.key < info[location].key) break; else location++; } // make room for new element in sorted list for ( int index = length ; index > location ; index-- ) info [ index ] = info [ index - 1 ] ; info [ location ] = item ; length++ ; }
    20. 20. DeleteItem algorithm for SortedList ADT <ul><li>Find the location of the element to be deleted from the sorted list. </li></ul><ul><li>Eliminate space occupied by the item being deleted by moving up all the list elements that follow it. </li></ul><ul><li>Decrement length . </li></ul>
    21. 21. Delete Item <ul><li>void SortedType :: DeleteItem ( ItemType item ) </li></ul><ul><li>{ </li></ul><ul><li>int location = 0 ; </li></ul><ul><li>/ / find location of element to be deleted </li></ul><ul><li>while ( item. key != info[location] .key ) </li></ul><ul><li>location++ ; </li></ul><ul><li>// move up elements that follow deleted item in sorted list </li></ul><ul><li>for ( int index = location + 1 ; index < length; index++ ) </li></ul><ul><li> info [ index - 1 ] = info [ index ] ; </li></ul><ul><li>length-- ; </li></ul><ul><li>} </li></ul>
    22. 22. <ul><li>void SortedType::RetrieveItem ( ItemType& item, bool& found ) </li></ul><ul><li>// Pre: Key member of item is initialized. </li></ul><ul><li>// Post: If found, item’s key matches an element’s key in the list and a copy of that element </li></ul><ul><li>// has been stored in item; otherwise, item is unchanged. </li></ul><ul><li>{ </li></ul><ul><li>bool moreToSearch ; </li></ul><ul><li>int location = 0 ; </li></ul><ul><li>found = false ; </li></ul><ul><li>moreToSearch = ( location < length ) ; </li></ul><ul><li>while ( moreToSearch && !found ) </li></ul><ul><li>{ if (item.key == info[location].key) </li></ul><ul><li>{ </li></ul><ul><li>found = true; </li></ul><ul><li>item = info[location]; </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>{ location++; </li></ul><ul><li>moreToSearch = (location < length); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Retrieve Item
    23. 23. <ul><li>void SortedType::RetrieveItem ( ItemType& item, bool& found ) </li></ul><ul><li>// Pre: Key member of item is initialized. </li></ul><ul><li>// Post: If found, item’s key matches an element’s key in the list and a copy of that element </li></ul><ul><li>// has been stored in item; otherwise, item is unchanged. </li></ul><ul><li>{ </li></ul><ul><li>int location = 0 ; </li></ul><ul><li>found = false ; </li></ul><ul><li>while ( location < length && !found ) </li></ul><ul><li>{ if (item.key == info[location].key) </li></ul><ul><li>{ </li></ul><ul><li>found = true; </li></ul><ul><li>item = info[location]; </li></ul><ul><li>} </li></ul><ul><li>else </li></ul><ul><li>location++; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
    24. 24. Improving member function RetrieveItem <ul><li>Recall that with the Unsorted List ADT we examined each list element beginning with info[ 0 ], until we either found a matching key, or we had examined all the elements in the Unsorted List. </li></ul><ul><li>How can the searching algorithm be improved for Sorted List ADT? </li></ul>
    25. 25. Binary Sea r ch ( 이진탐색 ) in a Sorted List <ul><li>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. </li></ul><ul><li>Repeat the process in the half of the list that should be examined next. </li></ul><ul><li>Stop when item is found, or when there is nowhere else to look and item has not been found. </li></ul>
    26. 26. <ul><li>void SortedType::RetrieveItem ( Item Type& item, bool& found ) </li></ul><ul><li>// Pre: Key member of item is initialized. </li></ul><ul><li>// Post: If found, item’s key matches an element’s key in the list </li></ul><ul><li>// and a copy of that element has been stored in item; otherwise, item is unchanged. </li></ul><ul><li>{ </li></ul><ul><li>int midPoint ; </li></ul><ul><li>int first = 0; </li></ul><ul><li>int last = length - 1 ; </li></ul><ul><li>found = false ; </li></ul><ul><li>bool moreToSearch = ( first <= last ) ; </li></ul><ul><li>while (moreToSearch && !found ) </li></ul><ul><li>{ </li></ul><ul><li>midPoint = ( first + last ) / 2 ; // INDEX OF MIDDLE ELEMENT </li></ul><ul><li> if (item.key < info[midPoint].key) </li></ul><ul><li>last = midPoint – 1; </li></ul><ul><li>else if (item.key > info[midPoint].key) </li></ul><ul><li>first = midPoint + 1; </li></ul><ul><li>else </li></ul><ul><li>{ </li></ul><ul><li>found = true; </li></ul><ul><li>item = info[midPoint]; </li></ul><ul><li>} </li></ul><ul><li>moreToSearch = ( first <= last ) ; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Retrieve Item
    27. 27. void SortedType::RetrieveItem ( Item Type& item, bool& found ) // Pre: Key member of item is initialized. // Post: If found, item’s key matches an element’s key in the list // and a copy of that element has been stored in item; otherwise, item is unchanged. { int midPoint ; int first = 0; int last = length - 1 ; found = false ; while ( first <= last && !found ) { midPoint = ( first + last ) / 2 ; // INDEX OF MIDDLE ELEMENT if (item.key < info[midPoint].key) last = midPoint – 1; else if (item.key > info[midPoint].key) first = midPoint + 1; else { found = true; item = info[midPoint]; } } }
    28. 28. <ul><li>void SortedType::ResetList ( ) </li></ul><ul><li>// Pre: List has been initialized. </li></ul><ul><li>// Post: Current position is prior to first element in list. </li></ul><ul><li>{ </li></ul><ul><li>currentPos = -1 ; </li></ul><ul><li>} </li></ul><ul><li>void SortedType::GetNextItem ( ItemType& item ) </li></ul><ul><li>// Pre: List has been initialized. Current position is defined. </li></ul><ul><li>// Element at current position is not last in list. </li></ul><ul><li>// Post: Current position is updated to next position. </li></ul><ul><li>// item is a copy of element at current position. </li></ul><ul><li>{ </li></ul><ul><li>currentPos++ ; </li></ul><ul><li>item = info [currentPos] ; </li></ul><ul><li>} </li></ul>
    29. 29. Order of Magnitude of a Function <ul><li>The order of magnitude, or Big-O notation, of a function expresses the computing time of a problem as the term in a function that increases most rapidly relative to the size of a problem . </li></ul><ul><li>수행시간의 Big-O 표기 : 수행시간에 대한 함수 표기방법으로 문제의 크기와 관련하여 수행시간을 가장 빠르게 증가시키는 함수의 항으로 나타냄 </li></ul><ul><li>O : 함수를 ( 증가속도에 따라 ) 분류하는 방법 </li></ul><ul><li> </li></ul>
    30. 30. Names of Orders of Magnitude <ul><li>O(1) bounded (by a constant) time </li></ul><ul><li>O(log 2 N) logarithmic time </li></ul><ul><li>O(N) linear time </li></ul><ul><li>O(N*log 2 N) N*log 2 N time </li></ul><ul><li>O(N 2 ) quadratic time </li></ul><ul><li> </li></ul><ul><li>O( 2 N ) exponential time </li></ul>
    31. 31. Big-O Comparison of List Operations <ul><li> </li></ul>OPERATION UnsortedList SortedList RetrieveItem O(N) O(N) linear search O(log 2 N) binary search InsertItem Find O(1) O(N) search Put O(1) O(N) moving down Combined O(1) O(N) DeleteItem Find O(N) O(N) search Put O(1) swap O(N) moving up Combined O(N) O(N)
    32. 32. Object-Oriented Design <ul><li>A problem-solving methodology that produces a solution to a problem in terms of self-contained entities called objects </li></ul><ul><li>An object is a thing or entity that makes sense within the context of the problem </li></ul><ul><ul><li>For example, a student </li></ul></ul>
    33. 33. Object-Oriented Design <ul><li>A group of similar objects is described by an object class , or class </li></ul><ul><li>A class contains fields that represent the properties and behaviors of the class </li></ul><ul><ul><li>A field can contain data value(s) and/or </li></ul></ul><ul><ul><li>functions (subprograms) </li></ul></ul><ul><ul><li>A functions is a named algorithm that manipulates the data values in the object </li></ul></ul>

    ×