Selection Sort 3 slides - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Selection Sort 3 slides

Description:

Entire expression is called the 'Big-O' measure for the algorithm. ** Big-O notation provides a machine independent means for determining the ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 30
Provided by: alaneu
Category:
Tags: big | selection | slides | sort

less

Transcript and Presenter's Notes

Title: Selection Sort 3 slides


1
Lecture 3 (Chapter 3) Introduction to Algorithms
Selection Sort (3 slides) Selection Sort Alg.
(3 slides) Search Algorithms (6
slides) Illustrating the Binary
Search -Successful (3 slides) -Unsuccessful (3
slides) Binary Search Alg. (3 slides) Big-O
Notation Constant Time Algorithms Linear Time
Algorithms Exponential Algs. (2
slides) Logarithmic Time Algorithms
Selection Sort Algorithm -Integer
Version -String Version Template Syntax (4
slides) Summary Slides (4 slides)
2
Selection Sort- 5 Element Array
  • Pass 0
  • Scan the entire list from arr0 to arr4 and
    identify 20 at index 1 as the smallest element.
  • Exchange 20 with arr0 50, the first element
    in the list.

3
Selection Sort- 5 Element Array
  • Pass 1
  • Scan the sublist 50, 40, 75, and 35.
  • Exchange the smallest element 35 at index 4 with
    arr1 50.

4
Selection Sort- 5 Element Array
  • Pass 2
  • Locate the smallest element in the sublist 40,
    75, and 50.

5
Selection Sort- 5 Element Array
  • Pass 3
  • Two elements remain to be sorted.
  • Scan the sublist 75, 50 and exchange the smaller
    element with arr3.
  • The exchange places 50 at index 4 in arr3.

6
Selection Sort- 5 Element Array
7
Selection Sort Algorithm
void selectionSort(int arr, int n)   for
(int pass 0 pass lt n-1 pass) // scan
unsorted sublist to find smallest value int
smallIndex pass for (int j pass1 j lt
n j) if (arrj lt arrsmallIndex)
smallIndex j // swap smallest value
with leftmost if (smallIndex ! pass)
int temp arrpass arrpass
arrsmallIndex arrsmallIndex temp

8
Search Algorithms
  • Search algorithms start with a target value and
    employ some strategy to visit the elements
    looking for a match.
  • If target is found, the index of the matching
    element becomes the return value.

9
Search Algorithms
10
Search Algorithms- Sequential Search Algorithm
int seqSearch (const int arr, int first,
int last, int target) // scan indices
in range first lt I lt last // test for a match
or index out of range. int i first
while(i ! last arri ! target) i
return i // i is index of match or i last if
no match
11
Binary Search Algorithm
Case 1. A match occurs. The search is complete
and mid is the index that locates the target.
if (midValue target) // found
match return mid
12
Binary Search Algorithm
Case 2. The value of target is less than
midvalue and the search must continue in the
lower sublist. Reposition the index last to the
end of the sublist (last mid). // search the
lower sublist if (target lt midvalue) ltr
eposition last to midgt ltsearch sublist
arrfirstarrmid-1
13
Binary Search Algorithm
Case 3. The value of target is greater than
midvalue and the search must continue in the
upper sublist . Reposition the index first to
the front of the sublist (first mid1). //
search upper sublist if (target gt midvalue)
ltreposition first to mid1gt ltsearch sublist
arrmid1arrlast-1gt
14
Illustrating the Binary Search- Successful Search
1. Search for target 23 Step 1 Indices first
0, last 9, mid (09)/2 4.
Since target 23 gt midvalue 12, step 2
searches the upper sublist with first 5 and
last 9.
15
Illustrating the Binary Search- Successful Search
Step 2 Indices first 5, last 9, mid
(59)/2 7.
Since target 23 lt midvalue 33, step 3
searches the lower sublist with first 5 and
last 7.
16
Illustrating the Binary Search- Successful Search
Step 3 Indices first 5, last 7, mid
(57)/2 6.
Since target midvalue 23, a match is found at
index mid 6.
17
Illustrating the Binary Search- Unsuccessful
Search
Search for target 4. Step 1 Indices first
0, last 9, mid (09)/2 4.
Since target 4 lt midvalue 12, step 2 searches
the lower sublist with first 0 and last 4.
18
Illustrating the Binary Search- Unsuccessful
Search
Step 2 Indices first 0, last 4, mid
(04)/2 2.
Since target 4 lt midvalue 5, step 3 searches
the lower sublist with first 0 and last 2.
19
Illustrating the Binary Search- Unsuccessful
Search
Step 3 Indices first 0, last 2, mid
(02)/2 1.
Since target 4 gt midvalue 3, step 4 should
search the upper sublist with first 2 and last
2. However, since first gt last, the target is
not in the list and we return index last 9.
20
Binary Search Algorithm
Int binSearch (const int arr, int first, int
last, int target) int origLast
last // original value of last while
(first lt last) int mid (firstlast)/2
if (target arrmid) return mid //
we have a match so return mid else if (target
lt arrmid) last mid // search lower
sublist else first mid1 // search
upper sublist return origLast // target
not found
21
Big-O notation
Big-O notation provides a machine independent



means for
determining the efficiency of an Algorithm.
For the selection sort, the number of comparisons
is T(n) n2/2 - n/2.
n 100 T(100) 1002/2 -100/2 10000/2 -
100/2 5,000 - 50 4,950
22
Constant Time Algorithms
An algorithm is O(1) when its running time is
independent of the number of data items. The
algorithm runs in constant time.
The storing of the element involves a simple
assignment statement and thus has efficiency
O(1).
23
Linear Time Algorithms
An algorithm is O(n) when its running time is
proportional to the size of the list. When the
number of elements doubles, the number of
operations doubles.
24
Less Efficient Algorithms
  • Quadratic Algorithms O(n2)
  • practical only for relatively small values of n.
    When n doubles, running time quadruples.
  • Cubic Algorithms O(n3)
  • efficiency is generally poor doubling the size
    of n increases the running time eight-fold.
  • Exponential O(2n)
  • Awful, but some algorithms have no better
    algorithmic solution

25
Algorithm Efficiency
26
Logarithmic Time Algorithms
The logarithm of n, base 2, is commonly used when
analyzing computer algorithms. Ex. log2(2)
1 log2(75) 6.2288 When compared to the
functions n and n2, the function log2 n grows
very slowly.
27
Summary Slide 1
- The simplest form of searching is the
sequential search. - It
compares the target with every element in a list
until matching the target or reaching
the end of the list. - If the list is in sorted
order, the binary search algorithm is more
efficient. - It exploits the
structure of an ordered list to produce very
fast search times.
28
Summary Slide 2
- Big-O notation measures the efficiency of an
algorithm by estimating the number of
certain operations that the algorithm must
perform. - For searching and sorting
algorithms, the operation is data
comparison. - Big-O measure is very useful for
selecting among competing algorithms.
29
Summary Slide 3
- The running time of the sequential search is
O(n) for the worst and the average
cases. - The worst and average case for the
binary search is O(log2n). - Timing data
obtained from a program provides experimental
evidence to support the greater efficiency
of the binary search.
Write a Comment
User Comments (0)
About PowerShow.com