Selection Sort and Quick Sort - PowerPoint PPT Presentation

About This Presentation
Title:

Selection Sort and Quick Sort

Description:

Selection Sort and Quick Sort Instructor: Mainak Chaudhuri mainakc_at_cse.iitk.ac.in Selection sort class SelectionSort { public static void main (String arg ... – PowerPoint PPT presentation

Number of Views:286
Avg rating:3.0/5.0
Slides: 12
Provided by: iitkAcIn81
Category:
Tags: merge | quick | selection | sort

less

Transcript and Presenter's Notes

Title: Selection Sort and Quick Sort


1
Selection Sort and Quick Sort
  • Instructor Mainak Chaudhuri
  • mainakc_at_cse.iitk.ac.in

2
Selection sort
  • class SelectionSort
  • public static void main (String arg)
  • int size 20
  • double array new doublesize
  • Initialize (array, size) // not shown
  • PrintArray (array, size) // not shown
  • Sort (array, size)
  • PrintArray (array, size) // not shown

3
Selection sort
  • public static void Sort (double array, int
    size)
  • int i, j, minIndex
  • for (i0iltsize-2i)
  • minIndex i
  • for (ji1jltsizej)
  • if (arrayj lt arrayminIndex)
  • minIndex j
  • Swap (array, minIndex, i)
  • // Invariant array0 to arrayi is
    sorted

4
Selection sort
  • public static void Swap (double array, int
    p, int q)
  • double temp
  • temp arrayp
  • arrayp arrayq
  • arrayq temp
  • // end class (How many comparisons?)

5
Selection sort
  • Better than bubble sort by a constant factor
  • Less number of assignments
  • Asymptotically both are O(n2)

6
Quick sort
  • Pick a pivot element and put it in its place
  • All elements to its left are smaller and all to
    its right are bigger
  • Recursively sort the left half and the right half
  • Best case is O(nlogn), same as merge sort
  • Worst case is O(n2)
  • Recall that the worst case time for merge sort is
    O(nlogn)

7
Quick sort
  • class QuickSort
  • public static void main (String arg)
  • int n 20
  • double array new doublen
  • Initialize (array, n) // not shown
  • Sort (array, 0, n-1)
  • // continued on next slide

8
Quick sort
  • public static void Sort (double array, int
    start, int end)
  • int pivot
  • if (start lt end)
  • if (start end-1)
  • if (arraystart gt arrayend)
  • Swap (array, start, end)
  • else
  • pivot Partition (array, start,
    end)
  • Sort (array, start, pivot-1) //
    left half
  • Sort (array, pivot1, end) //
    right half
  • // continued in next slide

9
Quick sort
  • public static int Partition (double array,
    int start, int end)
  • int pivot start
  • int downstream start1
  • int upstream end
  • while (true)
  • while ((downstream lt end)
    (arraydownstream lt arraypivot))
  • downstream
  • // continued in next slide

10
Quick sort
  • while ((upstream gt start)
    (arrayupstream gt arraypivot))
  • upstream--
  • if (downstream lt upstream)
  • Swap(array, downstream, upstream)
  • else
  • break
  • Swap (array, pivot, upstream)
  • return upstream
  • // continued in next slide

11
Quick sort
  • public static void Swap (double array, int
    p, int q)
  • double temp
  • temp arrayp
  • arrayp arrayq
  • arrayq temp
  • // end class
Write a Comment
User Comments (0)
About PowerShow.com