Sorting: Advanced Techniques - PowerPoint PPT Presentation

1 / 24
About This Presentation
Title:

Sorting: Advanced Techniques

Description:

IKI 10100: Data Structures & Algorithms. Ruli Manurung (acknowledgments to Denny & Ade Azurat) ... Recursively sort the first and second half separately. ... – PowerPoint PPT presentation

Number of Views:32
Avg rating:3.0/5.0
Slides: 25
Provided by: csU82
Category:

less

Transcript and Presenter's Notes

Title: Sorting: Advanced Techniques


1
Sorting Advanced Techniques
2
Outline
  • Divide-and-conquer sorting algorithms
  • Mergesort
  • Quicksort
  • For each algorithm
  • Idea
  • Example
  • Implementation
  • Running time for each algorithm

3
Mergesort Basic Idea
  • Divide and Conquer approach
  • Idea
  • Merging two sorted array takes O(n) time
  • Split an array into two takes O(1) time

4
Mergesort Merge Implementation
  • Implement operation to merge two sorted arrays
    into one sorted array
  • public static void merge(int A, int B, int
    C)

Assume A and B are sorted and C A B
5
Mergesort Algorithm
  • If the number of items to sort is 0 or 1, return.
  • Recursively sort the first and second half
    separately.
  • Merge the two sorted halves into a sorted group.

6
Mergesort Example
split
7
Mergesort Example
split
merge
8
Mergesort Example
merge
9
Mergesort Implementation
  • MergeSort implementation (and driver method)
  • void mergeSort(int array)
  • mergeSort(array, 0, a.length-1)
  • void mergeSort(int a, int left, int right)
  • if(left lt right)
  • int centre (left right)/2
  • mergeSort(a, left, centre)
  • mergeSort(a, center1, right)
  • merge(a, left, center1, right)

How to merge the two subarrays of A without any
temporary space?
10
Mergesort Merge Implementation
  • Implement operation to merge two sorted
    subarrays
  • public static void merge(int A, int l, int c,
    int r)

11
Mergesort Analysis
  • Running Time O(n log n)
  • Why?

12
Quicksort Basic Idea
  • Divide and Conquer approach
  • Quicksort(S) algorithm
  • If the number of items in S is 0 or 1, return.
  • Pick any element v ? S. This element is called
    the pivot.
  • Partition S v into two disjoint groups
  • L x ? S v x ? v and
  • R x ? S v x ? v
  • Return the result of Quicksort(L), followed by v,
    followed by Quicksort(R).

13
Quicksort Select Pivot
-1
58
4
2
42
3
43
40
0
1
3
65
14
Quicksort Partition
65
2
42
0
3
40
4
43
-1
58
3
1
15
Quicksort Recursive Sort Merge
43
65
58
42
2
1
3
0
-1
3
4
16
Quicksort Partition Algorithm 1
17
Quicksort Partition Algorithm 1
18
Quicksort Partition Algorithm 1
19
Quicksort Partition Algorithm 2
move pivot out of the way
while gt pivot right--
while lt pivot left
20
Quicksort Partition Algorithm 2
CROSSING
move pivot back
Quicksort recursively
Quicksort recursively
21
Quicksort Implementation
  • static void QuickSort(int a, int low, int high)
  • if(high lt low) return // base case
  • pivot choosePivot(a) // select best
    pivot
  • int ilow, jhigh-1
  • swap(a,pivot,aj) // move pivot out of
    the way
  • while(i lt j)
  • // find large element starting from left
  • while(ilthigh ailtpivot) i
  • // find small element starting from right
  • while(jgtlow ajgtpivot) j--
  • // if the indexes have not crossed, swap
  • if(igtj) swap(a, i, j)
  • swap(a,i,high-1) // restore pivot

22
Quicksort Analysis
  • Partitioning takes
  • O(n)
  • Merging takes
  • O(1)
  • So, for each recursive call, the algorithm takes
    O(n)
  • How many recursive calls does a quick sort need?

23
Quicksort Choosing The Pivot
  • Ideal pivot
  • Median element
  • Common pivot
  • First element
  • Element at the middle
  • Median of three

24
Further Reading
  • http//telaga.cs.ui.ac.id/WebKuliah/IKI10100/resou
    rces/animation/
  • Chapter 8 Sorting Algorithm
Write a Comment
User Comments (0)
About PowerShow.com