Topic 9B Array Sorting and Searching - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Topic 9B Array Sorting and Searching

Description:

At each element, we look to the right and find the lowest value. Once we find the lowest value (to the right of the current element), we swap the ... – PowerPoint PPT presentation

Number of Views:49
Avg rating:3.0/5.0
Slides: 17
Provided by: jeffre78
Category:

less

Transcript and Presenter's Notes

Title: Topic 9B Array Sorting and Searching


1
Topic 9B Array Sorting and Searching
2
Algorithms
  • An algorithm is a set of steps that can be
    followed to solve a problem.
  • Designed and developing an algorithm is often the
    most difficult part of the problem-solving
    process.
  • For each problem, there can be many different
    algorithms that correctly solve the problem.

3
Algorithms
  • Some algorithms may do so faster than others that
    do the same thing (algorithms that solve the same
    problem may vary in efficiency).
  • Some algorithms may be easier to understand and
    may make more sense than others that do the
    same thing (algorithms that solve the same
    problem may vary in complexity).

4
Introduction to Sorting
  • Many of the algorithms that operate on data
    contained within an array require the data in
    that array to be sorted.
  • This means that the data contained in the array
    needs to be in order, from lowest-to-highest, or
    vice versa.
  • We will examine a simple technique for putting
    the array in lowest-to-highest order.
  • Note that once this problem is solved, the
    reverse, putting the array in highest-to-lowest
    order, is trivial.

5
Introduction to Sorting
  • There are many approaches to sorting.
  • Some sorting algorithms are much better than
    others.
  • We will examine one of the simplest sorting
    algorithms, a selection sort.
  • This algorithm is intuitive (easy-to-understand),
    although it is not very efficient.

6
Selection SortThe Basic Idea
  • The basic idea of the selection sort is to
    process the array from left-to-right (index 0,
    index 1, etc)
  • At each element, we look to the right and find
    the lowest value. Once we find the lowest value
    (to the right of the current element), we swap
    the two elements.
  • Then, we move onto the next element and repeat
    the process.

7
Selection SortThe Algorithm
  • (1) current_element 0
  • (2) Find index_of_min, the index of the smallest
    element in the subarray, arraycurrent_element
    to arraysize 1
  • (3) If current_element does not equal
    index_of_min, swap elements at current_element
    and index_of_min
  • (4) If current_element size 1 , stop. Else
    current_element goto step (2)

8
Selection Sort An Example
24
98
4
3
55
62
3
24
4
98
24
98
98
55
98
62
X1
X2
X3
X4
X5
X0
9
Sorting Algorithms
  • Keep in mind that a selection sort is simply one
    method of sorting an array.
  • It is a very simple-to-understand approach.
  • It is also not very efficient.
  • Other sorting algorithms that are much more
    efficient, although much harder-to-understand,
    include the bubble sort and the quicksort. We
    will not discuss these algorithms.

10
Introduction to Searching
  • Searching an array is a closely related problem
    to sorting an array.
  • The simplest approach to array searching is the
    linear search.
  • This searching method consists of examining the
    array elements from left to right.
  • If the target is found, we save the index it was
    found at and stop. If not, and there are still
    more array elements to the right, we move to the
    next element and repeat.

11
The Linear Search
  • Thus, we simply start at index 0 and see if the
    target is there. If so, we stop.
  • If not, we move to index 1 and see if the target
    is there.
  • If not, we move to index 2 and see if the target
    is there.
  • etc

12
Linear Search An Example
We will perform a linear search on the array X in
order to find the target value of 55.
24
98
4
3
55
62
X1
X2
X3
X4
X5
X0
55 is located at index 4!
13
Searching Algorithms
  • The linear search is not a very efficient
    algorithm.
  • Notice that this algorithm does not depend on, or
    require, that the array is sorted, or in any
    order at all.
  • If the array is sorted, we can use a much more
    efficient searching algorithm, the binary search.

14
The Binary Search
  • The binary search is very intuitive.
  • (1) Begin with the entire array
  • (2) Select the middle element.
  • (3) If the target is less than the middle
    element, choose the next subarray to be the half
    of the array to the left (smaller than) the
    middle element. If the target is greater than
    the middle element, choose the next subarray to
    be the half of the array to the right (greater
    than) the middle element.
  • (4) Then, repeat on the new subarray.

15
Binary Search An Example
We will perform a binary search on the array X in
order to find the target value of 55.
3
4
24
25
55
62
98
X1
X2
X3
X4
X5
X0
X6
55 is located at index 4!
16
Summary
  • Searching and sorting arrays are common problems
    in computer science.
  • Both problems, like almost all problems, have
    multiple solutions.
  • Solutions vary on complexity and efficiency.
  • We have examined the selection sort, the linear
    search, and the binary search.
Write a Comment
User Comments (0)
About PowerShow.com