This is simple program to demonstrate the bubble sort algorithm implementation, Please feel free to send us email or comment to this post in case you have any questions.
Code for Bubble Sort:
using System;
using System.Text;
using System.Collections;
using System.Data;
namespace Console_App
{
public class clsBubbleSort
{
public static void Main()
{
try
{
int[] SortArray = new int[200];
Console.WriteLine("Program for Bubble Sort");
Console.WriteLine("Enter number of elements of Sorting array?");
int NumberCount = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(" ");
Console.WriteLine("\nEnter integer Sorting array elements \n");
for (int i = 0; i < NumberCount; i++)
{
SortArray[i] = Convert.ToInt32(Console.ReadLine());
}
int Secondlimit= NumberCount-1;
for (int q = 0; q < NumberCount - 1; q++)
{
for (int j = 0; j < Secondlimit - q; j++)
{
if (SortArray[j] > SortArray[j + 1])
{
int k = SortArray[j];
SortArray[j] = SortArray[j + 1];
SortArray[j + 1] = k;
}
}
}
Console.WriteLine(" ");
Console.WriteLine("Bubble Sort Results: ");
for (int j = 0; j < NumberCount; j++)
Console.WriteLine(SortArray[j]);
}
catch (Exception ex)
{
//handle exception here
}
Console.ReadLine();
}
}
}
If you want to understand more about bubble sort, Please visit the Wikipedia link for more details:
No comments:
Post a Comment