Title: Basic Searching Methods Linear Search
1Basic Searching MethodsLinear Search
- Begins search at first item in list, continues
searching sequentially (item by item) through
list, until desired item (key) is found, or until
end of list is reached - Also called sequential or serial search
- If used to look up a name in phone directory
- examine each entry in the order it occurs in
directory, beginning with first entry (say
Aardvark, Aaron) until desired name is found or
until last entry (say Zzxgy, Zora) is reached - Obviously not an efficient method for searching
ordered lists like phone directory (which is
ordered alphabetically) - Advantages
- Algorithm is simple
- List need not be ordered in any particular way
2Basic Searching MethodsLinear Search (contd)
- Algorithm
- (list of n items stored in 1D array)
- Begin search at first array element (make it the
current element) - While (current element does not match the key
AND - there are still array elements left to be
searched) - Make next array element the current element
- EndWhile
- If (all array elements have been searched AND
key is still not found) - Return -1 (to indicate that no value
matching the key was found) - Else
- Return index of array element that matches
the key - EndIf
3Basic Searching MethodsLinear Search (contd)
- Translated into C Function
- int LinearSearch(int list, int size, int key)
-
- int index 0
- while (index lt size-1 listindex ! key)
- index
- if (index size)
- return -1
- else
- return index
4Basic Searching MethodsLinear Search (contd)
- Example Use of Function
- include ltiostream.hgt
- int main()
-
- int LinearSearch(int , int, int)
- int nums5 98, 87, 92, 79, 85, item,
location - cout ltlt "Enter item (integer) you are searching
for " - cin gtgt item
- location LinearSearch(nums, 5, item)
- if (location gt -1)
- cout ltlt "\nItem was found at index location "
- ltlt location ltlt "." ltlt endl
- else
- cout ltlt "\nItem was not found in the list."
ltlt endl - return 0
5Basic Searching MethodsBinary Search
- List must be in sorted order to begin with
- Description (assuming list is sorted in ascending
order) - Compare key with middle entry of list
- for lists with even number of entries, either of
the two middle entries can be used - 3 possibilities for result of comparison
- key matches middle entry ? terminates search with
success - key is greater than middle entry ? matching entry
(if exists) must be in upper part of list (lower
part of list can be discarded from search) - key is less than middle entry ? matching entry
(if exists) must be in lower part of list (upper
part of list can be discarded from search) - Keep applying above 2 steps to the progressively
reduced lists, until match is found or until no
further list reduction can be done (which means
no match can be found)
6Basic Searching MethodsBinary Search (contd)
- Algorithm
- (list of n items stored in 1D array)
- Initialize found flag to FALSE (i.e., match not
yet found) - Initialize lower index to 0
- Initialize upper index to one less than size of
array - While (lower index is lt upper index AND match
is not yet found) - Set midpoint index to integer average of lower
upper index values - If (key matches midpoint element)
- Set found flag to TRUE (i.e., match has
been found) - ElseIf (midpoint element lt key)
- Set lower index to midpoint index plus 1
- Else
- Set upper index to midpoint index less 1
- EndIf
- EndWhile
- If (found flag is FALSE)
- Return -1 (to indicate that no value
matching the key was found) - Else
- Return midpoint index as index of array
element that matches the key
7Basic Searching MethodsBinary Search (contd)
- Translated into C Function
- int BinarySearch(int list, int size, int key)
-
- int low 0, mid, up size - 1, found 0
- while (found 0 low lt up)
-
- mid (low up)/2
- if (listmid key)
- found 1
- else if (listmid lt key)
- low mid 1
- else
- up mid - 1
-
- if (found 1)
- return mid
- else
- return -1
8Basic Searching MethodsBinary Search (contd)
- Example Use of Function
- include ltiostream.hgt
- int main()
-
- int BinarySearch(int , int, int)
- int nums5 2, 10, 45, 98, 101, item,
location - cout ltlt "Enter item you are searching for "
- cin gtgt item
- location BinarySearch(nums, 5, item)
- if (location gt -1)
- cout ltlt "\nItem was found at index location "
- ltlt location ltlt "." ltlt endl
- else
- cout ltlt "\nItem was not found in the list."
ltlt endl - return 0
9Basic Sorting MethodsBubble Sort
- First Level Considerations
- To sort list of n elements in ascending order
- Pass 1 make nth element the largest
- Pass 2, if needed make n-1th element the 2nd
largest - Pass 3, if needed make n-2th element the 3rd
largest -
- Pass n-2, if needed make 3rd n-(n-3)th
element the n-2th largest - Pass n-1, if needed make 2nd n-(n-2)th
element the n-1th largest - Maximum number of passes is (n-1)
- Can it be less?
10Basic Sorting MethodsBubble Sort (contd)
- Second Level Considerations
- Pass 1 Make nth element the largest
- Compare each successive pair of elements,
beginning with 1st, 2nd and ending with
n-1th, nth, and swap the elements if
necessary - Pass 2 (if needed) Make n-1th element the 2nd
largest - Compare each successive pair of elements,
beginning with 1st, 2nd and ending with
n-2th, n-1th, and swap the elements if
necessary - ...
- Pass n-1 (if needed) Make 2nd n-(n-2)th
element the n-1)th largest - Compare each successive pair of elements,
beginning with 1st, 2nd and ending with
n-(n-1)th, n-(n-2)th 1st, 2nd , and
swap the elements if necessary - List is sorted when either of the following
occurs - No swapping involved in any pass
- Pass n-1, the last pass, has been executed
11Basic Sorting MethodsBubble Sort (contd)
- Algorithm
- (list of n items stored in 1D array, sort order
is ascending) - Initialize sorted flag to FALSE (i.e., list not
yet sorted) - Initialize pass to 1
- While (sorted flag is FALSE)
- Set sorted flag to TRUE (list is sorted unless
swapping occurs) - For each i from index of 1st element to
index of n - passth element - If arrayi gt arrayi 1
- Swap arrayi and arrayi 1
- Set sorted flag to FALSE
- EndIf
- EndFor
- Increment pass
- EndWhile
12Basic Sorting MethodsBubble Sort (contd)
- Translated into C Function
- void BubbleSort(int list, int size)
-
- int index, pass 1, sorted 0, hold
- while (sorted 0) // list not sorted yet
-
- sorted 1 // list sorted unless swapping
occurs - for (index 0 index lt size - pass index)
- if (listindex gt listindex 1)
-
- hold listindex
- listindex listindex 1
- listindex 1 hold
- sorted 0
-
- pass
-
13Basic Sorting MethodsBubble Sort (contd)
- Example Use of Function
- include ltiostream.hgt
- int main()
-
- void BubbleSort(int , int)
- int nums5 10, 2, 45, 101, 98
- BubbleSort(nums, 5)
- for (int i 0 i lt 5 i)
- cout ltlt numsi ltlt " "
- return 0
14Basic Sorting MethodsSelection Sort
- First Level Considerations
- To sort list of n elements in ascending order
- Pass 1 make 1st element the smallest
- Pass 2 make 2nd element the 2nd smallest
- Pass 3 make 3rd element the 3rd smallest
-
- Pass n-2 make n-2th element the n-2th
smallest - Pass n-1 make n-1th element the n-1th
smallest - Number of passes is (n-1)
- Can it be less?
15Basic Sorting MethodsSelection Sort (contd)
- Second Level Considerations
- Pass 1 Make 1st element the smallest
- Examine list from 1st to last element, locate
element with smallest value, and swap it with the
1st element where appropriate - Pass 2 Make 2nd element the 2nd smallest
- Examine list from 2nd to last element, locate
element with smallest value, and swap it with the
2nd element where appropriate - ...
- Pass n-1 Make n-1th element the n-1th
smallest - Examine list from n-1th to last element, locate
element with smallest value, and swap it with the
n-1th element where appropriate
16Basic Sorting MethodsSelection Sort (contd)
- Algorithm
- (list of n items stored in 1D array, sort order
is ascending) - For each i from index of 1st element to
index of n-1th element - (Note Pass 1 ? i 0, Pass 2 ? i 1, , Pass
n-1 ? i n-2) - (Re)initialize minimum value to listi
- (Re)initialize index of element with minimum
value (k) to i - For each j from index of i1th element
to index of last element - If (listj lt minimum value)
- Set minimum value to listj
- Set k to j
- EndIf
- EndFor
- If (k ! i)
- Set listk to listi
- Set listi to minimum value
- EndIf
- EndFor
17Basic Sorting MethodsSelection Sort (contd)
- Translated into C Function
- void SelectionSort(int list, int size)
-
- int i, j, k, minValue
- for (i 0 i lt size - 1 i)
-
- minValue listi
- k i
- for (j i 1 j lt size j)
- if (listj lt minValue)
-
- minValue listj
- k j
-
- if (k ! i)
-
- listk listi
- listi minValue
-
18Basic Sorting MethodsSelection Sort (contd)
- Example Use of Function
- include ltiostream.hgt
- int main()
-
- void SelectionSort(int , int)
- int nums5 10, 2, 45, 101, 98
- SelectionSort(nums, 5)
- for (int i 0 i lt 5 i)
- cout ltlt numsi ltlt " "
- return 0
-
19Basic Sorting MethodsInsertion Sort
- First Level Considerations
- To sort list of n items (stored as 1D array) in
ascending order - NOTE 1-element sub-array (1st) is always sorted
- Pass 1 make 2-element sub-array (1st, 2nd)
sorted - Pass 2 make 3-element sub-array (1st, 2nd, 3rd)
sorted - Pass 3 make 4-element sub-array (1st, , 4th)
sorted -
- Pass n-2 make n-1-element sub-array (1st, ,
n-1th) sorted - Pass n-1 make entire n-element array (1st, ,
nth) sorted - Number of passes is (n-1)
- Can it be less?
20Basic Sorting MethodsInsertion Sort (contd)
- Second Level Considerations
- (NOTE For conciseness, C/C conditional
operator syntax has been used) - Pass 1 make 2-element sub-array (1st, 2nd)
sorted - (1st gt 2nd) ? swap(1st, 2nd) done done
- Above is short for If (1st gt 2nd) Then
"swap(1st, 2nd) done" Else "done" - Pass 2 make 3-element sub-array (1st, 2nd, 3rd)
sorted - (2nd gt 3rd) ? save 3rd as tmp move 2nd down 1
done - (1st gt tmp) ? move 1st down 1 put tmp in 1st
done put tmp in 2nd done - Pass 3 make 4-element sub-array (1st, ..., 4th)
sorted - (3rd gt 4th) ? save 4th as tmp move 3rd down 1
done - (2nd gt tmp) ? move 2nd down 1 put tmp in 3rd
done - (1st gt tmp) ? move 1st down 1 put tmp in 1st
done put tmp in 2nd done - ...
21Basic Sorting MethodsInsertion Sort (contd)
- Second Level Considerations (cont'd)
- Pass 3 make 4-element sub-array (1st, ..., 4th)
sorted - (3rd gt 4th) ? save 4th as tmp move 3rd down 1
done - (2nd gt tmp) ? move 2nd down 1 put tmp in 3rd
done - (1st gt tmp) ? move 1st down 1 put tmp in 1st
done put tmp in 2nd done - ...
- Pass n-1 make entire n-element array (1st, ,
nth) sorted - (n-1th gt nth) ? save nth as tmp move
n-1th down 1 done - (n-2th gt tmp) ? move n-2th down 1 put tmp
in n-1th done - (n-3th gt tmp) ? move n-3th down 1 put tmp
in n-2th done -
- (2nd gt tmp) ? move 2nd down 1 put tmp in 3rd
done - (1st gt tmp) ? move 1st down 1 put tmp in 1st
done put tmp in 2nd done
22Basic Sorting MethodsInsertion Sort (contd)
- Algorithm
- (list of n items stored in 1D array, sort order
is ascending) - For each k-element sub-array, k varying from 2 to
n in steps of 1 - Save kth as tmp
- For each j from k-1 to 1 in steps of 1
- If (j 1) // NOTE jth 1st loop
automatically ends hereafter - If (1st gt tmp) "2nd 1st, 1st tmp" Else
"2nd tmp" EndIf - Else
- If (jth gt tmp) "j1th jth" Else
"j1th tmp, end loop" EndIf - EndIf
- EndFor
- EndFor
23Basic Sorting MethodsInsertion Sort (contd)
- Translated into C Function
- void InsertionSort(int list, int size)
-
- int k, j, tmp
- for (k 1 k lt size k)
-
- tmp listk
- for (j k - 1 j gt 0 j--)
- if (j 0)
- if (listj gt tmp)
-
- list1 list0
- list0 tmp
-
- else
- list1 tmp
- else
- if (listj gt tmp)
- listj 1 listj
24Basic Sorting MethodsInsertion Sort (contd)
- Example Use of Function
- include ltiostream.hgt
- int main()
-
- void InsertionSort(int , int)
- int nums5 10, 2, 45, 101, 98
- InsertionSort(nums, 5)
- for (int i 0 i lt 5 i)
- cout ltlt numsi ltlt " "
- return 0