Download presentation
Presentation is loading. Please wait.
1
CHAPTER 9 SORTING & SEARCHING
2
CONTENT Sorting methods Selection Sort Bubble Sort Insertion Sort
Searching method Q & A Class exercise
3
SORTING METHODS Sorting refers to a technique of arranging data in alphabetical order or numeric order using one key field/concatenated fields (primary key). Sorting method is divided into: Internal sort – sorting array (data in memory) External sort – sorting file (data is too large to fit into the memory)
4
INTERNAL SORTING METHODS
There are many different techniques of internal sorting. All techniques requires a comparison of two data items with a possibility of a switch. 3 of them are: Selection sort Bubble sort Insertion sort
5
INTERNAL SORTING METHODS
The choice of best sorting method depends on the how large the array or file is. Ultimately the best one is determined by the number of comparisons and switches that need to take place.
6
SELECTION SORT METHODS
Selection sort performs sorting by repeatedly putting the largest element in the unprocessed portion of the array to the end of the this unprocessed portion until the whole array is sorted. Simplest method (ascending Order)
7
SELECTION SORT METHODS
HOW? Search for the smallest key, exchange with the key in position 1 Then search the second smallest, exchange with key 2 (if key 2 is bigger) Repeat the same process until the last 2 keys are compared and positioned in order.
8
SELECTION SORT METHODS
Example Initial keys STAGE 1 2 3 4 5 6 10 12 8 19 1ST KEY 10 1 4 8 10 6 19 12 1 4 6 10 8 19 12 1 4 6 8 10 19 12 1 4 6 8 10 19 12 1 4 6 8 10 12 19 12 8 LOWEST 1 6 19 4
9
SELECTION SORT METHODS
Algorithm: Set size to numberofelements FOR i = 1 to size - 1 lowindex = i; smallest = array[lowindex]; FOR j = (i + 1) to size IF array[j] < smallest THEN lowindex = j; smallest = array[j]; ENDIF ENDFOR array[lowindex] = array[i]; array[i] = smallest;
10
BUBBLE SORT METHODS Bubble sort is similar to selection sort in the sense that it repeatedly finds the smallest value in the unprocessed portion of the array and put it in its correct position. However, finding the smallest value is not done by selection this time. We "bubbles" up the smallest value instead. From bottom to top, Compare each pair of adjacent elements in the array If the pair is out of order – they switched position, otherwise they remained the same
11
BUBBLE SORT METHODS At the end of first stage - smallest value is bubbled to the first position in the array Next stage works only with the remaining elements. Next smallest value is bubbled to the second position. These will be repeated until the last and second last values are compared
12
BUBBLE SORT METHODS FIRST Stage:
(Sorting in Ascending Order) FIRST Stage: Compare the last two values. If the last value is smaller then exchange them. If not then they remain unchanged. Next compare the second-last and third-last values exchanging them if the second-last is smaller. Continue comparing (and possibly exchanging) the third-last and fourth-last, the fourth-last and fifth-last etc. until you reach the top of the array Note that the smallest value in the array ends up stored in the first position
13
BUBBLE SORT METHODS NEXT PASSES:
Go back to the bottom of the array and repeat the comparing and exchanging values again. The array will become sorted when we get to the point of comparing only the first two values.
14
BUBBLE SORT METHODS Passes 1 2 3 4 5 6 10 12 8 19 10 12 8 1 6 10 12 8
Example : Initial Keys Passes 1 2 3 4 5 6 10 12 8 19 FIRST STAGE 10 12 8 1 6 10 12 8 1 4 6 19 4 6 10 12 8 1 4 6 19 1 8 10 12 4 6 19 1 12 10 8 4 6 19 1 10 12 8 4 6 19 19 4
15
BUBBLE SORT METHODS Initial Stg 1 2 3 4 5 6 10 1 12 8 19
16
BUBBLE SORT METHODS Algorithm: Set size to number of elements
FOR i = 1 to size - 1 FOR j = size – i to 1 step -1 IF (array[j] > array[j+1]) THEN temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; ENDIF ENDFOR
17
BUBBLE SORT METHODS Improved Algorithm: Set size to number of elements
Set i = 1 DO exchangecount = 0 FOR j = size – i to 1 step -1 IF (array[j] > array[j+1]) THEN temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; exchangecount = exchangecount + 1 ENDIF ENDFOR i = i + 1 WHILE exchangecount <> 0
18
INSERTION SORT The array is scanned until an out-of-order element is found The scan then temporarily halted. Backward scan is made to find the correct position to insert the out-of-order element. Then the out-of-order element will be removed from the array
19
INSERTION SORT Elements bypassed during backward scan are moved up one position to make room for the out-of-order element to be inserted. Simple but not efficient
20
INSERTION SORT Example : Initial Keys 1st Pass 2nd Pass 3rd Pass
4th Pass 5th Pass 6th Pass 10 8 1 1 12 12 8 6 6 4 19 19 4 position where the new key is inserted
21
INSERTION SORT It sorts the values seen so far and repeatedly inserts unseen values in the array into the top sorted array. The topmost value can be said to be sorted in relation to itself. Check to see if the second value is smaller than the first one. If it is so, these values swap position. The top array are now relatively sorted. Next we need to insert the third value into the sorted portion
22
INSERTION SORT Remove the third value first.
Slide the second value down to make room for insertion. Insert the third value into the appropriate position. Repeat the process until the last value is inserted.
23
INSERTION SORT Algorithm : Set n to size of elements FOR i = 1 to n
temp = array[i] j = i – 1 WHILE j >= 1 AND array[j] > temp array[j + 1] = array[j] j = j - 1 ENDWHILE array[j+1] = temp ENDFOR
24
Searching Method Sequential search Binary search
In order to have an effective search process, the data set must be sorted into a list There are several methods to search for the existence of a specific key in an array. 2 of the methods are Sequential search Binary search The most easiest is sequential search. Using sequential search, the search value is compared with the array key value, one by one from top of array until a match is found or the search fails. Binary search is the fastest method.
25
Binary Search Method The key to be matched is compared with keys at the extremities of the list to ensure that it does lie in the list Select the median element in the array Compare the value of the median element with the target value. If they match, the search stops
26
Binary Search Method Otherwise (In this case, the upper or lower boundaries will be reset. This will designate the new section of the array) If the target value < median value, the target value must be in the first half of the array If the target value > median value, the target value must be in the other half of the array
27
Binary Search Method If the target value > median value, the lower boundary is set to one more than the median position. If the target value < median value, the upper boundary is set to one less than the median position. Next, sub-divide the half list and select the median value of the half. So it narrows the search area by half at each step until it found a match or the search fails.
28
Binary Search Method When the upper boundary is less than the lower boundary, this indicates that the search fails. If the sub-list contains an even number of keys, the median value is the next lowest key from the center From the example later, Notice that only three comparisons are needed compared with ten in the sequential search
29
Binary Search Method example: Search value = 54 POS INITIAL LIST
SUB-LIST1 SUB-LIST2 1 2 5 3 8 4 14 21 6 25 MEDIAN VAL. 54 > MEDIAN 7 27 39 9 53 53MEDIAN VAL. 10 54 54MEDIAN VAL. 54 = MEDIAN 11 60
30
Binary Search Method Number to search X = 54 STEP 1 Find the median
Working Number to search X = 54 STEP 1 Find the median No. of elements = 11, Top = 1, Bottom = 11 Median = (Top + Bottom)/ 2 = (1 + 11)/2 = 6 = Therefore median is in 6th position, value = 25 STEP 2 Comparison Compare X (54) and median value (25) where 54 > 25, therefore 54 is on the upper part of the array.
31
Binary Search Method Working STEP 3 Find the next median in upper part of the array Top = 7 (the next position after median) Bottom = 11 Median = (7 + 11) /2 = 9 Median value = 53 STEP 4 Comparison Compare X (54) and median value (53) where 54>53, therefore the search key lies in the upper part of the half array.
32
Binary Search Method Working STEP 5 Find the next median in upper part of the array Top = 10 (the next position after median) Bottom = 11 Median = ( ) /2 = 10.5 therefore 10 (the next lowest key from the median) Median value = 54 STEP 6 Comparison Compare X (54) and median value (54) where 54=54, therefore match found. No. of comparisons = 3
33
Binary Search Method Algorithm: SET LAST TO NO. OF ELEMENTS
SET START = 1; FOUND = ‘N’ IF SEARCHVAL >= ARRAY[START] AND SEARCHVAL <= ARRAY[LAST] THEN - DO WHILE FOUND = ‘N’ AND START <= LAST ** find the median value ** MED = INT((START + LAST) / 2 ) ** check for match ** IF ARRAY[MED] = SEARCHVAL THEN FOUND = ‘Y’ ELSE IF ARRAY[MED] > SEARCHVAL THEN ** search value is in the lower part of the array ** LAST = MED - 1 ** search value is in the upper part of the array ** START = MED + 1 ENDIF ENDWHILE 4. ** return result ** IF (FOUND) THEN DISPLAY SEARCH SUCCESS MESSAGE ELSE DISPLAY KEY NOT FOUND ENDIF
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.