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

About C#.NET, ASP.NET MVC Core, Angular, Azure, (MSBI)Business Intelligence, Data Science - Python 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. ASP.NET MVC Interview Questions and Answers:- https://youtu.be/pXmMdmJUC0g C# Interview Questions and Answers:- https://youtu.be/BKynEBPqiIM Angular Interview Questions and Answers:- https://youtu.be/-jeoyDJDsSM C# tutorial for beginners(4 hrs.):- https://youtu.be/AxEGRBFwlmI Learn Azure Step by Step:- https://youtu.be/wdUK7bCMXqs Azure AZ-900 fundamentals certification :- https://youtu.be/hC9iGgJorz8 AZ- 204 certification Azure:- https://youtu.be/qI8PRn2C080 Learn Angular tutorial step by step https://tinyurl.com/ycd9j895 Learn MVC 5 step by step in 16 hours:- https://youtu.be/Lp7nSImO5vk Learn Design Pattern Step by Step https://goo.gl/eJdn0m Learn MSBI Step by Step in 32 hours:- https://goo.gl/TTpFZN Learn SQL Server Step by Step http://tinyurl.com/ja4zmwu Python Tutorial for Beginners:- https://youtu.be/KjJ7WzEL-es Learn Data Science in 1 hour :- https://tinyurl.com/y5o7qbau Learn Power BI Step by Step:- https://tinyurl.com/y6thhkxw Learn Tableau step by step :- https://tinyurl.com/kh6ojyo
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 comment