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] )       ...

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;                 ...

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        ...

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++)             {     ...

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 - ...

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;          ...