Take-Away Notes for Module COMP2048.
Bubble Sort, Selection Sort, Insertion Sort;
- Proofread
- Reviewed
- Add Figures
Simple Sorting Algorithms
Bubble Sort
Bubble Sort repeatedly compares and swaps(if required)
adjacent elements in every pass.
Code Implementation
1 | void bubbleSort(int[] arr) { |
Complexity
(Worst) Time Complexity:
Note: For the ith pass through the all n-1 passes, there
would be n-i comparisons and swaps.
(Worst) Space Complexity:
In-Place Sort.
Selection Sort
Selection Sort selects ith smallest/biggest element from
the unsorted sub-sequence and places it at ith position.
Arena-Driven.
Code Implementation
1 | void selectionSort(int[] arr){ |
Complexity
(Worst) Time Complexity:
Note: For the ith pass through the all n-1 passes, there
would be n-i comparisons but 1 swap only.
(Worst) Space Complexity:
In-Place Sort.
Insertion Sort
Selection Sort inserts every element from the unsorted
sub-sequence and inserts it in a appropriate position within a sorted
subsequences.
Code Implementation
1 | void insertionSort(int[] arr){ |
Complexity
(Worst) Time Complexity:
Note: Must make
(Worst) Space Complexity:
In-Place Sort.
Advanced Sorting Algorithms
Merge Sort
Divide-and-Conquer based sorting algorithms. -
Divide: Divide the input data in two (or more) disjoint
subsets - Conquer: Solve the sub-problems recursively -
Combine: Combine the solutions to sub-problems into a
solution for the problem
Code Implementation
1 | void mergeSort(int[] arr) { |
Complexity
(Average) Time Complexity:
(Worst) Space Complexity:
Out-Place Sort.
Heap Sort
TBC.
See also in: Runoob.com
Quick Sort
Divide-Recur-Conquer based randomized
sorting algorithms. - Divide
Pick a random element
elements less than elements equal to elements greater than RecurSort
and Concur
Join
, and .
Code Implementation
Time Complexity
Note: See also in Textbook, Section 12.2, Page 544 for further calculation
In-place Quick Sort
TBC. See also in the textbook, section 12.2.2, "Additional Optimizations for Quick Sort"
Summary
| Algorithm | Time Complexity | Notes |
|---|---|---|
| Selection Sort | In-Place; For little inputs |
|
| Insertion Sort | In-Place; For little inputs |
|
| Quick Sort | In-Place; Randomized Ideally; For
large inputs; Fastest |
|
| Heap Sort | In-Place; For large inputs; Fast |
|
| Merge Sort | Out-Place; Sequential Data Access(e.g. Queue); For
huge inputs; Fast |