Created
February 2, 2017 08:06
-
-
Save LindaLawton/ba99566458b961e32ea7a7f77e2a11a5 to your computer and use it in GitHub Desktop.
Big O notation shows time complexity of an algorithm.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Program | |
{ | |
delegate int del(int i); | |
delegate int[] delArray(int i); | |
delegate int[] delArray2(int[] i); | |
static void Main(string[] args) | |
{ | |
//Big O Notation | |
//Big O notation shows time complexity of an algorithm. | |
//Time complexity refers to how fast an algorithm works compared to the input size n. | |
//Big O is one form of asymptotic notation. | |
//Constant time: O(1) | |
//Logarithmic time: O(log n) | |
//Linear time: O(n) | |
//Linearithmic time: O(n log n) | |
//Polynomial time: O(n * *2) | |
//Exponential time: O(10 * *n) | |
//O(1) | |
var num = 10; | |
del addOne = x => x++; | |
var j = addOne(num); | |
Console.WriteLine("O(1):"); | |
Console.WriteLine(addOne(num)); | |
Console.WriteLine(); | |
Console.WriteLine(); | |
//# O(log n) | |
del divide = l => | |
{ | |
var checkCnt = 0; | |
while (l > 1) | |
{ | |
l /= 2; | |
Console.WriteLine(l); | |
checkCnt++; | |
} | |
Console.WriteLine("Check Count:" + checkCnt); | |
return l; | |
}; | |
Console.WriteLine("O(log n):"); | |
divide(num); | |
Console.WriteLine(); | |
Console.WriteLine(); | |
//# O(n) | |
delArray makeArray = l => | |
{ | |
var checkCnt = 1; | |
int[] results = new int[l - 1]; | |
for (int i = 1; i < l; i++) | |
{ | |
results[i - 1] = i; | |
Console.WriteLine(" [" + string.Join("],[", results) + "] "); | |
checkCnt++; | |
} | |
Console.WriteLine("Check Count:" + checkCnt); | |
return results; | |
}; | |
Console.WriteLine("O(n):"); | |
var testArray = makeArray(num); | |
Console.WriteLine(); | |
Console.WriteLine(); | |
// O(n^2) bubble sort | |
delArray2 sortArrayDescending = l => | |
{ | |
Console.WriteLine(" [" + string.Join("],[", l) + "] "); | |
var checkCnt = 0; | |
for (int i = l.Length - 1; i >= 0; i--) | |
{ | |
for (int x = i - 1; x >= 0; x--) | |
{ | |
if (l[i] > l[x]) | |
{ | |
var hold = l[i]; | |
l[i] = l[x]; | |
l[x] = hold; | |
Console.WriteLine("Compare l[" + i + "] and l[" + x + "] : swapped "); | |
checkCnt++; | |
} | |
} | |
} | |
Console.WriteLine(" [" + string.Join("],[", l) + "] "); | |
Console.WriteLine("Swaps made:" + checkCnt); | |
return l; | |
}; | |
Console.WriteLine("O(n^2):"); | |
testArray = sortArrayDescending(testArray); | |
Console.WriteLine(); | |
Console.WriteLine(); | |
Console.WriteLine("O(n^2): Best case"); | |
sortArrayDescending(testArray); | |
Console.WriteLine(); | |
Console.WriteLine(); | |
Console.WriteLine("O(n log n): MergeSort"); | |
// O(n log n) | |
testArray = new int[] { 1, 43, 31, 21, 6, 96, 48, 13, 25, 5 }; | |
Sort(testArray, 0, testArray.Length - 1); | |
Console.Read(); | |
} | |
public static void Sort(int[] data, int left, int right) | |
{ | |
if (right > left) | |
{ | |
var mid = (right + left) / 2; | |
Sort(data, left, mid); | |
Sort(data, mid + 1, right); | |
Merge(data, left, mid + 1, right); | |
} | |
} | |
public static void Merge(int[] data, int left, int mid, int right) | |
{ | |
var tmp = new int[data.Length]; | |
var eol = mid - 1; | |
var pos = left; | |
var num = (right - left) + 1; | |
Console.WriteLine("Left: [" + string.Join("],[", new List<int>(data).GetRange(left, eol-left+1).ToArray() ) + "] "); | |
Console.WriteLine("Right: [" + string.Join("],[", new List<int>(data).GetRange(mid,right - mid +1).ToArray()) + "] "); | |
while ((left <= eol) && (mid <= right)) | |
{ | |
if (data[left] <= data[mid]) | |
tmp[pos++] = data[left++]; | |
else | |
tmp[pos++] = data[mid++]; | |
} | |
while (left <= eol) | |
tmp[pos++] = data[left++]; | |
while (mid <= right) | |
tmp[pos++] = data[mid++]; | |
for (int i = 0; i < num; i++) | |
{ | |
data[right] = tmp[right]; | |
right--; | |
} | |
Console.WriteLine("Final: [" + string.Join("],[", tmp) + "] "); | |
Console.WriteLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment