Start with an unsorted arrayA
withN
items while the array is unsorted{ For everyposition
between 1 andN
{ ifA[i]
andA[i-1]
are not in the correct order{ SwapA[i]
andA[i-1]
} } }
Start with an unsorted arrayA
withN
items For everypos
between 1 andN-1
{ Letj
=pos
LetITEM
=A[pos]
Whilej
> 0 andITEM
is smaller thanA[j -1]
{ SetA[j]
=A[j-1]
Decreasej
by 1 } SetA[j]
=ITEM
}
Let's implement this
Why is insertion sort faster on average?
An estimate number of times a particular operation is repeated by your algorithm:
N
N
items.An algorithm is A faster than algorithm B if
As N
grows to infinity, A requires less operations to give an answer.
An algorithm is A faster than algorithm B if
As N
grows to infinity, A requires less operations to give an answer.
N
comparisons are needed to find a target number N
)+1 comparisons are needed to find a target number The worst case: The target number is not in the array
An algorithm is A faster than algorithm B if
As N
grows to infinity, A requires less operations to give an answer.
N
2 comparisons are needed N
2 comparisons are needed The worst case for BubbleSort and InsertionSort: The array is sorted in reverse
An estimate of the number of times a particular operation is repeated by your algorithm, in the worst case ( An upper bound )
Not an exact number or expression. We only look at how fast it grows when N
becomes very large.
N
comparisons are needed to find a target number N
)+1 comparisons are needed to find a target number N
2 comparisons are needed N
2 comparisons are needed An estimate of the number of times a particular operation is repeated by your algorithm, in the worst case ( An upper bound )
Not an exact number or expression. We only look at how fast it grows when N
becomes very large.
N
) comparisons N
) ) comparisons N
2 ) comparisonsN
2 ) comparisonsCan we do some trick like in binary search?
Idea 1: instead of sorting a list of size N
, sort two lists of size N/2
and merge them together
Idea 1: instead of sorting a list of size N
, sort two lists of size N/2
and merge them together
Idea 2: instead of sorting a list of size N/2
, sort two lists of size N/4
and merge them together
Idea 3: instead of sorting a list of size N/4
, sort two lists of size N/8
and merge them together
...
Idea K: instead of sorting a list of size 2
, sort two lists of size 1
and merge them together
Idea 1: instead of sorting a list of size N
, sort two lists of size N/2
and merge them together
Idea 2: instead of sorting a list of size N/2
, sort two lists of size N/4
and merge them together
Idea 3: instead of sorting a list of size N/4
, sort two lists of size N/8
and merge them together
...
Idea i : instead of sorting a list of size N/(2^(i-1))
, sort two lists of size N/(2^i)
and merge them together
...
Idea K: instead of sorting a list of size 2
, sort two lists of size 1
and merge them together
An algorithm is A faster than algorithm B if
As N
grows to infinity, A requires less operations to give an answer.
N
2 comparisons are needed N
2 comparisons are needed N
*log2(N
) comparisons are needed The worst case for BubbleSort and InsertionSort: The array is sorted in reverse
The worst case for MergeSort: The number of comparisons is independent from the state of the array
Merge (A
,B
) { LetN
be the number of items inA
LetM
be the number of items inB
Create an empty arrayR
with sizeN+M
Leti
= 0, andj
= 0 Whilei
+j
<N
+M
-1 { ifi
==N
{ // Reached the end of array AR[i+j]
=B[j]
j
=j
+ 1 } else ifj
==M
{ // Reached the end of array BR[i+j]
=A[j]
i
=i
+ 1 } else ifA[i]
≤B[j]
{R[i+j]
=A[i]
i
=i
+ 1 } else ifA[i]
≥B[j]
{R[i+j]
=B[j]
j
=j
+ 1 } } return R; }
A:
B:
Merged result:
MergeSort (A
) { LetN
be the number of items inA
ifN
<2
{ // An array with 0 or 1 elements is already sorted returnA
} Copy the left half ofA
inleftHalf
Copy the right half ofA
inrightHalf
leftHalf
= MergeSort (leftHalf
)rightHalf
= MergeSort (righttHalf
)result
= Merge(leftHalf
,rightHalf
) returnresult
}
MergeSort (A
) { LetN
be the number of items inA
ifN
<2
{ // An array with 0 or 1 elements is already sorted returnA
} Copy the left half ofA
inleftHalf
Copy the right half ofA
inrightHalf
// What is this ??????leftHalf
= MergeSort (leftHalf
)rightHalf
= MergeSort (righttHalf
)result
= Merge(leftHalf
,rightHalf
) returnresult
}
Remember, methods can be viewed as boxes that process data
We can call other methods inside any method we define
In this case, someMethod
stops until anotherMethod
returns
We can call other methods inside any method we define
In this case, magicMethod
stops until System.out.println
returns
There is no reason why we can't call a method inside its definition
In this case, magicMethod
calls System.out.println
Then, magicMethod
stops until the second call to magicMethod
returns
This is called recursion.
In this case, magicMethod
stops until System.out.println
Then, magicMethod
stops until the second call to magicMethod
returns
When will the second call to magicMethod
return ?
Writing down the steps executed by the previous code
magicMethod
with 10
as input
System.out.println
magicMethod
again, with 9
as input
System.out.println
magicMethod
again, with 8
as input
System.out.println
magicMethod
again, with 7
as inputs
When will the second call to magicMethod
return ? The answer is never
magicMethod
with 10
as input
System.out.println
magicMethod
again, with 9
as input
System.out.println
magicMethod
again, with 8
as input
System.out.println
magicMethod
again, with 7
as inputs
For example, make it stop calling itself when n == 0
This is called a base case
For example, make it stop calling itself when n
== 0
This is called a base case
For example, make it stop calling itself when n
== 0
This is called a base case
The inner call to magicMethod
, happens when n
> 0. This is called the recursive case
N
We use this function to count the number of comparisons in BubbleSort and InsertionSort
N
== 1
N
> 1
N
We use this function to count the number of comparisons in BubbleSort and InsertionSort
N
== 1
N
> 1
N
+ the sum of all numbers from 1 to N -1
N!
= 1 * 2 * 3 * 4 * 5 * ... * N!We can use this function to count the number of different orderings of an array of size N
(i.e., permutations)
N
== 1
N
> 1
N!
We can use this function to count the number of different orderings of an array of size N
(i.e., permutations)
N
== 1
factorial(1)
= 1N
> 1
factorial(N)
= N
* factorial(N -1)
N
-th Fibonnaci number: 1, 1, 2 ,3 ,5, 8 , 13, ... A number sequence ubiquitous in science and mathematics
The N
-th Fibonnaci number is the sum of the two previous Fibonnaci numbers
N
== 0
N
== 1
N
> 2
N
-th Fibonnaci number: 1, 1, 2 ,3 ,5, 8 , 13, ... A number sequence ubiquitous in science and mathematics
The N
-th Fibonnaci number is the sum of the two previous Fibonnaci numbers
N
== 0
fibonnaci(0)
= 1N
== 1
fibonnaci(1)
= 1N
> 2
fibonnaci(N)
= fibonnaci(N-1) + fibonnaci(N-2)Hint: These recursive methods are very similar to the Fibonnaci sequence
/