Implementing The Sorts Algorithms In C#

In this article I want to show an implementation I made about the sorts algorithms, the most known widely are: Burble, SelectionSort, InsertionSort, MergeSort and QuickSort.

The fastest are the QuickSort and MergeSort and unlike the others they use recursives calls and split the arrays to gain betters performance while sorting.

Below I add a brief definition about each one:

Bubble: This is a simple sorting algorithm, it start at the beginning of the data set and compares the first two elements and if the second is lower than the first it swaps them. The process continues comparing each adjacent element until the dataset’s end. The worst case performance of this algorithm is O(x^2).

Selection: This is a simple sorting algorithm focus on an in-place comparison sort, looking the lowest element and moving to its corresponding place at the array. Its worst performance is O(x^2) making it inefficient with large arrays.

Insertion: This is a simple sorting algorithm  that is relatively efficient for small lists and mostly sorted lists, It works by taking elements from the list one by one and inserting them in their correct position into a new sorted list

MergeSort: Takes advantage of the ease of merging already sorted lists into a new sorted list. It starts by comparing every two elements (i.e., 1 with 2, then 3 with 4...) and swapping them if the first should come after the second. It then merges each of the resulting lists of two into lists of four, then merges those lists of four, and so on; until at last two lists are merged into the final sorted list. This is the first that scales well to very large lists, because its worst-case running time is O(n log n). It is also easily applied to lists, not only arrays, as it only requires sequential access, not random access. However, it has additional O(n) space complexity, and involves a large number of copies in simple implementations.

QuickSort: Quicksort is a divide and conquer algorithm which relies on a partition operation: to partition an array an element called a pivot is selected. All elements smaller than the pivot are moved before it and all greater elements are moved after it. This can be done efficiently in linear time and in-place. The lesser and greater sublists are then recursively sorted. This yields average time complexity of O(n log n), with low overhead, and thus this is a popular algorithm. Efficient implementations of quicksort (with in-place partitioning) are typically unstable sorts and somewhat complex, but are among the fastest sorting algorithms in practice. Together with its modest O(log n) space usage, quicksort is one of the most popular sorting algorithms and is available in many standard programming libraries.

In this link you have the code example for each sorting algorithm: https://github.com/edgarleonardo/SortAlgorithms

Comments

Popular posts from this blog

WebSockets Implementation With SignalR And Xamarin.Forms

Creating A Push Notification Server with ASP.NET And Xamarin.Form App With Firebase Push Notification, Refit, And LiteDB - Part 1 (Firebase Configuration)

CDN And Cloud Storage On Azure