Sorting And Searching 
Arrays 
The Binary Search Algorithm
Binary Search Algorithm-Advantages of 
Binary Search 
 Binary search s more efficient with large arrays 
 Binary search’s only requirement is – must be 
sorted in ascending order. 
 Starts in the middle instead of at the beginning 
 The search is over if the desired value is in 
that element. Otherwise,
Binary Search Algorithm 
 Instead of testing the first element in the array, the binary 
search starts with the element in the middle and if that 
element has the desired value, then the search is over 
 If the value in the middle is not the desired element, then it is 
either greater than or less than the value being searched. 
 If it is greater, then the desired value(if it is in the list) will be 
found somewhere in the first half of the array. 
 If it is less, then the desired value will be found somewhere in 
the last half of the array(if it is in the list)
Binary Search Algorithm 
 If the desired value isn't found in the middle element, the 
procedure is repeated for half the array. Example: if the last 
half of the array is to be searched, the algorithm tests its 
middle element. 
 If the value still isn't found in the middle element, the search 
is narrowed to the quarter of the array before or after that 
element. 
 The search continues until a value is either found or there is 
no more elements to test.
Binary Search Algorithm 
 The Binary Search Algorithm uses three variables to mark 
positions within the array: first, last, and middle. 
 The first and last variables mark the boundaries of the portion of 
array currently being searched and are initialized with the 
subscripts of the arrays first and last elements. 
 The subscript halfway between the first and last element is 
calculated and stored in the middle variable. 
 If the middle element of the array does not contain the search 
value, then first or last variables are adjusted so that only the 
top or bottom half of the array is searched during the next 
iteration. This cuts the portion of the array being searched in half 
each time the loop fails to locate the search value.
Binary Search Algorithm-Efficiency 
of a binary search. 
 Each time a binary search fails to find the desired item, it e 
eliminates half of the remaining portion of the array that must be 
searched. 
 Example: if a binary search fails to find an item on the first 
attempt in an array with 1000 elements, the number of elements 
that remains to be searched is 500. if the value is not found on 
the second attempt, the number of elements that remains to be 
searched is 250, and so on. 
 The process continues until the binary search has either located 
the desired item or determined that it is not in the array. 
 With 1000 elements, the binary search takes no more than 10 
comparisons. A sequential search would take an average of 500 
comparisons, so the binary search his much more efficient.
1 Module main() 
2 // Constant for array sizes 
3 Constant Integer SIZE = 6 
4 
5 // Array of instructor names, already sorted in 
6 // ascending order. 
7 Declare String names[SIZE] = "Hall", "Harrison", 
8 "Hoyle", "Kimura", 
9 "Lopez", "Pike" 
10 
11 // Parallel array of instructor phone numbers. 
12 Declare String phones[SIZE] = "555-6783", "555-0199", 
13 "555-9974", "555-2377", 
14 "555-7772", "555-1716" 
15 
16 // Variable to hold the last name to search for. 
17 Declare String searchName 
18 
19 // Variable to hold the subscript of the name. 
20 Declare Integer index 
21
22 // Variable to control the loop. 
23 Declare String again = "Y" 
24 
25 While (again == "Y" OR again == "y") 
26 // Get the name to search for. 
27 Display "Enter a last name to search for." 
28 Input searchName 
29 
30 // Search for the last name. 
31 index = binarySearch(names, searchName, SIZE) 
32 
33 If index ! = -1 Then 
34 // Display the phone number. 
35 Display "The phone number is ", phones[index] 
36 Else 
37 // The name was not found in the array. 
38 Display searchName, " was not found." 
39 End If 
40 
41 // Search again? 
42 Display "Do you want to search again? (Y=Yes, N=No)" 
43 Input again 
44 End While 
45 
46 End Module
47 
48 // The binarySearch function accepts as arguments a String 
49 // array, a value to search the array for, and the size 
50 // of the array. If the value is found in the array, its 
51 // subscript is returned. Otherwise, -1 is returned, 
52 // indicating that the value was not found in the array. 
53 Function Integer binarySearch(String array[], String value, 
54 Integer arraySize) 
55 // Variable to hold the subscript of the first element. 
56 Declare Integer first = 0 
57 
58 // Variable to hold the subscript of the last element. 
59 Declare Integer last = arraySize - 1 
60 
61 // Position of the search value 
62 Declare Integer position = -1 
63 
64 // Flag 
65 Declare Boolean found = False 
66 
67 // Variable to hold the subscript of the midpoint. 
68 Declare Integer middle
69 
70 While (NOT found) AND (first <= last) 
71 // Calculate the midpoint. 
72 Set middle = (first + last) / 2 
73 
74 // See if the value is found at the midpoint... 
75 If array[middle] == value Then 
76 Set found = True 
77 Set position = middle 
78 
79 // Else, if the value is in the lower half... 
80 Else If array[middle] > value Then 
81 Set last = middle - 1 
82 
83 // Else, if the value is in the upper half... 
84 Else 
85 Set first = middle + 1 
86 End If 
87 End While 
88 
89 // Return the position of the item, or -1 
90 // if the item was not found. 
91 Return position 
92 End Function
88 
89 // Return the position of the item, or -1 
90 // if the item was not found. 
91 Return position 
92 End Function 
Program Output (with Input Shown in Bold) 
Enter a last name to search for. 
Lopez [Enter] 
The phone number is 555-7772 
Do you want to search again? (Y=Yes, N=No) 
Y [Enter] 
Enter a last name to search for. 
Harrison [Enter] 
The phone number is 555-0199 
Do you want to search again? (Y=Yes, N=No) 
Y [Enter] 
Enter a last name to search for. 
Lee [Enter] 
Lee was not found. 
Do you want to search again? (Y=Yes, N=No) 
N [Enter]

