Title: Searching and Sorting
1 Searching and Sorting
Chapter 14
2Outline
- Search
- Linear Search
- Binary Search
- Sort
- Selection Sort
- Bubble Sort
- Insertion Sort
- Template Functions for Sorting and Searching
3Sorting and Searching
- Sorting Arranging a list of values in order
- Searching Locating a particular item in a list
- Searching a sorted list is faster
- but sorting could be time consuming
- Linear (sequential) search
- used on any list
- Binary search
- used on sorted list only
4Linear Search
- Using a bounded number of tries
- use the variables found and index to control the
search loop
use a mid-loop exit
5Binary Search
- Given a sorted list
- keep narrowing the search by eliminating half of
the remaining list - Example search for the name Gil
- Compare the name Gil to the contents of the
middle cell v5 - Because Gil is alphabetically less than Jan, Gil
(if exits) must be in the left half
If there are even number of cells ? take the
middle cell to be that cell just to the left of
the middle line
6Binary Search
- Compare the name Gil to the contents of the
current middle cell v2 - Because Gil is greater than Fay, Gil (if exits)
must be in the right half
7 Binary Search Function
- We want the binary_search function to return
either the subscript of the cell containing the
desired item or -1 if the item is not in the
vector - use variables first, last, and mid
- the segment still under consideration is changed
to the left half segment by last mid -1or to
the right half segment by first mid 1
8Binary Search Function
- Page 322
- Trace to-find 48
- Trace to-find 82 (not in the Vector)
first mid 1
9Selection Sort
- Idea 1) find the smallest number and swap it
with the number in position 02) from the
remaining numbers, find the next smallest number
and swap it with the number in position 13)
continue this fashion until the last position
10Selection Sort
- for cell 0, we make a pass through v0 .. v6
to locate the smallest number, 12, and swap it
with the number in cell 0
- for cell 1, we make a pass through v1 .. v6
to locate the smallest number, 30, and swap it
with the number in cell 1
11Selection Sort
- A vector with N numbers
- ctf cell to fill
- suppose v0 through vctf-1 are sorted
- we need to pass through subvector vctf ..
vN-1to find the index_min of the smallest
element in this subvector - then swap the contends of vctf and vindex_min
12Selection Sort
- outer loop vctf..vN-2
- inner loop vctf1..vN-1
- index_min is initialized with ctf
- how many passes? N-1
13Bubble Sort
- Idea 1) During each pass of bubble sort, the
contents of successive pairs of adjacent cells
are compared and, if out of order, switched2)
bubble sort will correctly fill cells, starting
at the right end with the last cell
Pass 1
Comparison Result Action v0 gt v1 true
switchv1 gt v2 false none v2 gt
v3 true switch
After Pass 1 the largest number is in v3
14Bubble Sort
Pass 2
Comparison Result Action v0 gt v1 false
nonev1 gt v2 true switch
After Pass 2 the two largest numbers are in
v2, v3
Pass 3
Comparison Result Action v0 gt v1 true
switch
After Pass 3 the vector, v0 .. vsize-1, is
sorted
15Bubble Sort function
- The smaller numbers bubble to the top, while
larger ones sink into place, one by one, at the
bottom.
- Version 1 of the bubble sort
- for (pass 1 pass lt size, pass)
-
- ctf size pass
- for (i0 iltctf i)
-
- if (vi gt vi1
- swap(vi, vi1)
-
-
16Bubble Sort function - Version 2
- As pass goes from 1 to size ? cft goes from
size-1 down to 0 - If no swap occurs during a pass ? the vector is
already sorted
for (ctf size-1 ctf gt 0, ctf--)
swaps 0 for (i0 iltctf i)
if (vi gt vi1 swap(vi,
vi1) swaps if
(swaps0) return
17Bubble Sort function - Version 2
18Inserting into a sorted vector
- insert function inserts one new entry into a
sorted vector so that the new vector is also
sorted - use a for loop
- 1) after the loop, the value of i will be the
index of the cell immediately to the left of the
cell into which the new entry is to be inserted - 2) the loop shifts some elements to the right
to make room for the new entry.
19Inserting into a sorted vector
e.g. entry 55
20Insertion Sort
- Idea making repeated calls to the insert function