Title: CS 1400
1CS 1400
- Chapter 8 Searching and Sorting
2Linear Search
int LinearSearch (int list, int size, int
value) bool found false int position
-1 for (int n0 nltsize !found n)
if (listn value) position
n found true
return position
3Linear Search
- Advantages
- easy to write
- easy to understand
- doesnt require array elements to be in any
particular order - Disadvantages
- very inefficient for large arrays
- (average search requires N/2 array comparisons)
4Is there a better algorithm for searching?
- When array elements are in order, a Binary Search
can be performed - How do you search a phonebook for a given
persons listing?
5Binary Search Pseudocode(assumes search item is
in array)
found false position -1 while search_item has
not yet been found set middle to halfway
between the first and last index if
arraymiddle equals search_item item is at
middle position!! else if arraymiddle is less
than search_item set first index to
middle1 else set last index to middle-1 end
if end while
6Binary Search function (assumes search item is
in array)
int BinarySearch (int array , int first, int
last, int search_item) int middle while
(true) middle (first last) / 2 if
(arraymiddle search_item) return
middle else if (arraymiddle lt
search_item) first middle1 else last
middle-1
7Binary Search example
0 1 2 3 4 5 6 7 8 9
int eaglenums10 float grades10
8What would be output?
- cout ltlt BinarySearch(eaglenums, 0, 9, 5122)
- cout ltlt BinarySearch(eaglenums, 0, 9, 1456)
- cout ltlt BinarySearch(eaglenums, 0, 9, 4556)
- int position BinarySearch(eaglenums, 0, 9,
4556) - cout ltlt gradesposition
- cout ltlt BinarySearch(grades, 0, 9, 93.1) //????
9Binary Search (1st refinement)
Lets move the return to the end of the
function int BinarySearch (int array , int
first, int last, int search_item) bool found
false int position -1, middle while
(!found) middle (first last) / 2 if
(arraymiddle search_item) position
middle found true else if
(arraymiddle lt search_item) first
middle1 else last middle-1 return
position
10Binary Search (2nd refinement)
Lets return -1 if the search_item isnt
found int BinarySearch (int array , int
first, int last, int search_item) bool found
false int position -1, middle while
(!found first lt last) middle (first
last) / 2 if (arraymiddle
search_item) position middle found
true else if (arraymiddle lt
search_item) first middle1 else last
middle-1 return position
11Binary Search (3rd refinement)
Lets simplify the parameters int BinarySearch
(int array , int size, int search_item) int
first 0, last size-1 bool found
false int position -1, middle while
(!found first lt last) middle (first
last) / 2 if (arraymiddle
search_item) position middle found
true else if (arraymiddle lt
search_item) first middle1 else last
middle-1 return position
12Binary Search
- Advantages
- very efficient
- (worst-cast search requires log2(N) array
comparisons) - Disadvantages
- a bit more difficult to write and understand
- requires the array to be ordered!
13An overloaded BinarySearch()
int BinarySearch (char array 30, int size,
char search_item ) int first 0, last
size-1 bool found false int position
-1, middle while (!found first lt
last) middle (first last) / 2 if
(strcmp(arraymiddle, search_item)
0) position middle found true else
if (strcmp(arraymiddle, search_item) lt
0) first middle1 else last middle-1
return position
14So, how does an array get ordered?
- void BubbleSort (int array , int size)
- void BubbleSort (float array , int size)
- void BubbleSort (char array 30, int size)
15Bubble sorting
- Pseudocode
-
- Repeatedly compare adjacent cells until
array is in order - if they are out of order, swap them
161st Refinement
- an array is in order when no further swaps can be
made -
- Set done to false
- While (not done)
- Set done to true
- Compare all adjacent cell pairs
- if they are out of order,
- a) swap them and
- b) set done to false
- end while
172nd Refinement
bool done false while (!done) done
true Compare all adjacent cell pairs
if they are out of order, a) swap
them and b) set done to false
183rd Refinement
- If there are N cells in an array, there are N-1
adjacent pairs
bool done false while (!done) done
true for (int n0 nltN-1 n) if
(arrayn gt arrayn1) Swap (array,
n, n1) done false
19As a function
void BubbleSort (int array , int size)
bool done false while (!done)
done true for (int n0
nltsize-1 n) if (arrayn gt
arrayn1) Swap (array, n,
n1) done false
20Swap()
void Swap (int array , int j, int k) int
temp temp arrayj arrayj
arrayk arrayk temp
21slight improvement
void BubbleSort (int array , int size)
bool done false while (!done)
done true for (int n0
nltsize-1-n n) if (arrayn gt
arrayn1) Swap (array, n,
n1) done false
Each pass leaves the bottom value in the correct
position
22Selection Sort
- Advantages
- somewhat more efficient than a Bubble Sort for
large arrays - Disadvantages
- slightly more difficult to program and understand
23Selection Sort pseudocode
- For (int n0 nltN-1 n)
- find the smallest value beginning at position n
- swap this value with element n
24n0
11
n1
22
smallest
33
smallest
77
Second Pass
First Pass
25n2
33
n3
44
smallest
44
smallest
88
Third Pass
Forth Pass
26n4
smallest
n5
66
88
smallest
Fifth Pass
Sixth Pass
27Since there are 8 elements in this array, only
8-1 passes are required.
n6
smallest
Seventh Pass
28Selection Sort
void SelectionSort (int array , int size)
int smallest_position for (int n0
nltsize-1 n) // find the smallest in
array beginning at position n smallest_position
FindSmallest (array, n, size) // swap the
elements at positions n and smallest_position
Swap (array, n, smallest_position)
29This is the same swapping function used for
BubbleSort()
void Swap (int array , int j, int k) int
temp temp arrayj arrayj
arrayk arrayk temp
30FindSmallest()
int FindSmallest (int array , int start, int
size) int position start int
smallest arraystart // first assumption
for (int kstart1 kltsize k) if (arrayk lt
smallest) smallest arrayk // change
your mind position k // and remember
pos. return position
31Sorting parallel arrays
- Consider parallel arrays for student eagle
numbers and grades - int eagles100
- float grades100
eagles grades
32Anytime we move an eagle number, we need to move
the corresponding grade along with it so that
matching eagle numbers and grades are always in
the same row.
eagles grades
matching swap
swap..
33SelectionSort with parallel arrays
void SelectionSort (int array , float array2
, int size) int smallest_position for
(int n0 nltsize-1 n) smallest_position
FindSmallest (array1, n, size) Swap (array1,
n, smallest_position) Swap (array2, n,
smallest_position)
Naturally, Swap() would need to be overloaded to
also handle float arrays