Posts

Showing posts from March, 2016

Migrating to Azure redis cache?

They are deprecating the inrole cache starting 2.9 SDK. It was trouble migrating it. I recommend you first install the session state provider for the redis cache and then use the strong named redis dll which comes with it for all other assemblies. If you first install the regular dll via nuget and then install the session state provider, it gets messed up.

Binary Search Algorithm in C# (min, max, middle position)

class BinarySearch     {         public static int Find(List<int> list, int searched)         {             // First sort input             BucketSort.Execute(list, ShellSort.Execute);             // Find min position             int min = 0 ;             // Find max position             int max = list.Count - 1 ;             int loops = 0;             do             {                 // Find middle position                 int middle = (min + max) / 2 ;                 if ( searched < list[middle] )                 {                     // Bring down max so we half the search span the next time.                     max = middle - 1 ;                 }                 else if ( searched > list[middle] )                 {                     // Bring up min so we can half the search span the next time.                     min = middle + 1 ;                 }                 else                 {                     Console.WriteLine($"Lo

Shell Sort Algorithm in C# (Insertion with middle position split)

class ShellSort     {         public static List<int> Execute(List<int> list)         {             // Start position is at the middle of the list.             var position = list.Count / 2 ;             // Iterate as long as we don't reach the "bottom"             while ( position > 0 )             {                 // Go through the entire list                 for (int i=0;i<list.Count;i++)                 {                     // Get current index                     var index = i;                     // Get current item                     var current = list[i];                     while ( index >= position &&                 // As long as we have not reached this position AND                             list[index - position] > current )    // Item at a more previous index greater than current                     {                         // Swap (travel more than one position)                         list[ind

Insertion Sort Algorithm in C# (Simple)

class InsertionSort     {         public static List<int> Execute(List<int> list)         {             // Iterate through entire list starting from 1             for (int i=1 ;i<list.Count;i++)             {                 var index = i ;                 // Get current item from list.                 var current = list[i] ;                 // While previous is greater than current                 while ( index > 0 && list[index - 1] > current )                 {                     // Swap                     list[index] = list[index - 1];                     // Go back                     index-- ;                 }                 // The current item is stored at the right position in the list.                 list[index] = current ;             }             return list;         }     }

MergeSort Algorithm in C# (middle)

class MergeSort     {         public static List<int> Execute(List<int> list)         {             if ( list.Count <= 1 ) { return list; }             // Find the middle point             int middle = list.Count / 2 ;             var left = new List<int>();             var right = new List<int>();             // Divide the list from the middle             for (int i=0;i<middle;i++)             {                 left.Add(list[i]);             }             for (int i=middle;i<list.Count;i++)             {                 right.Add(list[i]);             }             // Recursion to keep dividing and sorting.             left = Execute (left);             right = Execute (right);             // This indicates that sorting is done.             if ( left[left.Count-1] < right[0] )             {                 left.AddRange(right);                 return left;             }             // If sorting is not done... Mer

QuickSort Algorithm in C# (Pivot)

This is an interesting and relatively easy sort algorithm which is built around the creative use of recursion and a pivot point.     //A kind of exchange sort.     public class QuickSort     {         public static List<int> Execute(List<int> list)         {             // Return if nothing to sort             if ( list.Count <= 1 ) { return list ; }             // Find a random pivot point             var random = new Random();             var pivotIndex = random.Next(list.Count);             // Take out the pivot             var pivot = list[pivotIndex];             list.RemoveAt(pivotIndex);             // Lesser - Greater Lists             var lesser = new List<int>();             var greater = new List<int>();             // Sort around the pivot             foreach (var item in list)             {                 if ( item <= pivot )                 {                     lesser.Add(item);                 }              

Sort Algorithm in C# - Bubble Sort Explained

I did not like any of the examples of sorting algorithms in C# online. So, I decided to write it by myself with some good code, and not something which looks like it was written by people who have never done commercial software development before. Good code is readable and maintainable. You can quickly understand what the code does. This is the easiest sort algorithm. There are three variations of this algorithm: (1) The first one uses a while loop to go through the entire list as many times as even a single instance of unsorted items were found: public static void Type1 (List<int> list)         {             Console.WriteLine("Bubble Sorting");             var unsortedFound = false;             do             {                 unsortedFound = false;                 for (int i = 0; i < list.Count - 1; i++)                 {                     if ( list[i] > list[i + 1] )                     {                         var temp = list[i + 1];