Sorting - PowerPoint PPT Presentation

About This Presentation
Title:

Sorting

Description:

... one, move it up by one and insert the second-last element in the last position. ... last element up by one, and insert the third-last element into the ... – PowerPoint PPT presentation

Number of Views:42
Avg rating:3.0/5.0
Slides: 14
Provided by: University482
Learn more at: http://pirate.shu.edu
Category:
Tags: sorting

less

Transcript and Presenter's Notes

Title: Sorting


1
Sorting
  • Summary of Sorting Algorithms

2
_Plan
  • General idea of sorting we start with an array
    of items in no particular order, and we want to
    convert it into a sorted array in either
    ascending or descending order.

3
Definition Selection Sort (array of double
numbers)
  • Find the smallest element in the entire array A
    and swap it with the first element--now the first
    element is in the correct position
  • Find the smallest element in A minus the first
    element, and swap it with the second element--now
    the first two elements are in the correct order.
  • Find the smallest element in A disregarding the
    first two elements, and swap it with the third
    element. Now the first three elements are in the
    correct order. Continue in this fashion.
  • This algorithm is illustrated by the following
    figure
  • Figure How Selection Sort works
  • public static boolean search(double A, double
    key)
  • for (int i 0 i lt A.length i)
  • if (Ai key)
  • return true
  • return false

4
Example (Selection)
5
Search which returns the object esired
3
  • 22 is the smallest overall number and is swapped
    with the first number
  • 27 is the smallest number starting at the second
    one, and swapped with itself
  • 45 is the smallest number starting at the third,
    and swapped with the third

6
Definition Bubble Sort (Exchange Sort)
  • Process the full array by comparing consecutive
    elements if the (i1)st element is less than the
    ith element, swap them with each other, otherwise
    leave them alone.
  • If no exchange is made, the array is sorted and
    we are done.
  • At the end of this pass the overall largest
    number will have "bubbled" to the last position
    of the array.
  • Repeat this "bubbling" procedure, but apply it to
    the current array except the last element.

7
Definition Bubble Sort (Exchange Sort)
  • If no exchange was made, the array is sorted and
    we are done. At the end of this pass the
    second-largest number will have "bubbled" to the
    second-last position of the array.
  • Repeat this "bubbling" procedure, but apply it to
    the current array except the last two elements.
    If no exchange was made, the array is sorted and
    we are done.
  • Continue in this fashion until either no exchange
    was necessary during a particular pass, or there
    is nothing left to sort.

8
Example (Bubble)
9
Definition Insertion Sort
  • Consider the second last element in the array. If
    the last element is less than this one, move it
    up by one and insert the second-last element in
    the last position.
  • Consider the third-last element in the array, and
    the last two elements of the array. Move all
    entries in that subarray that are less than the
    third-last element up by one, and insert the
    third-last element into the resulting empty slot.

10
Example (Insertion)
11
Implementation
  • if (userArrayn lt userArraycounter)
  • // Swap the numbers in the array
  • temp userArrayn
  • userArrayn userArraycounter
  • userArraycounter temp

12
Demonstrate
  • Goto SelectBinary.java
  • http//sciris.shu.edu/thinklets--------go to
    COmpSci---------Sorting

13
Code
  • /
  • Author(s) Felicia Escorpizo, Chunkai Szu,
    Rehan Malik
  • Date June 13, 2000
  • Program SelectBinary.java
  • Description Get and sort array according to
    selection sort algorithm and perform binary
    search
  • Pre-condition/Post-Condition Get the values
    from the user to input into the array.
  • Print the original array and sorted
    array.
  • Print the value in the array to search
    for (if selectec).
  • Design Roles Initialize array and fill
    with values
  • Initialize a few counter int values
  • Design Idea
  • 1. Get the array from the user and input into
    an array.
  • 2. Print the original array.
  • 3. Run selection sort algorithm
  • a. Select first value and compare against the
    rest of the values. (switch if need)
Write a Comment
User Comments (0)
About PowerShow.com