In some of the bigger companies like google , Microsoft etc algorithm has been a favorite topic during c# interviews. So we are starting with a dedicatedseries which mainly targets how to write these algorithms in c#. So let’s start with the famous one bubble sort.
Bubble sort is a sorting algorithm which steps through a collection repeatedly, comparing and swapping until the collection gets sorted. Below is a simple pictorial representation of how bubble sort works. We have a simple number collection of values 5, 1, 3 and 4 and we would like to sort them in an ascending manner.
Step 1 :- So we first start with number 5. The adjacent number to 5 is 1. So we compare 5 with 1. Now 5 is greater than 1 so we swap position of 1 with 5 and 5 with 1. Look at Step 2 for the final image after swapping.
Step 2:- Now we compare 5 with 3. 5 is greater than 3 so we swap position of 5 with 3 and 3 with 5. This continues until 5 has been compared with all numbers in the list. Step 4 is the final position for number 5.
Now we take the second number i.e. 1 and we compare and swap 1 with all other numbers until it reaches its final position. This is done with all numbers in the collection.
Now to implement the above logic in c#it’s a two-step process.
- Loop through all elements in the collection.
Below is a sample code which loops through all elements in a list called as unsorted.
// Loop through all elements
for (int i = 1; i <= unsorted.Count; i++)
{
}
- For every element in the collection compare and swap with all the remaining elements.
Now for every element we need to loop through the rest of elements. In other words we need a nested for loop. The outer “for loop” loops through all elements and the inner “for loop” loops this element with other elements. Please note the loop runs till unsorted.Count-i.
// Loop through all elements
for (int i = 1; i <= unsorted.Count; i++)
// All elements compare / swap with rest of elements
for (int j = 0; j < unsorted.Count – i; j++)
{
// Compare and Swap logic goes here
}
Below is the compare and swap logic which goes in the inner loop.
if (unsorted[j] > unsorted[j + 1])
{
temp = unsorted[j];
unsorted[j] = unsorted[j + 1];
unsorted[j + 1] = temp;
}
Below is the full final code for bubble sort.
static public List<int> bubblesort(List<int> unsorted)
{
int temp;
for (int i = 1; i <= unsorted.Count; i++)
for (int j = 0; j < unsorted.Count – i; j++)
if (unsorted[j] > unsorted[j + 1])
{
temp = unsorted[j];
unsorted[j] = unsorted[j + 1];
unsorted[j + 1] = temp;
}
return unsorted;
}
Visit us at www.questpond.com for more .NET interview questions and answers
This is a nice c# interview question :- What are partial classes in c# ?
Pingback: Algorithm interview questions and answers: – How does selection sort algorithm works? | One stop place for c# ,.NET , SQL Server interview questions
I was wondering if you ever considered changing the layout of
your website? Its very well written; I love what youve
got to say. Buut maybe you could a little more in the way of content so
people could connect with it better. Youve goot an awful lott of text for only
having one or 2 pictures. Maybe you culd space it out better?