Example of bubble short in c# asp.net
In Bubble short we start comparing from the first two adjacent element of the Array a for given order like Ascending or Descending for each pass.
Syntex
int i=0, temp;
pas 1 for i=0
Array[i]>Array[i+1];
if condition is true then swap
temp=Array[i];
Array[i]=Array[i+1];
Array[i]=temp;
then next pass...
Here I wrting a program in which I have taken defined Array with elements.
Simply it will short by Bubble short and print the resulted Array in Ascending order.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleDemoApp
{
class test
{
static void Main()
{
int[] NumArray = { 4, 69, 9, 83, 34, 45,11,17,55};
int temp;
Console.Write("Before Sorting : ");
Console.Write("\n");
for (int k = 0; k < NumArray.Length; k++)
Console.Write(NumArray[k] + " ");
Console.Write("\n");
for (int OuterPass = 1; OuterPass <= NumArray.Length - 2; OuterPass++)
{
// Inner loop
for (int i = 0; i <= NumArray.Length - 2; i++)
{
// condition check
if (NumArray[i] > NumArray[i + 1])
{
// Swap the values
temp = NumArray[i + 1];
NumArray[i + 1] = NumArray[i];
NumArray[i] = temp;
}
}
}
Console.WriteLine("The Sorted array");
foreach (int aa in NumArray)
Console.Write(aa + " ");
Console.ReadKey();
}
}
}
Output
And Here I am writing a program that will ask user to enter size of Array thel will to enter all elemets one by one. it will print the result after each pass and then final shorted Array.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleDemoApp
{
class test
{
static void Main()
{
Console.Write("\nBUBBLE SORT Program in c#");
Console.Write("\n\nEnter the total number of elements: ");
int max = Convert.ToInt32(Console.ReadLine());
int[] NumArray = new int[max];
for (int i = 0; i < max; i++)
{
Console.Write("\nEnter [" + (i + 1).ToString() + "] element: ");
NumArray[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("Before Sorting : ");
for (int k = 0; k < max; k++)
Console.Write(NumArray[k] + " ");
Console.Write("\n");
for (int i = 1; i < max; i++)
{
for (int j = 0; j < max - i; j++)
{
if (NumArray[j] > NumArray[j + 1])
{
int temp = NumArray[j];
NumArray[j] = NumArray[j + 1];
NumArray[j + 1] = temp;
}
}
Console.Write("After iteration " + i.ToString() + ": ");
for (int k = 0; k < max; k++)
Console.Write(NumArray[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(NumArray[i]);
Console.Write("\n");
}
Console.ReadKey();
}
}
}
Output
In Bubble short we start comparing from the first two adjacent element of the Array a for given order like Ascending or Descending for each pass.
Syntex
int i=0, temp;
pas 1 for i=0
Array[i]>Array[i+1];
if condition is true then swap
temp=Array[i];
Array[i]=Array[i+1];
Array[i]=temp;
then next pass...
Here I wrting a program in which I have taken defined Array with elements.
Simply it will short by Bubble short and print the resulted Array in Ascending order.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleDemoApp
{
class test
{
static void Main()
{
int[] NumArray = { 4, 69, 9, 83, 34, 45,11,17,55};
int temp;
Console.Write("Before Sorting : ");
Console.Write("\n");
for (int k = 0; k < NumArray.Length; k++)
Console.Write(NumArray[k] + " ");
Console.Write("\n");
for (int OuterPass = 1; OuterPass <= NumArray.Length - 2; OuterPass++)
{
// Inner loop
for (int i = 0; i <= NumArray.Length - 2; i++)
{
// condition check
if (NumArray[i] > NumArray[i + 1])
{
// Swap the values
temp = NumArray[i + 1];
NumArray[i + 1] = NumArray[i];
NumArray[i] = temp;
}
}
}
Console.WriteLine("The Sorted array");
foreach (int aa in NumArray)
Console.Write(aa + " ");
Console.ReadKey();
}
}
}
Output
And Here I am writing a program that will ask user to enter size of Array thel will to enter all elemets one by one. it will print the result after each pass and then final shorted Array.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleDemoApp
{
class test
{
static void Main()
{
Console.Write("\nBUBBLE SORT Program in c#");
Console.Write("\n\nEnter the total number of elements: ");
int max = Convert.ToInt32(Console.ReadLine());
int[] NumArray = new int[max];
for (int i = 0; i < max; i++)
{
Console.Write("\nEnter [" + (i + 1).ToString() + "] element: ");
NumArray[i] = Convert.ToInt32(Console.ReadLine());
}
Console.Write("Before Sorting : ");
for (int k = 0; k < max; k++)
Console.Write(NumArray[k] + " ");
Console.Write("\n");
for (int i = 1; i < max; i++)
{
for (int j = 0; j < max - i; j++)
{
if (NumArray[j] > NumArray[j + 1])
{
int temp = NumArray[j];
NumArray[j] = NumArray[j + 1];
NumArray[j + 1] = temp;
}
}
Console.Write("After iteration " + i.ToString() + ": ");
for (int k = 0; k < max; k++)
Console.Write(NumArray[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(NumArray[i]);
Console.Write("\n");
}
Console.ReadKey();
}
}
}
Output
very helpful code sir
ReplyDelete