Algorithmic Analysis of Sort Functions: Difference between revisions
From charlesreid1
(Fix math syntax: \dfrac → \frac (amsmath-specific command not supported by all renderers); fix heading =Flags= → ==Flags== (via update-page on MediaWiki MCP Server)) |
|||
| (8 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
==Analysis of Selection Sort== | ==Analysis of Selection Sort== | ||
| Line 23: | Line 24: | ||
<math> | <math> | ||
O(i) : \sum_{i=1}^{n} i = \ | O(i) : \sum_{i=1}^{n} i = \frac{n(n+1)}{2} \sim O(n^2) | ||
</math> | </math> | ||
| Line 41: | Line 42: | ||
} | } | ||
</pre> | </pre> | ||
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 <math>\frac{n(n+1)}{2}</math> times, or <math>O(n^2)</math>. | |||
==Flags== | |||
{{AlgorithmsFlag}} | |||
[[Category:C]] | |||
[[Category:Java]] | |||
Latest revision as of 23:25, 5 June 2026
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 = \frac{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) $.
Flags
| Algorithms Part of Computer Science Notes
Series on Algorithms
Algorithms/Sort · Algorithmic Analysis of Sort Functions · Divide and Conquer · Divide and Conquer/Master Theorem Three solid O(n log n) search algorithms: Merge Sort · Heap Sort · Quick Sort Algorithm Analysis/Merge Sort · Algorithm Analysis/Randomized Quick Sort
Algorithms/Search · Binary Search · Binary Search Modifications
Algorithms/Combinatorics · Algorithms/Combinatorics and Heuristics · Algorithms/Optimization · Divide and Conquer
Algorithms/Strings · Algorithm Analysis/Substring Pattern Matching
Algorithm complexity · Theta vs Big O Amortization · Amortization/Aggregate Method · Amortization/Accounting Method Algorithm Analysis/Matrix Multiplication
Estimation Estimation · Estimation/BitsAndBytes
Algorithm Practice and Writeups Project Euler · Five Letter Words · Letter Coverage
|