bubble sort in c# ?
bubble sort , one of simplest sorting algorithm for small app you write as a student ,
the reason why its not good for bigger app cause it is slow let me explain it to you
the amount of oration need to be done equals = (N-1)
it means if you have 1000 number your app will do 999 operation to sort the array or whatever
each two neighbor number will be compare together , here is the core of sorting algorithm
for (int i = 1; i < max; i++)
{
for (int j = 0; j < max - i; j++)
{
if (numarr[j] > numarr[j + 1])
{
int temp = numarr[j];
numarr[j] = numarr[j + 1];
numarr[j + 1] = temp;
}
}
but for educational reason here is a complete code for bubble sort algorithm
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace bubble_sort
{
class Program
{
static int BubbleSorting()
{
Console.Write("\nProgram for sorting using Bubble Sort Algorithm");
Console.Write("\n\nEnter the total number of elements: ");
int max = Convert.ToInt32(Console.ReadLine());
int[] numarr = new int[max];
for (int i = 0; i < max; i++)
{
Console.Write("\nEnter [" + (i + 1).ToString() + "] element: ");
numarr[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("Input int array : ");
for (int k = 0; k < max; k++)
Console.Write(numarr[k] + " ");
Console.Write("\n");
for (int i = 1; i < max; i++)
{
for (int j = 0; j < max - i; j++)
{
if (numarr[j] > numarr[j + 1])
{
int temp = numarr[j];
numarr[j] = numarr[j + 1];
numarr[j + 1] = temp;
}
}
Console.Write("Iteration " + i.ToString() + ": ");
for (int k = 0; k < max; k++)
Console.Write(numarr[k] + " ");
Console.Write("\n");
}
Console.Write("\n\nThe numbers in ascending orders are given below:\n\n");
for (int i = 0; i < max; i++)
{
Console.Write("Sorted [" + (i + 1).ToString() + "] element: ");
Console.Write(numarr[i]);
Console.Write("\n");
}
return 0;
}
static void Main(string[] args)
{
BubbleSorting();
Console.ReadLine();
}
}
}
the reason why its not good for bigger app cause it is slow let me explain it to you
the amount of oration need to be done equals = (N-1)
it means if you have 1000 number your app will do 999 operation to sort the array or whatever
each two neighbor number will be compare together , here is the core of sorting algorithm
for (int i = 1; i < max; i++)
{
for (int j = 0; j < max - i; j++)
{
if (numarr[j] > numarr[j + 1])
{
int temp = numarr[j];
numarr[j] = numarr[j + 1];
numarr[j + 1] = temp;
}
}
but for educational reason here is a complete code for bubble sort algorithm
:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace bubble_sort
{
class Program
{
static int BubbleSorting()
{
Console.Write("\nProgram for sorting using Bubble Sort Algorithm");
Console.Write("\n\nEnter the total number of elements: ");
int max = Convert.ToInt32(Console.ReadLine());
int[] numarr = new int[max];
for (int i = 0; i < max; i++)
{
Console.Write("\nEnter [" + (i + 1).ToString() + "] element: ");
numarr[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("Input int array : ");
for (int k = 0; k < max; k++)
Console.Write(numarr[k] + " ");
Console.Write("\n");
for (int i = 1; i < max; i++)
{
for (int j = 0; j < max - i; j++)
{
if (numarr[j] > numarr[j + 1])
{
int temp = numarr[j];
numarr[j] = numarr[j + 1];
numarr[j + 1] = temp;
}
}
Console.Write("Iteration " + i.ToString() + ": ");
for (int k = 0; k < max; k++)
Console.Write(numarr[k] + " ");
Console.Write("\n");
}
Console.Write("\n\nThe numbers in ascending orders are given below:\n\n");
for (int i = 0; i < max; i++)
{
Console.Write("Sorted [" + (i + 1).ToString() + "] element: ");
Console.Write(numarr[i]);
Console.Write("\n");
}
return 0;
}
static void Main(string[] args)
{
BubbleSorting();
Console.ReadLine();
}
}
}
Comments
Post a Comment