Bubble sort

The third sorting algorithm presented in the book is bubble sort. Its way of operation is very simple, because the algorithm just iterates through the array and compares adjacent elements. If they are located in an incorrect order, they are swapped. It sounds very easy, but the algorithm is not very efficient and its usage with large collections could cause performance-related problems.

To better understand how the algorithm works, let's take a look at the following diagram that shows how the algorithm operates in the case of sorting a single-dimensional array with nine elements (-11, 12, -42, 0, 1, 90, 68, 6, -9):

As you can see, ...

Get C# Data Structures and Algorithms now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.