Sorting And Searching - PowerPoint PPT Presentation

About This Presentation
Title:

Sorting And Searching

Description:

... problem of sorting a collection of keys to produce an ordered collection is one ... We will look at an example, an algorithm, analyze the algorithm, and look at ... – PowerPoint PPT presentation

Number of Views:15
Avg rating:3.0/5.0
Slides: 14
Provided by: scienceand1
Learn more at: https://cse.buffalo.edu
Category:

less

Transcript and Presenter's Notes

Title: Sorting And Searching


1
Sorting And Searching
  • CSE116A,B

2
Introduction
  • The problem of sorting a collection of keys to
    produce an ordered collection is one of the
    richest in computer science.
  • The richness derives from the fact that there are
    a number of ways of solving the sorting problem.
  • Sorting is a fascinating operation that provides
    a model for investigating many problems in
    Computer Science. Ex analysis and comparison of
    algorithms, divide and conquer, space and time
    tradeoff, recursion etc.

3
Sorting
  • Comparison based selection, insertion sort (with
    online animations)
  • Divide and conquer merge sort, quick sort (with
    animations from supplements of your text book)
  • Priority Queue /Heap based heap sort
  • Assume Ascending order for all our discussion

4
Selection Sort
  • Select the first smallest element, place it in
    first location (by exchanging contents of
    locations), find the next smallest, place it in
    the next location, and so on.
  • We will look at an example, an algorithm, analyze
    the algorithm, and look at code implementation.

5
Selection Sort Example
2
8
16
10
6
4
2
4
16
10
6
8
2
4
16
10
6
8
Sorted array
2
4
16
8
6
10
2
4
10
8
6
16
6
Selection Sort Pseudo Code
  • Let cursor be pointer to first element of an n
    element list to be sorted.
  • Repeat until cursor points to last but one
    element.
  • 2.1 Search for the smallest element in the list
    starting from the cursor. Let it be at location
    target.
  • 2.2 Exchange elements at cursor and target.
  • 2.3 Update cursor to point to next element.

7
Sort Analysis
  • Let el be the list n be the number of elements
    cursor 0 target 0
  • while (cursor lt n-1)
  • 2.1.1 target cursor
  • 2.1.2 for (i cursor1 i lt n i)
  • if (eli lt eltarget) target
    i
  • 2.2 exchange (eltarget, elcursor) //
    takes 3 assignments
  • 2.3 cursor
  • (n-1) 4 2 (n (n-1) (n-2) ..1)
  • 4n 4 2n(n1)/2 4n 4 n2 n n2 5n -
    4
  • An2 B n C where A 1, B 5, C -4 for
    large n, drop the constant, lower order term in
    n, and the multiplicative constant to get big-O
    notation
  • O(n2) quadratic sort

8
Insertion Sort
Unsorted array
10
Trivially sorted
8
10
Insert 8
6
8
10
Insert 6
2
6
10
8
Insert 2
2
6
16
10
8
Insert 4
Insert 16
9
Insertion Sort Pseudo Code
  • Single element is trivially sorted start with
    first element
  • Repeat for second to nth element of the list
  • 2.1 cursor next location
  • 2.2 Find a location to insert for listcursor
    by comparing
  • and shifting
  • let the location be target
  • 2.3 Insert listtarget listcursor

10
Insertion Sort Analysis
  • cursor 0
  • while (cursor lt n)
  • 2.1 cursor cursor 1
  • 2.2 temp listcursor // save element to
    inserted
  • 2.2.2 j cursor //find location
  • 2.2.3 while (j gt 0 listj-1 gt temp )
  • listj listj-1 //shift
    right
  • j j 1
  • // assert location found or hit left
    end(j0)
  • 2.3 listj temp
  • Worst case O(n2) quadratic
  • Best case linear (when the list is already
    sorted)

11
Merge Sort
  • Divide and Conquer
  • Divide the list into two subsets s1, and s2
  • (recurse) Sort s1 and s2 by divide and conquer
  • (conquer) Merge the sorted s1 and s2.
  • O(n log n) algorithm

12
Merge Sort (s)
  • mergeSort(s)
  • If S.size() gt 1
  • 1. S1, S2 ? partition (S, n/2)
  • 2. mergeSort(s1)
  • 3. mergeSort(s2)
  • 4. S ? merge(s1,s2)
  • Lets look at examples.

13
Example
S1
S2
8
6
10
S11
S12
8
6
S121
S122
Write a Comment
User Comments (0)
About PowerShow.com