Sorting Algorithms - PowerPoint PPT Presentation

About This Presentation
Title:

Sorting Algorithms

Description:

Radix-sort - repeatedly apply Bucket-sort digit-by-digit ... Radix-sort - nearly finished code. Only for students having problem with Java programming! ... – PowerPoint PPT presentation

Number of Views:46
Avg rating:3.0/5.0
Slides: 15
Provided by: rong94
Category:

less

Transcript and Presenter's Notes

Title: Sorting Algorithms


1
Sorting Algorithms
  • O(n2) algorithms
  • O(n log n) algorithms
  • Something even better?

2
Sorting a frequently used process
  • Nature tends towards disorder
  • Human prefer order
  • e.g.
  • address books
  • shopping lists
  • databases

3
Insertion sort O(n2)
  • Commonly used for hand-sorting
  • Use two lists unsorted and sorted
  • unsorted input list (size n)
  • sorted an empty list
  • loop from i 0 to n-1 do
    n loops
  • sorted.insertInOrder(unsortedi)
    o(n)
  • You have just implemented insertInOrder()

4
Bubble sort O(n2)
  • Imagine an unsorted list held vertically
  • Smaller values are light and bubble up
  • void bubbleSort()
  • int i,j int lastcurrent_size-1
  • for(i0iltlast i)
  • fot(jlastjgtIj--)
  • if(dataj is less than dataj-1)
    swap(j, j-1)

5
Quicksort the first O(n log n) algorithm (1962)
pivot
  • Using divide-and-conquer technique
  • Select a pivot, split list into 2 sublists
    smaller and larger
  • Repeat this to all sublists until sublists size
    is reduced to 1

5 2 1 9 3 8 7
2 1 3 5 9 8 7
1 2 3 5 8 7 9
1 2 3 5 7 8 9
6
Quicksort average O(n log n) the worst
case O(n2)
Time per level
  • quicksort(int fm, int to)
  • int p // pivot position
  • if(fm lt to)
  • p partition(fm,to)
  • quicksort(fm, p-1)
  • quicksort(p1,to)

5 2 1 9 3 8 7
O(n)
2 1 3 5 9 8 7
O(n)
1 2 3 5 8 7 9
O(n)
1 2 3 5 7 8 9
O(n)
O(n log n)
Total
7
Merge sort - O(n log n)(ref. last weeks lecture)
  • Why O(n log n)?
  • Merging 2 sorted lists takes O(n1n2), n1 and n2
    are sizes of these 2 lists
  • There are log n levels of merging, each level
    takes O(n)

17 24 31 45 50 63 85 96
8
17 31 90 96
24 45 63 85
8
4
4
24 85
45 63
17 31
90 96
8
2
2
2
2
85 24 63 45 17 31 96 90
8
Can we have an O(n) sorting algorithm?Bucket-sort
  • Yes, if we know the range of sorted items.
  • Let the range be 1,k,
  • If(kltO(n)), we can use extra momey
  • to make k buckets. Then put each
  • item into the correct bucket.
  • We can do sorting without comparisons!
  • Why O(n)? only need go through n items once

A bag of coins
Total n
8
2
2
3
4
1
5
6
7
2p
5p
10p
20p
50p
1p
1
9
Radix-sort - repeatedly apply Bucket-sort
digit-by-digit
  • An example assume that we know sorted items are
    integers at most 3 digits (i.e. less than 1000)
  • Input list 314,17,802,509,87,352,199,128.

The 1st phase
314
0
802
17
1
352 314
sorted by units
802
2
By 1st digit
3
join them
17
509
4
87
87
352
5
128
199
6
509
128
119
7
8
Input list
10 buckets
9
10
Radix-sort cont.
  • The 2nd phase, sorted by 2nd digit (tens).
  • list from last phase 802,352,314,17,87,128,509,1
    19.

0
802
802
1
352
509
2
314
314
By 2nd digit
3
join them
17
17
4
87
119
5
128
128
6
509
352
7
119
87
8
list from last phase
10 buckets
9
11
Radix-sort cont.
  • The last phase, sorted by 3rd digit.
  • list from last phase 802,509,314,17,119,128,352,
    87.

0
802
17
1
509
87
2
314
119
By 3rd digit
3
join them
17
128
4
119
314
5
128
352
6
352
509
7
87
802
8
list from last phase
sorted!
9
12
Radix-sort - nearly finished codeOnly for
students having problem with Java programming!
  • void radixSort()
  • int k,j int b0,b1,b2,b3,b4,b5,b6,b7,b8,
    b9 // need 10 counters for 10 buckets
  • // declare 10 buckets, B0, B2, , B9
  • String B0 new Stringlimit
  • // add other 9 more declarations
  • for(k1 klt5 k) // loop for digits.
    (we know that there are not more than 5 digits)
  • b0b1b2b3b4b5b6b7b8b90 //
    set all counter to 0
  • for(j0 jltcurrent_size j) //
    loop for all items in the list, put them into
    correct buckets
  • char c Func.getNthDigit(dataj,
    k)
  • switch (c)
  • case '0' B0b0dataj
    break
  • ......
  • case '9' B9b9dataj
    break
  • // join the buckets
  • j 0
  • for(k0 kltb0 k) dataj B0k
  • ....

13
Radix-sort time complexity?
  • O(nk) where n is the length of the given list, k
    is the maximum number of digits used in the list
  • To satisfy O(nk) lt O(n log(n)), we need k lt
    log(n)
  • Bucket-sort or Radix-sort trade off between
    space and time

14

divide- conquer
sorting
timing
insertion
deletion

L
recursion
iteration
binary search tree
TREE
STACK
QUEUE
LIST
LIST
Time complexity
Write a Comment
User Comments (0)
About PowerShow.com