Programming Alogorithm Series: Bubble Sort

using System;

namespace BubbleSort

{

class Program

{

static void Main(string[] args)

{

int[] array = new int[] {2,5,7,1,9,0,3,4,2,6,7,5};

// Before sorting

for (int i = 0; i < array.Length - 1; i++)

{

Console.Write(array[i]);

}

Console.ReadLine();

ArraySort.SortArray(array);

// After sorting

for (int i = 0; i < array.Length - 1; i++)

{

Console.Write(array[i]);

}

Console.ReadLine();

}

}

class ArraySort

{

public static void SortArray(int[] numbers)

{

int tempInt;

for (int i = (numbers.Length-1); i >=0; i--)

{

for (int j =1; j <=i; j++)

{

if (numbers[j-1] > numbers[j])

{

tempInt = numbers[j-1];

numbers[j-1] = numbers[j];

numbers[j] = tempInt;

}

}

}

}

}

}

Published Tuesday, September 19, 2006 12:00 AM by sanjeebsarangi
Filed under:

Comments

Thursday, September 20, 2007 4:10 PM by Tosa

# re: Programming Alogorithm Series: Bubble Sort

This is really a brilliant way to create and use a Sort class.

However, there was a bug in the code

instead of:

for (int i = 0; i < array.Length - 1; i++)

this is more accurate

for (int i = 0; i < array.Length; i++)

because '<' is used.

Leave a Comment

(required) 
(required) 
(optional)
(required)