Download presentation
Presentation is loading. Please wait.
Published byCharleen Cain Modified over 8 years ago
1
Exam practice
2
Exam 1 Describe how a Boolean expression can control the operation of a loop Describe the difference between a while loop and for loop Explain the dangers of using global variables In terms of function explain what a parameter is Explain the difference between passing parameters by reference and by value Explain the difference between a class and an object 159 Describe how a Boolean expression can control the operation of a loop Describe the difference between a while loop and for loop Explain the dangers of using global variables In terms of function explain what a parameter is Explain the difference between passing parameters by reference and by value Explain the difference between a class and an object 159
3
Search algorithms Linear search: involves methodically searching one location after another until the searched for value is found. It starts at the beginning of a list and checks each item in turn until the desired item is found. E FOUND AT LOCATION 4 WHAT LOCATION IS D DFCAEBHG
4
PSEUDOCODE POINTER=0 WHILE POINTER<LENGTHOFLIST AND LIST[POINTER]!=SEARCHEDFOR ADD ONE TO POINTER ENDWHILE IF POINTER>=LENGTHOFLIST THEN PRINT(“ITEM IS NOT IN THE LIST”) ELSE PRINT(“ITEM IS AT LOCATION “+POINTER) ENDIF POINTER=0 WHILE POINTER<LENGTHOFLIST AND LIST[POINTER]!=SEARCHEDFOR ADD ONE TO POINTER ENDWHILE IF POINTER>=LENGTHOFLIST THEN PRINT(“ITEM IS NOT IN THE LIST”) ELSE PRINT(“ITEM IS AT LOCATION “+POINTER) ENDIF
5
Binary search works by dividing the list in two each time until being searched for is found. For binary search to work the list has to be in order.
6
LOWERBOUND=0 UPPERBOUND=LENGTHOFLIST-1 FOUND=FALSE WHILE FOUND==FALSE AND LOWERBOUND!=UPPERBOUND MIDPOINT=ROUND(LOWERBOUND+UPPERBOUND)/2) IFLIST[MIDPOINT]==SEARCHEDFOR THEN FOUND=TRUE LOWERBOUND=0 UPPERBOUND=LENGTHOFLIST-1 FOUND=FALSE WHILE FOUND==FALSE AND LOWERBOUND!=UPPERBOUND MIDPOINT=ROUND(LOWERBOUND+UPPERBOUND)/2) IFLIST[MIDPOINT]==SEARCHEDFOR THEN FOUND=TRUE
7
Insertion sort: Divide the list into two parts sorted and unsorted and insert it in the correct position in the sorted section. Make the first item the sorted list, the remaining items are the unsorted list. WHILE there are items in the unsorted list Take the first item of the unsorted list. WHILE there is an item to the left of it which is smaller than itself Swap with that item END WHILE The sorted list is now one item bigger END WHILE Make the first item the sorted list, the remaining items are the unsorted list. WHILE there are items in the unsorted list Take the first item of the unsorted list. WHILE there is an item to the left of it which is smaller than itself Swap with that item END WHILE The sorted list is now one item bigger END WHILE
8
CABEFD UNSORTED SORTED ACBEFD UNSORTED SORTED ABCEFD UNSORTED
9
Merge sort: Splits the size n into n lists of size 1. Each of these lists is merged with another. Each of the newly merged lists is merged with another. This is repeated until there is only one, sorted list. The key to this algorithm is the way the lists are merged.
10
Merge sort IF the first item in list1<the first item in list2 THEN Remove the first item from list1 and add it to newlist ELSE Remove the first item from list2 and add it to newlist ENDIF ENDWHILE IF list1 is empty THEN Add the reminder of LIST2 to newlist ELSE ADD the remainder of list1 to newlist ENDIF IF the first item in list1<the first item in list2 THEN Remove the first item from list1 and add it to newlist ELSE Remove the first item from list2 and add it to newlist ENDIF ENDWHILE IF list1 is empty THEN Add the reminder of LIST2 to newlist ELSE ADD the remainder of list1 to newlist ENDIF
11
BACFEDHG BACFE DH G AB CF DEGH AB CF DEGH ABCDEFGH
12
Quick sort: pick an item and call it the pivot, normally the first item is picked. Split the remainder of the list into two sub-lists where list 1 less than or equal to the pivot and list 2 greater than the pivot. Keep applying the split until the pivot can be combined and sorted.
13
Quick sort Place leftPointer at first item in the list and rightPointer at the last item in the list. WHILE leftPOINTER!=rightPOINTER WHILE list[leftPointer] < list[rightPointer] AND leftPointer != rightPointer Add one to leftPointer END WHILE Swap list[leftPointer] with list[[rightPointer] END WHILE Place leftPointer at first item in the list and rightPointer at the last item in the list. WHILE leftPOINTER!=rightPOINTER WHILE list[leftPointer] < list[rightPointer] AND leftPointer != rightPointer Add one to leftPointer END WHILE Swap list[leftPointer] with list[[rightPointer] END WHILE
14
CFDEAB AB CFDE PIVOT ACB D E F D E F ACB ABCDEF SUBLIST
15
Questions In quick sort, a pivot is picked. Explain what this pivot is used for. 149 Describe what is checked during the feasibility study152 Describe what is meant by user documentation In quick sort, a pivot is picked. Explain what this pivot is used for. 149 Describe what is checked during the feasibility study152 Describe what is meant by user documentation
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.