Algorithmic Analysis of Sort Functions
From charlesreid1
Analysis of Selection Sort
Consider the following selection sort algorithm:
selection_sort(int s[], int n)
{
int i, j; // counters
int min; // index of minimum
for(i=0; i<n; i++) {
min = i;
for(j=i+1; j<n; j++)
if(s[j] < s[min]) min = j;
swap(&s[i], &s[min]);
}
}
performing the algorithmic analysis:
- for loop with i index operates O(n) times
- second for loop operates O(i) times, within the loop that runs n times, for an algorithmic complexity given below.
$ O(i) : \sum_{i=1}^{n} i = \dfrac{n(n+1)}{2} \sim O(n^2) $
Overall this algorithm is quadratic.
Analysis of Insertion Sort
Consider the following insertion sort algorithm:
for(i=1; i<n; i++) {
j = i;
while((j>0)&&(s[j]<s[j-1])) {
swap(&s[j],&s[j-1]);
j=j-1;
}
}
This algorithm requires focusing on the worst case scenario.
The outer for loop runs n times, and is O(n).
The inner for loop has two conditions that must be met. We can assume j>0 always, and focus on when the other condition would be true - when would a random index of S be less than its neighbor? Worst case assumption is, it will always be smaller, and so the while loop will run every single time. This gives us a while loop where j iterates from i to 0.
As before, the outer loop is O(n) and the inner loop is O(i), and therefore runs $ \frac{n(n+1)}{2} $ times, or $ O(n^2) $.
| Computer Science notes on computer science topics on the wiki, for educational and learning purposes
Part of the 2017 CS Study Plan.
Python/Exceptions · Python/Assertions · Python/Decorators Python/Os (os module) · Python/Strings Python/Splat · Python/Iterators · Python/Generators Python/Comparators · Python/Lambdas
Builtin features of Java: Java/Exceptions · Java/Assertions · Java/Memory · Java/Interfaces Java/Generics · Java/Decorators · Java/Diamond Notation Java/Iterators · Java/Iterable · Iterators vs Iterable Java/Comparators · Java/Comparable · Comparators vs Comparable Java/Numeric · Java/TypeChecking · Java/Testing · Java/Timing · Java/Profiling Documentation: Javadocs · Java/Documentation Tools and functionality: Java/URLs · Java/CSV External libraries: Guava · Fastutil · Eclipse Collections OOP: OOP Checklist · Java/Abstract Class · Java/Encapsulation · Java/Generics
|
See also: