Sorting Algorithms: InsertionSort, MergeSort, and QuickSort, Lecture notes of Object Oriented Programming

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

2012/2013

Uploaded on 08/20/2013

yumni
yumni 🇮🇳

5

(2)

25 documents

1 / 27

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Sorting and Asymptotic
Complexity
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b

Partial preview of the text

Download Sorting Algorithms: InsertionSort, MergeSort, and QuickSort and more Lecture notes Object Oriented Programming in PDF only on Docsity!

Sorting and Asymptotic

Complexity

InsertionSort

•^

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; }

Divide

Conquer?

It

often

pays

to

Break

the

problem

into

smaller

subproblems,

Solve

the

subproblems

separately,

and

then

Assemble

a

final

solution

This

technique

is

called

divide

‐and

‐conquer

Caveat:

It

won

t^

help

unless

the

partitioning

and

assembly

processes

are

inexpensive

Can

we

apply

this

approach

to

sorting?

MergeSort

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

Merging

Sorted

Arrays

A

and

B

into

C

•^

Create

array

C

of

size:

size

of

A

size

of

B
•^

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

A

or

B

becomes

empty,

copy

remaining

elements

from

the

other

array

(B

or

A,

respectively)

into

C

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.

MergeSort

Analysis

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

QuickSort

Idea

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

In

Place

Partitioning

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;

In

Place

Partitioning

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

QuickSort

procedure

/** Sort b[h..k]. */ public

static

void

QS(

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

QuickSort

versus

MergeSort

/** Sort b[h..k] */ public

static

void

QS

( 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

MS

( 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.