/// <summary> /// Insertion sort algorithm. /// Time complexity: O(n*n) /// Space complexity: O(1) /// /// It divides(logically) the list into 2 parts - sorted and unsorted. /// The first element of the list is considered the sorted and /// remaining list is considered unsorted. /// At a time it picks one element from unsorted list and puts it in it's place in the sorted list. /// /// It is much less efficient on large lists than more advanced algorithms, /// such as QuickSort, HeadSort and MergeSort. /// </summary> /// <param name="elements">The elements.</param> public static SortResult InsertionSort(this IList <int> elements) { var result = new SortResult(); var sw = Stopwatch.StartNew(); //Insertion Sort Algorithm var sortedIndex = 0; var unsortedIndex = 0; var firstUnsortedElement = elements[1]; while (++unsortedIndex < elements.Count) { sortedIndex = unsortedIndex - 1; firstUnsortedElement = elements[unsortedIndex]; if (firstUnsortedElement < elements[sortedIndex]) //when unsorted first is less than sorted last. { while ((0 <= sortedIndex) && (firstUnsortedElement < elements[sortedIndex])) { //swap the elements elements[sortedIndex] = elements[sortedIndex] + elements[sortedIndex + 1]; elements[sortedIndex + 1] = elements[sortedIndex] - elements[sortedIndex + 1]; elements[sortedIndex] = elements[sortedIndex] - elements[sortedIndex + 1]; --sortedIndex; } } } sw.Stop(); result.Ticks = sw.ElapsedTicks; result.Elements = elements; return(result); }
/// <summary> /// Bubble sort algorithm. /// Time complexity: O(n*n) /// Space complexity: O(n) /// /// The only significant advantage that bubble sort has over most other algorithms, /// is that the ability to detect that the list is already sorted is built into the /// algorithm. /// /// If the list is already sorted (best-case), its time-complexity is only O(n), /// And space-complexity is O(1). Most other algorithms, even those with better /// average-case complexity, performs their entire sorting process on the set and thus /// are more complex. /// /// It should be avoided in the case of large collections. /// </summary> /// <param name="elements">The elements.</param> public static SortResult BubbleSort(this IList <int> elements) { var result = new SortResult(); var sw = Stopwatch.StartNew(); //Bubble Sort Algorithm int i, j; var swapped = true; while (swapped) { i = -1; j = 0; swapped = false; while ((++i < (elements.Count - 1)) && (++j < elements.Count)) { if (elements[j] < elements[i]) //when element at i is greater than element at j. { //swap the elements. elements[j] = elements[j] + elements[i]; elements[i] = elements[j] - elements[i]; elements[j] = elements[j] - elements[i]; swapped = true; } } } sw.Stop(); result.Ticks = sw.ElapsedTicks; result.Elements = elements; return(result); }
protected static SortResult HandleSort(Func <IList <int> > fn, Algorithm algorithm) { var stopwatch = Stopwatch.StartNew(); var sortResult = fn(); stopwatch.Stop(); return(SortResult.Create(algorithm, sortResult, stopwatch.Elapsed)); }
public void RankingIsSetCorrectly() { var ranking = new List <ImmutableGenome> { new ImmutableGenome(new Genome(1)), new ImmutableGenome(new Genome(2)) }; var result = new SortResult(ranking.ToImmutableList()); Assert.Equal(ranking, result.Ranking); }
public void WhenSwapWithOutOfBoundArgumentThenExceptionIsThrown() { SortResult result = new SortResult("2,1,0"); Assert.IsTrue(result.IsValid); Assert.AreEqual(2, result.GetIndex(0)); Assert.AreEqual(1, result.GetIndex(1)); Assert.AreEqual(0, result.GetIndex(2)); result.Swap(0, 3); }
/// <summary> /// Quick sort algorithm. /// Time complexity: O(n (log n)) for best and average case. O(n*n) for worst case. /// Space complexity: O(log n) /// /// /// </summary> /// <param name="elements">The elements.</param> /// <returns></returns> public static SortResult QuickSort(this IList <int> elements) { var result = new SortResult(); var sw = Stopwatch.StartNew(); QuickSort_Sort(elements, 0, elements.Count - 1); sw.Stop(); result.Ticks = sw.ElapsedTicks; result.Elements = elements; return(result); }