0% found this document useful (0 votes)
12 views24 pages

Sorting 2

Uploaded by

Raihana Qader
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views24 pages

Sorting 2

Uploaded by

Raihana Qader
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

SORTING 2

Shell Sort
Merge Sort
Quick Sort
Intuition
• Insertion sort is effective:
• For small datasets
• For data that is nearly sorted
• Insertion sort is inefficient when:
• Elements must move far in array
The Shell Sort
Decreasing sequence of step sizes h
• Every sequence must end at 1
• … , 8, 4, 2, 1
For each h, sort sub-arrays that start at arbitrary element and include every
hth element
• if h = 4
• Sub-array with elements 1, 5, 9, 13 …
• Sub-array with elements 2, 6, 10, 14 …
• Sub-array with elements 3, 7, 11, 15 …
• Sub-array with elements 4, 8, 12, 16 …
Sub-arrays when Increment is 5
Sub-arrays when Increment is 3
First Increment = N/3
= 9/3
=3
Second Increment = First Increment /3
= 3/3
=1
Activity 1
The Merge Sort
Suppose we only know how to merge two sorted sets of elements into one
Merge {1, 5, 9} with {2, 11} -> {1, 2, 5, 9, 11} 
Question
Where do we get the two sorted sets in the first place? 
Idea (use merge to sort n items)
 Merge each pair of elements into sets of 2
 Merge each pair of sets of 2 into sets of 4
Repeat previous step for sets of 4 …
 Final step: merge 2 sets of n/2 elements to obtain a fully sorted set
Divide-and-Conquer Method
• A powerful problem solving technique
Divide-and-conquer method solves problem in the following steps the
following steps
• Divide step
Divide the large problem into smaller problems
Recursively solve the smaller problems
• Conquer step
Combine the results of the smaller problems to produce the result of the
larger problem
Merge Sort
Splitting the List in a Merge Sort¶ Lists as They Are Merged Together
Activity 2
The Quick Sort
• Quick Sort is a divide-and-conquer algorithm
• Divide step
• Choose an item p (known as pivot) and partition the  Choose an item p (known as
pivot) and partition the items of a[i...j] into two parts
•  Items that are smaller than p 
• Items that are greater than or equal to p
• Recursively sort the two parts
pivot
Implementation
Activity 3
Radix sort
8 Radix Sort
– radix is a synonym for base. base 10, base 2
8 Multi pass sorting algorithm that only looks at individual digits
during each pass
8 Use queues as buckets to store elements
8 Create an array of 10 queues
8 Starting with the least significant digit place value in queue that
matches digit
8 empty queues back into array
8 repeat, moving to next least significant digit
Radix Sort in Action: 1s place
8 original values in array
9, 113, 70, 86, 12, 93, 37, 40, 252, 7, 79, 12
8 Look at ones place
9, 113, 70, 86, 12, 93, 37, 40, 252, 7, 79, 12
8 Array of Queues (all empty initially):
0 5
1 6
2 7
3 8
4 9
Radix Sort in Action: 1s
8 original values in array
9, 113, 70, 86, 12, 93, 37, 40, 252, 7, 79, 12
8 Look at ones place
9, 113, 70, 86, 12, 93, 37, 40, 252, 7, 79, 12
8 Queues:
0: 70, 40 5:
1: 6: 86
2: 12, 252, 12 7: 37, 7
3: 113, 93 8:
4: 9: 9, 79
Radix Sort in Action: 10s
8 Empty queues in order from 0 to 9 back into array
70, 40, 12, 252, 12, 113, 93, 86, 37, 7, 9, 79
8 Now look at 10's place
70, 40, 12, 252, 12, 113, 93, 86, 37, 7, 9, 79
8 Queues:
0: 7, 9 5: 252
1:12, 12, 113 6:
2: 7: 70, 79
3: 37 8: 86
4: 40 9: 93
Radix Sort in Action: 100s
8 Empty queues in order from 0 to 9 back into array
7, 9, 12, 12, 113, 37, 40, 252, 70, 79, 86, 93
8 Now look at 100's place
__7, __9, _12, _12, 113, _37, _40, 252, _70, _79, _86, _93
8 Queues:
0: 7, 9, _12, _12, _37, _40, _70, _79, _86, _93 5
1: 113 6:
2: 252 7:
3: 8:
4: 9:
Radix Sort in Action: Final Step
8 Empty queues in order from 0 to 9 back into array
7, 9, 12, 12, 40, 70, 79, 86, 93, 113, 252
Radix Sort Code

You might also like