



















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of three popular sorting algorithms: insertionsort, mergesort, and quicksort. Each algorithm is explained in detail, including their time complexities and advantages. Insertionsort is a simple algorithm that works well for small arrays, mergesort is a divide-and-conquer algorithm that has a better average time complexity, and quicksort is an in-place partitioning algorithm that is efficient for larger arrays.
Typology: Lecture notes
1 / 27
This page cannot be seen from the preview
Don't miss anything!




















Many people sort cards this way
-^
Invariant of main loop:
a[0..i-1] is sorted
Works especially well when input is
nearly sorted
^
Worst-case: O(n
2 )
(reverse-sorted input) ^
Best-case: O(n)
(sorted input) ^
Expected case: O(n
2 )
^
Expected number ofinversions: n(n–1)/
//sort a[], an array of intfor (int i = 1; i < a.length; i++) { // Push a[i] down to its sorted position//
in a[0..i]
int temp = a[i];int k;for (k = i; 0 < k && temp < a[k–1]; k– –)
a[k] = a[k–1]; a[k] = temp; }
Quintessential divide-and-conquer algorithm
Divide array into equal parts, sort each part, then merge
Questions:^
Q1: How do we divide array into two equal parts?A1: Find middle index: a.length/
Q2: How do we sort the parts?A2: Call MergeSort recursively!
Q3: How do we merge the sorted subarrays?A3: Write some (easy) code
Create
array
of
size:
size
of
size
of
i=
j=
k=
initially,
nothing
copied
Copy
smaller
of
A[i]
and
B[j]
into
C[k]
Increment
i^
or
j,
whichever
one
was
used,
and
k
When
either
or
becomes
empty,
copy
remaining
elements
from
the
other
array
or
respectively)
into
This tells what has been done so far:A[0..i-1] and B[0..j-1] have been placed in C[0..k-1].C[0..k-1] is sorted.
Outline
(code
on
website)
Split
array
into
two
halves
Recursively
sort
each
half
Merge
two
halves
Merge:
combine
two
sorted
arrays
into
one
sorted
array
Rule:
always
choose
smallest
item
Time:
O(n)
where
n
is
the
total
size
of
the
two
arrays
Runtime recurrenceT(n): time to sort array of size n
T(1) = 1T(n) = 2T(n/2) + O(n) Can show by induction that
T(n) is O(n log n) Alternatively, can see thatT(n) is O(n log n) by looking attree of recursive calls
To sort b[h..k], which has an arbitrary value x in b[h]:
first swap array values around until b[h..k] looks like this
x
h
h+
k
<= x
x
= x
h
j^
k
Then sort b[h..j-1] and b[j+1..k] —how do you do that?
Recursively!
x is calledthe pivot
pivot
partition j
Not yetsorted
Not yet
sorted
QuickSort
QuickSort
sorted
In
‐ Place
Partitioning
Key
issues
How to choose a
pivot
How to
partition
array
in place? Partitioning
in
place
Takes O(n) time (nextslide)
Requires no extra space
Choosing pivot
Ideal pivot: the median, sinceit splits array in half
Computing median ofunsorted array is O(n), quitecomplicated Popular heuristics: Use
first array value (not good)
middle array value
median of first, middle, last,values GOOD!
Choose a random element
Change b[h..k]from this:to this by repeatedlyswapping arrayelements:
x
h
h+
k
<= x
x
= x
h
j^
k
b b
Do it one swap ata time, keeping thearray looking likethis. At each step, swapb[j+1] with something
<= x
x
= x
h
j^
t^
k
b
Start with:
j= h; t= k;
How can we move all the blues to the left of all the reds?
-^
Keep two indices, LEFT and RIGHT
-^
Initialize LEFT at start of array and RIGHT at end of array Invariant:
all elements to left of LEFT are blueall elements to right of RIGHT are red
-^
Keep advancing indices until they pass, maintaining invaria
nt
Now neither LEFT nor RIGHT can advance and maintain invariant.We can swap red and blue pointed to by LEFT and RIGHT indices.After swap, indices can continue to advance until next conflict.
swap swap swap
/** Sort b[h..k]. */ public
static
void
int
[] b,
int
h,
int
k) {
if
(b[h..k] has < 2 elements)
return
int
j= partition(b, h, k); // We know b[h..j–1] <= b[j] <= b[j+1..k]// So we need to sort b[h..j-1] and b[j+1..k]QS(b, h, j-1);QS(b, j+1, k); }
Base case Function does thepartition algorithm andreturns position j ofpivot
/** Sort b[h..k] */ public
static
void
( int
[] b,
int
h,
int
k) {
if
(k – h < 1)
return
int
j= partition(b, h, k); QS(b, h, j-1);QS(b, j+1, k); }
/** Sort b[h..k] */ public
static
void
( int
[] b,
int
h,
int
k) {
if
(k – h < 1)
return
MS(b, h, (h+k)/2);MS(b, (h+k)/2 + 1, k); merge(b, h, (h+k)/2, k); }
One processes the array then recurses.One recurses then processes the array.