Algorithm interview questions and answers: – How does selection sort algorithm works?

Selection sort is the most simples sorting algorithm.  It finds the lowest value from the collection and moves it to the left. This is repeated until the complete collection is sorted.

Below is a pictorial representation of how selection sort works. We have simple collection down below which has 5, 7, 1, and 0 values.

Step 1:- Find the smallest value from the collection. In the current collection the smallest value is “0”. Move this smallest value to the left hand side.

Step 2 :- Iterate again to find the next smallest value. The next smallest value is “1”. Move this value to the left hand side before the first smallest value.

In this manner continue with all elements in a collection and your list is sorted.

sr

Below is a simple c# code for selection algorithm.

// array of integers to hold values

  private static int[] a = new int[4]{2,8,0,3};

static void Main(string[] args)

{

Sort();

foreach (int temp in a)

{

Console.WriteLine(temp);

}

Console.ReadLine();

}

public static void Sort()

{

int i, j;

int min, temp;

for (i = 0; i < a.Count() – 1; i++) // Loop through each element

{

min = i; // Assume that he is the smallest

for (j = i + 1; j < a.Count(); j++) // Loop through the remaining element

{

if (a[j] < a[min]) // If the current value smaller than the min

{

min = j; // Swap the values

}

}

temp = a[i]; // Store the current value in temp variuable

a[i] = a[min]; // Swap the minimum  value to the current position

a[min] = temp; // Swap the current value to the minimum value position

}

}

Also read inserted algorithm interview questions and answers  from https://dotnetinterviewquestion.wordpress.com/category/inserted-algorithm-interview-question/

Also read Bubble sort algorithm interview questions and answers from https://dotnetinterviewquestion.wordpress.com/2013/03/30/net-interview-questions-and-answers-can-you-write-code-for-bubble-sort/

You can also see c# interview questions and answers from www.questpond.com

This question is taken from the book c# interview question book written by Shivprasad koirala. You can buy the book from these shops à https://dotnetinterviewquestion.wordpress.com/buy-interview-question-books-from-our-shops/

Advertisements

About c# and .NET Interview questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. Learn MVC in 16 hours:- https://www.youtube.com/watch?v=Lp7nSImO5vk Learn AngularJS Step by Step:- https://www.youtube.com/watch?v=0kmdjqgO9IY Learn Design Pattern in 8 hours:- https://www.youtube.com/watch?v=YDobmucohqk Learn C# and .NET in 60 days:- https://www.youtube.com/watch?v=yh2SrzCkNQA Learn MSBI in 32 hours:- https://www.youtube.com/watch?v=mGPJx3ocFgg Learn SharePoint Step by Step in 8 hours:- https://youtu.be/C2fW76SwJNU
This entry was posted in Uncategorized and tagged , , , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s