Algorithm interview questions and answers: – Can you write code for bubble sort algorithm?

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.

Sorted change

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# ?

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.

2 Responses to Algorithm interview questions and answers: – Can you write code for bubble sort algorithm?

  1. Pingback: Algorithm interview questions and answers: – How does selection sort algorithm works? | One stop place for c# ,.NET , SQL Server interview questions

  2. 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?

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