Skip to main content

Posts

Showing posts with the label insertion sort

QuickSort

This is similar to Median Sort, which I skipped, but Quicksort supersedes it.  This uses a similar strategy, but instead of finding the median, it accepts any value for the pivot index, then simply performs an insertion sort on the resulting partitioned array of 2 sub-arrays.  The insertion sort used here is from a prior post on Insertion Sort . Class using System; namespace Algorithms {     class QuickSort     {         public void Sort()         {             //creates array             int[] arrayToSort = { 11, 1, 22, 2, 33, 3, 44, 4, 55, 5, 66, 6, 7, 77 };             //gets pivotIndex, set at midpoint of arry             int pivotIndex = Partition(arrayToSort, 0, arrayToSort.Length - 1, (arrayToSort.Length - 1)/2);             //sorts...

Insertion Sort

The implementation of insertion sort is very well known, but to improve upon the standard description this method uses System.Collections.ObjectModel.IComparable, allowing it to sort a range of types, and the class and methods are marked as static.  Both modifications make the methods more reusable than in typical examples.             IComparable[] start = new IComparable[] {44, 2,4,1,6,7,33,25,5,2};             IComparable[] result = InsertionSort.Sort(start);             IComparable[] startAlpha = new IComparable[] { "r", "A", "z", "c", "a", "h"};             IComparable[] resultAlpha = InsertionSort.Sort(startAlpha); The two lower routines are useful for quick sort, in that they will handle portions of arrays Class static class InsertionSort     {         /// <summary>         /// Given a ...