Sorting and Searching - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

Sorting and Searching

Description:

... search algorithm for Bank's member function VerifyAccounts ... return mid; Case 2: target midValue, continue the search in the lower sublist [first, mid) ... – PowerPoint PPT presentation

Number of Views:25
Avg rating:3.0/5.0
Slides: 16
Provided by: yxie
Category:
Tags: bank | first | mid | searching | sorting

less

Transcript and Presenter's Notes

Title: Sorting and Searching


1
Sorting and Searching
  • Dr. Ying Xie

2
  • Selection Sort

3
Recap Bank-ATM problem
  • We want to design some efficiently search
    algorithm for Banks member function
    VerifyAccounts().
  • An efficient search algorithm is build on a
    sorted list.

4
Selection Sorting Algorithm
50
75
20
40
35
void selectionSort(int arr, int n) //for
loop for (int startP 0 startP lt n-2
startP) //1. find the smallest number
in the sublist //2. swap the smallest
number and the first number of this sublist
50
75
20
40
35
pass 1
20
75
50
40
35
pass 2
20
75
35
40
50
pass 3
20
75
35
40
50
pass 4
20
50
35
40
75
stop
5
Find the smallest number is a sublist

75
50
40
35
startP
startP1
startP2
startP3
indexForSmallest
startP
startP1
startP1
startP3
int indexForSmallest startP For (int
jstartP1 jltn-1 j) if (arrj lt
arrindexForSmallest)
indexForSmallest j
6
  • swap the smallest number and the first number in
    the sublist (swap arrstartP and
    arrsubscriptForSmallest)
  • int temp arrstartP
  • arrstarP arrsubscriptForSmallest
  • arrsubscriptForSmallest temp

7
  • void selectionSort(int arr, int n)
  • for (int startP 0 startP lt n-2 startP)
  • //1. find the smallest number in the
    sublist
  • int subscriptForSmallest startP
  • For (int jstartP jltn-1 j)
  • if (arrj lt arrsubscriptForSmalles
    t)
  • subscriptForSmallest j
  • //2. swap the smallest number and the
    first number of this sublist
  • int temp arrstartP
  • arrstarP arrsubscriptForSmallest
  • arrsubscriptForSmallest temp

8
  • Binary Search

9
Binary Search (Only works on a sorted array)
Target midValue
first
Last-1
mid
Last
mid (first last)/2 MidValue arr mid
10
  • Case 1 target midValue, search is done

Target midValue
first
Last-1
mid
Last
if (midValue target) // found match
return mid
11
  • Case 2 target lt midValue, continue the search in
    the lower sublist first, mid)

first
mid-1
last-1
last
New last mid
// search the lower sublist If (target lt
midValue) ltreposition last to midgt ltsearch
sublist arr firstarr mid-1
12
Case 3 target gt midValue, continue the search in
the upper sublist mid 1, last)
last-1
last
first
New first mid 1
If (target gt midValue) first mid
1 Continue the search in first, last)
13
  • Terminate Condition
  • a match is found (return the index of
    that match)
  • the sublist finally become empty (return
    -1)
  • first gt last

14
int binSearch (const int arr , int first, int
last, int target) // while the sublist is
not empty // find the midValue of
sublist and // compare the midValue with
the target // if they are equal
// return mid // else if
target lt midValue // reposition
last to mid, generate the lower sublist
// else // repostion first to
mid 1, generate the upper sublist //
return -1
15
int binSearch (const int arr , int first, int
last, int target) int mid, midValue //
while the sublist is not empty while (first
lt last) // find the midValue of sublist
and mid (firstlast)/2
midValue arrmid // compare the
midValue with the target // if they are
equal, return mid if (target
midValue) return mid //
else if target lt midValue //
reposition last to mid, generate the lower
sublist else if (target lt midValue)
last mid // else
// repostion first to mid 1, generate
the upper sublist else
first mid1 // return -1
Write a Comment
User Comments (0)
About PowerShow.com