## Friday, 30 December 2016

Selection Sort Algorithm

Selection sort algorithm is use for sorting an array where this loop from the start of the first, and check through other elements to find the minimum value in an aarray.
After the end of the first loop,the minimum value is swapped with the current element.
The iteration then continues from the 2nd element and so on.

Algorithm
Step 1 − Set MIN to location 0
Step 2 − Search the minimum element in the list
Step 3 − Swap with value at location MIN
Step 4 − Increment MIN to point to next element
Step 5 − Repeat until list is sorted

Selection Sorting Code Using C# Language

```class Lab
{
static void Main(string[] args)
{
int[] myArray = new int { 120, 60, 30, 50, 10, 70, 90, 70};
int Size= myArray.Lenght;
Console.WriteLine("Before Selection algo the Array is: ");
for (int k = 0; k < Size; i++)
{
Console.WriteLine(myArray[i]);
}
int tmp, min_key;

for (int j = 0; j < Size - 1; j++)
{
min_key = j;

for (int k = j + 1; k < Size; k++)
{
if (myArray[k] < myArray[min_key])
{
min_key = k;
}
}

tmp = myArray[min_key];
myArray[min_key] = myArray[j];
myArray[j] = tmp;
}

Console.WriteLine("The Array After Selection Sort is: ");
for (int i = 0; i < 10; i++)
{
Console.WriteLine(myArray[i]);
}
}
}
```

Complexity Analysis of Selection Sorting
```Worst Case Time Complexity : O(n2)
Best Case Time Complexity : O(n2)
Average Time Complexity : O(n2)
Space Complexity : O(1)
```