ECS110 Lecture Notes for Monday, November 6, 1995
Professor Rogaway conducting
Lecture 16
Scribe of the day: Dave D. Lin
Reading:
Read chapter 7
may omit Chapter 7, section 10
Today:
Sorting Problem
1) Bubble Sort
2) Insertion Sort
3) Merge Sort
4) Quick Sort
SORTING PROBLEM:
Let say there is a group that consists of Records: R1.......Rn
and then there are keys: K1.......Kn
from total ordering:
if Ki, Kj either Ki <= Kj or
Kj >= Ki
and
if Ki < = Kj AND Kj <= Kj ==> Ki = Kj
examples for keys:
- ints, reals
In example:
- strings - usual ordering
" Dictionary order"
abc <= adb
a <= adb
ax >= adb
But in"lexicographic order"
ax <= adb
{ 0, 1, 00, 01, 10, 11, 000}
order is
_________
0 <= | 1 <= 00 <=
_________
- topless order
Sorting Problem:
_______________
Find a Permutation 'Pi'[1..n] -> [1..n]
Such that
K pi(1) <= K pi(2) <= K pi(3) <= ........ <= K pi(n)
The techniques:
______________
I) Bubble Sort
===========================================================================
The number is increasing from left to right
( small -------------> big )
we start with the following numbers which are without any order
i) 680 420 100 368 561 421 500
after the first bubble sort, we have
ii) 100 680 420 368 421 561 500
( the smallest number is been moved to the left most position and then
we are not going to change it after the first bubble sort )
after the scond bubble sort, we have
iii) 100 368 420 680 421 561 500
(note that every time we sort the list, we have one less element to sort
than before. So eventually, after n times, all the elements should be
sorted)
iv) 100 368 420 421 680 561 500
v) 100 368 420 421 680 561 500
vi) 100 368 420 421 500 680 561
vii) 100 368 420 421 500 561 680
( finally, all the elements in the list are sorted )
T(n) = n + (n - 1) + (n - 2) + (n - 3) + (n - 4)....(2) + (1)
~= n ( n/2 ) = theta ( n^2)
----------------------------------
a little more accurate |
representation: |
|
( n ( n + 1 )) /2 |
----------------------------------
conclude: Bubble sort
time complexity:
theta ( n ^ 2)
the sort is done in place
____________________________________________________________________________
II) Insertion Sort
First we have the following elements in the list
i) 500 368 421 561 680 100 420
(Note: In insertion sort, we pick out the smallest element in
the list and then push back every other elements to make
room for that smallest element. Once that is done, then we
leave the first element alone and then look for the next smallest
element and so on.
)
ii) 100 500 368 421 561 680 420
(Note: there are two different ways to push back all elements to make
room for the right element to be inserted. First, we can set up temp
element and then swap every single one of them. In the second way, we
increase all the element positions by one and then put the element into
the space that we have created. )
iii) 100 368 500 421 561 680 420
iv) 100 368 420 500 421 561 680
v) 100 368 420 421 500 561 680
vi) 100 368 420 421 500 561 680
( Insertion sort, done! )
( it doesn't matter if we creat the space for element insertion by
swaping every single one of them or move every element to one position
back. In either case, we move the list by n times)
Time complexity ( for Insertion sort )
1 + 2 + 3 + ..... + n
= theta ( n ^ 2 )
(in place sort)
_____________________________________________________________________________
III) Merge Sort
Let say that we have the following elements ( totally unsorted )
561 500 680 100 420 600 368 421
The first step in Merge sort is to divide all the elements into two groups.
Let pick a center for example;
then we have
Step1
Group 1: Group 2:
561 500 680 100 420 600 368 421
The next step is to sort these different groups by recursively doing the
merge sort on the two smaller groups until you are down to one element
which is already sorted. When you are down to one element on the list,
you merge the lists back together.
Step2 (break Group1 and Group2 into two separate lists )
Group1.1: Group1.2: Group2.1: Group2.2:
561 500 680 100 420 600 368 421
Step3 (break them up again)
Group1.1.1: Group1.1.2: Group1.2.1: Group1.2.2:
561 500 680 100
Group2.1.1: Group2.1.2: Group2.2.1: Group2.2.2:
420 600 368 421
(since groups of size one are already sorted, we now merge
the groups back together)
Step4 (merge the groups back together in pairs)
Group1.1: Group1.2: Group2.1: Group2.2:
500 561 100 680 420 600 368 421
Step5 (merge again)
So, the two sorted groups are:
Group #1: Group #2:
100 500 561 680 368 420 421 600
^ ^
| |
ptr1 ptr2
And then we merge them by comparing p1 and p2, the smallest of those two
will then put into a new list and then move the ptr to the right. After
insertion, we then do the comparison again and so on. We will continue
until one of the two ptrs is at the end of the list. That's when we just
copy the rest of the other ptr elements to the new list.
Result of the above Merge sort turns out to be
100 368 420 421 500 561 600 680
Time complexity for the Merge sort
-
| 1 if n <= 1 ( single element by itself is always sorted )
T(n) = |
| 2T( n/2 ) + n
-
2T( n/2 ) is for the time to sort both left side and right side of the list.
and n is for time to merge both lists.
We can recall that
T(n) = 2 T(n/2) + n
= 2 (2T(n/4) + n/2) + n
= 4 (T(n/4)) + n
= 8 (T(n/8)) + 3n
= 16(T(n/16)) + 4n
= theta ( n lg n )
Time complexity ( merge sort ):
theta ( n log n )
(not in place )
and
theta ( n )
( if merging n stack size )
______________________________________________________________________________
IV: Quick Sort-----Hoare ( 1962 )
At first we have the follwoing elements.
420 500 100 680 561 368 600 421
First we pick any random number as a pivot point.
( the ideal situation is that the pivot point is the center in the list
however it's not necessary )
( The trick of why quicksort is so fast is because everytime the
sort go thought the list, they put everything lower than pivot point one the
left side and greater than pivot point on the other. And then we pick a
pivot point within those tow halves, and then choose a pivot there, as
the program progress, more pivot points are chosen and the sort runs
faster.
The code for QuickSort
***********************************************************************
QuickSort ( A, L, R)
{
if L < R then
q <- partition( A, L, R ); // All A[L...q-1] <= All A[q..r]
Quick sort ( A, L, q-1);
Quick Sort ( A, q, r);
}
==========================================================================
That's all. End of lecture # 16