Skip to content

Commit 165f52c

Browse files
ClémentClément
authored andcommitted
Notes on complexity.
1 parent cde64f2 commit 165f52c

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

source/code/projects/Sorting/Sorting/Sorting.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
static class Sorting<T>
55
where T : IComparable<T>
66
{
7-
// Helper method
7+
// Helper methods
88
private static void Swap(List<T> listP, int lhs, int rhs)
99
{
1010
T current = listP[lhs];
@@ -24,7 +24,7 @@ public static bool IsSorted(List<T> listP)
2424
}
2525
return isSorted;
2626
}
27-
// Done with helper method.
27+
// Done with helpers method.
2828

2929
// Insertion Algorithm
3030
public static void InsertionSort(List<T> listP)
@@ -48,13 +48,13 @@ public static void InsertionSort(List<T> listP)
4848

4949
// Done with insertion Algorithm
5050

51-
// Helper methods for Heapsort
51+
// Helper method for Heapsort
5252
private static int LeftChild(int i)
5353
{
5454
return 2 * i + 1;
5555
}
5656

57-
// Done with helper methods for Heapsort
57+
// Done with helper method for Heapsort
5858

5959
// Heapsort Algorithm
6060
public static void Heapsort(List<T> listP)

source/lectures/misc/sorting.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Merge sort | $n \log n$ | $n \log n$ | $n \log n$ | n | Yes | No |
4646
We start by defining two simple "helper" methods:
4747

4848
```
49-
!include`snippetStart="// Helper method", snippetEnd="// Done with helper method."` code/projects/Sorting/Sorting/Sorting.cs
49+
!include`snippetStart="// Helper methods", snippetEnd="// Done with helpers method."` code/projects/Sorting/Sorting/Sorting.cs
5050
```
5151

5252
## Insertion Sort Algorithm
@@ -57,6 +57,9 @@ This algorithm is [nicely explained and illustrated on wikipedia](https://en.wik
5757
!include`snippetStart="// Insertion Algorithm", snippetEnd="// Done with insertion Algorithm"` code/projects/Sorting/Sorting/Sorting.cs
5858
```
5959

60+
[As explained on wikipedia](https://en.wikipedia.org/wiki/Insertion_sort#Best,_worst,_and_average_cases), the simplest worst case input is an array sorted in reverse order.
61+
With an array sorted in reverse order, every iteration of the inner loop will scan and shift the entire sorted subsection of the array before inserting the next element. This gives insertion sort a quadratic running time (i.e., $O(n^2)$).
62+
6063
## Heapsort Algorithm
6164

6265
We first define some helper methods:
@@ -73,6 +76,9 @@ and then leverage the heap structure to sort:
7376

7477
Note that `PercDown` builds a *max heap*: once the values are "pre-sorted **greater value first**", removing the first one to move it to the *end* of the list makes the list sorted from smallest to greatest value once we are done.
7578

79+
The `PercDown` is first called $N / 2$ times, which is equivalent to $O(n)$.
80+
Its complexity is $O(\log(n))$, and it is called a second time $O(n)$ times.
81+
So, we get an overall performance of $O((n + n) \times \log n) = O(n \times \log(n))$.
7682

7783
## Bubble Algorithm
7884

0 commit comments

Comments
 (0)