Sorting and searching arrays binary search algorithm

  • 1.
    Sorting And Searching Arrays The Binary Search Algorithm
  • 2.
    Binary Search Algorithm-Advantagesof Binary Search  Binary search s more efficient with large arrays  Binary search’s only requirement is – must be sorted in ascending order.  Starts in the middle instead of at the beginning  The search is over if the desired value is in that element. Otherwise,
  • 3.
    Binary Search Algorithm  Instead of testing the first element in the array, the binary search starts with the element in the middle and if that element has the desired value, then the search is over  If the value in the middle is not the desired element, then it is either greater than or less than the value being searched.  If it is greater, then the desired value(if it is in the list) will be found somewhere in the first half of the array.  If it is less, then the desired value will be found somewhere in the last half of the array(if it is in the list)
  • 4.
    Binary Search Algorithm  If the desired value isn't found in the middle element, the procedure is repeated for half the array. Example: if the last half of the array is to be searched, the algorithm tests its middle element.  If the value still isn't found in the middle element, the search is narrowed to the quarter of the array before or after that element.  The search continues until a value is either found or there is no more elements to test.
  • 5.
    Binary Search Algorithm  The Binary Search Algorithm uses three variables to mark positions within the array: first, last, and middle.  The first and last variables mark the boundaries of the portion of array currently being searched and are initialized with the subscripts of the arrays first and last elements.  The subscript halfway between the first and last element is calculated and stored in the middle variable.  If the middle element of the array does not contain the search value, then first or last variables are adjusted so that only the top or bottom half of the array is searched during the next iteration. This cuts the portion of the array being searched in half each time the loop fails to locate the search value.
  • 6.
    Binary Search Algorithm-Efficiency of a binary search.  Each time a binary search fails to find the desired item, it e eliminates half of the remaining portion of the array that must be searched.  Example: if a binary search fails to find an item on the first attempt in an array with 1000 elements, the number of elements that remains to be searched is 500. if the value is not found on the second attempt, the number of elements that remains to be searched is 250, and so on.  The process continues until the binary search has either located the desired item or determined that it is not in the array.  With 1000 elements, the binary search takes no more than 10 comparisons. A sequential search would take an average of 500 comparisons, so the binary search his much more efficient.
  • 7.
    1 Module main() 2 // Constant for array sizes 3 Constant Integer SIZE = 6 4 5 // Array of instructor names, already sorted in 6 // ascending order. 7 Declare String names[SIZE] = "Hall", "Harrison", 8 "Hoyle", "Kimura", 9 "Lopez", "Pike" 10 11 // Parallel array of instructor phone numbers. 12 Declare String phones[SIZE] = "555-6783", "555-0199", 13 "555-9974", "555-2377", 14 "555-7772", "555-1716" 15 16 // Variable to hold the last name to search for. 17 Declare String searchName 18 19 // Variable to hold the subscript of the name. 20 Declare Integer index 21
  • 8.
    22 // Variableto control the loop. 23 Declare String again = "Y" 24 25 While (again == "Y" OR again == "y") 26 // Get the name to search for. 27 Display "Enter a last name to search for." 28 Input searchName 29 30 // Search for the last name. 31 index = binarySearch(names, searchName, SIZE) 32 33 If index ! = -1 Then 34 // Display the phone number. 35 Display "The phone number is ", phones[index] 36 Else 37 // The name was not found in the array. 38 Display searchName, " was not found." 39 End If 40 41 // Search again? 42 Display "Do you want to search again? (Y=Yes, N=No)" 43 Input again 44 End While 45 46 End Module
  • 9.
    47 48 //The binarySearch function accepts as arguments a String 49 // array, a value to search the array for, and the size 50 // of the array. If the value is found in the array, its 51 // subscript is returned. Otherwise, -1 is returned, 52 // indicating that the value was not found in the array. 53 Function Integer binarySearch(String array[], String value, 54 Integer arraySize) 55 // Variable to hold the subscript of the first element. 56 Declare Integer first = 0 57 58 // Variable to hold the subscript of the last element. 59 Declare Integer last = arraySize - 1 60 61 // Position of the search value 62 Declare Integer position = -1 63 64 // Flag 65 Declare Boolean found = False 66 67 // Variable to hold the subscript of the midpoint. 68 Declare Integer middle
  • 10.
    69 70 While(NOT found) AND (first <= last) 71 // Calculate the midpoint. 72 Set middle = (first + last) / 2 73 74 // See if the value is found at the midpoint... 75 If array[middle] == value Then 76 Set found = True 77 Set position = middle 78 79 // Else, if the value is in the lower half... 80 Else If array[middle] > value Then 81 Set last = middle - 1 82 83 // Else, if the value is in the upper half... 84 Else 85 Set first = middle + 1 86 End If 87 End While 88 89 // Return the position of the item, or -1 90 // if the item was not found. 91 Return position 92 End Function
  • 11.
    88 89 //Return the position of the item, or -1 90 // if the item was not found. 91 Return position 92 End Function Program Output (with Input Shown in Bold) Enter a last name to search for. Lopez [Enter] The phone number is 555-7772 Do you want to search again? (Y=Yes, N=No) Y [Enter] Enter a last name to search for. Harrison [Enter] The phone number is 555-0199 Do you want to search again? (Y=Yes, N=No) Y [Enter] Enter a last name to search for. Lee [Enter] Lee was not found. Do you want to search again? (Y=Yes, N=No) N [Enter]