Tuesday, May 21, 2013

Sort Algorithms (C#)

Quick sort


The quick sort function starts its work by selecting a random value from the unsorted list. This value is called the pivot value. Then we remove the pivot value from the unsorted list.
Now each element x in the unsorted list is compared with this pivot. We have two other lists 'Less' and 'Greater'.
if any element is less than the pivot, it is placed in the 'Less' list and if it is greater than the pivot value, it is placed in the 'Greater' list. Then we call the concat function with three arguments in it.
1.List name 'Less'
2.Variable Pivot
3.List Name 'Greater

Using System;
Using System.Collection.Generic;
Using System.Linq;
Using System.Text;

namespace SortAlgorithms
{
class Quicksort
{
public List QuickSort1(List a)
{
List Less = new List();
List Greater = new List();
if (a.Count<=1)
return a;
Random R = new Random();
int pos = R.Next(0,a.Count);
int pivot = a[pos];
a.RemoveAt(pos);
foreach(int i in a)
{
if  (i < pivot)
{
Less.Add(i);
}
else
{
Greater.Add(i);
}
}
return Concat(QuickSort1(Less), pivot, QuickSort1(Greater);

 

/* The concat function here performs a very important role, the quicksort algorithm uses the recursive approach throgh concat function. 
Here concat function receives the three arguments, two lists and pivot value.  
We have defined a new list of integers here name sorted.  
It concats the less, pivot and greater all together in sorted list and then returns this sorted list to quicksort method.  
The quicksort method receives this sorted list and then returns the sorted list to the function  
which sends the unsorted list to quicksort method.*/
 
public List Concat(List Less, int pivot, List Greater)
{
List Sorted = new List(Less);
Sorted.Add(pivot);
foreach (int g in Greater)
{
Sorted.Add(g);
}
return Soretd;
}
}
}
------------------------------------------------------------------------------------------------------------

Merge Sort


Merge Sort is able to rearrange elements of a list in ascending order. Merge sort works as a divide and conquer algorithm. It recursively divide the list into two halves until one element left, and merge the already sorted halves into a sorted one list.
------------------------------------------------------------------------------------------------------------

Bubble Sort



In bubble sort, each element of the unsorted list is compared to the next element and if the value of first element is greater than the value of the second element, then they are swapped. The same is applied again and again until every element gets compared with the whole list.
The result may have ascending or descending sorted elements.The bubble sort consists on the passes. The number of passes depends on the number of elements in the list. More elements means more passes.
It consists of the two loops - a main loop and a nested loop. The main loop describes the number of passes and the nested loops defined the number of comparisons.
The bubblesort is easy to implement but it is a slow sorting algorithm. It traverses each and every element of the unsorted list in every case. That's why it has the worst case and average case complexity as 0(n2).

using System;
using System.Collection.Generic;
using System.Linq;
using System.Text;

namespace SortAlgorithms
{
class BubbleSort
{
public List BubbleSort1(List Input)
{
int temp;
for (int i =1; i < Input.Count; i++)
{
for (int j=0; j< Input.count - 1; j++)
{
if (Input[j] > Input [j+1])
{
temp =Input[j];
Input[j] = Input [j+1];
Input[j+1] = temp;
}
}
}
return Input;
}
}
}
 
 
 

 

No comments: