Title: MIS 215 Module 7
1MIS 215 Module 7 Sorting
2Where are we?
MIS215
Basic Algorithms
Introduction
List Structures
Advanced structures
Search Techniques
Intro to Java, Course
Sorting Techniques
Java lang. basics
Hashtables
Linked Lists
Binary Search
Graphs, Trees
Stacks, Queues
Arrays
Bubblesort
Fast Sorting algos (quicksort, mergesort)
Newbie
Programmers
Developers
Professionals
Designers
3Todays buzzwords
- Bubblesort
- The sorting method in which the big elements
bubble up to the top - Selection sort
- The sorting method where you select the
smallest item to be placed in the appropriate
place - Insertion sort
- The sorting method where an item from the
unsorted part of the array is inserted in the
sorted part - Divide-and-conquer techniques
- Techniques where the problem space is divided,
solved separately, and merged - Typically results in more efficient solutions
- Shellsort
- An advanced version of insertion sort where
elements are inserted into their appropriate
intervals - Mergesort
- The divide-and-conquer sorting technique that
divides the array into two, sorts them separately
and merges them - Partitioning
- The process of dividing an array into two parts
based on the value of a pivot element - Quicksort
- The sorting technique where the array is
partitioned and sorted separately
4Basic Sorting Techniques
- Bubble Sort
- Selection Sort
- Insertion Sort
- Sorting Objects
- Comparing the simple sorts
5Bubble sort what is it?
- The simplest sorting strategy at most 5-6 lines
of code - The large elements bubble up to the end of the
list
6Bubble sort basic strategy
- Start at left end
- Compare two adjacent items, put bigger one in the
right position - any reason why not the smaller one?
- what to do if theyre equal?
- Move to the right, repeat the comparison till you
get to the (right) end - What have you got?
- biggest one at the right end
- Now, start at the left end again, but move how
far? Only to the right end minus 1. Repeating.. - Why is this called a bubble sort?
7Bubble sort - analysis
- How many comparisons?
- How many swaps?
- Whats the best case?
- Whats the worst case?
8Selection sort
- Start at left end
- Keep track of smallest when you find a new
smaller, swap it to left end - any reason why not the biggest?
- what to do if equal?
- Move to the right, repeating...
- What have you got?
- smallest at the left end
- Now start at left end plus 1, repeating
9Selection sort - analysis
- How many comparisons?
- How many copies?
- Best case?
- Worst case?
10Insertion sort
- Start at left end plus one
- put this item in its proper place among the ones
on its left - now move one position to the right, put this item
in its proper place among the ones the left,
repeat....
11Insertion sort - analysis
- How many comparisons?
- How many swaps?
- Best case?
- Worst case?
12Efficiencies
- For random data (which means what?), all 3 run in
O(N2) - so if it took 20 sec to sort 10,000 items, how
long will it take to sort 20,000? - Remember this ignores the constants...
- so...under the right conditions, one runs faster
than another - for smaller N, selection sort may be faster than
bubble sort - for data already nearly in order, insertion runs
in (nearly) O(N)
13Divide-and-Conquer techniques
- Take a problem space, divide into two, solve
each, merge solution - Why is this better?
- Often you can disregard one of the partitions!
- You can only divide into 2 log n times
- So, if merging takes O(1), we get total O(lg n)!
- And if merging takes O(n), we get total O(n lg
n)! - O(lg n) is way better than O(n)
- O(n lg n) is way better than O(n2)
14Mergesort a divide-conquer sorting strategy
- Divide the array into two equal (almost) halves
- Sort each half separately
- Can call mergesort recursively!
- Merge the two halves
- O(n) time complexity if we use O(n) extra space
- O(n) complexity even if we do not use extra space
how? - Total runtime is O(n lg n) average and optimal
case.
15Recursion Tree for MergesortAn Example of 7
numbers
16Divide and Conquer SortingQuicksort
- In Quicksort
- We first choose some key from the list for
which, we hope, about half the keys will come
before and half after. Call this key the pivot.
Then we partition the items so that all those
with keys less than the pivot come in one
sublist, and all those with greater keys come in
another. Then we sort the two reduced lists
separately, put the sublists together, and the
whole list will be in order.
17Trace of QuicksortExample of the Same 7 Numbers
Sort (26,33,35,29,19,12,22)
18Recursion Tree for QuicksortExample of the Same
7 Numbers
19QuicksortContiguous Implementation
20Quicksort Implementation(Contd.)
21Analysis of Quicksort
- Worst-case analysis
- If the pivot is chosen poorly, one of the
partitioned sublists may be empty and others
reduced by only one entry. In this case,
quicksort is slower than either insertion sort or
selection sort. - Average-case analysis
- In its average case, quicksort performs
- O(n lg n)
- comparisons of keys in sorting a list of n
entries in random order.
22Quicksort Choice of Pivot
- First or last entry Worst case appears for a
list already sorted or in reverse order. - Median of three entry Poor cases are rare.
- Random entry Poor cases are very unlikely to
occur. Ensure that the random number generation
algorithm is fast.
23Pointers and Pitfalls
- Many computer systems have a general-purpose
sorting utility. If you can access this utility
and it proves adequate for your application, then
use it rather than writing a sorting program from
scratch. - In choosing a sorting method, take into account
the ways in which the keys will usually be
arranged before sorting, the size of the
application, the amount of time available for
programming, the need to save computer time and
space, the way in which the data structures are
implemented, the cost of moving data and cost of
comparing keys. - Divide-and-conquer is one of the most widely
applicable and most powerful methods for
designing algorithms. When faced with programming
problem, see if its solution can be obtained by
first solving the problem for two (or more)
problems of the same general form but of a
smaller size. If so, you may be able to formulate
an algorithm that uses the divide-and-conquer
method and program it using recursion.