Skip to main content

Posts

Showing posts from March, 2012

How to implement bubble sort in c#

In this post,I am going to show you how to implement bubble sorting in c#.
What is Bubble Sort
Bubble sort, is a simple sorting algorithm that works by repeatedly stepping through the list to be sorted, comparing each pair of adjacent items and swapping them if they are in the wrong order.
Implementation


using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace SortingDemo { publicclass BubbleSort { publicvoid BubbleSort() { //Initialize array dataint[] data = { 5, 3, 6, 1, 8, 7, 2, 4 }; for (int outerIndex = 0; outerIndex < data.Length; outerIndex++) { for (int innerIndex = 0; innerIndex < data.Length-1; innerIndex++) { if (data[innerIndex] > data[innerIndex+1]) { //swap data. Swap(data, innerIndex); } } } } privatestaticvoid Swap(int[] dat…