Object: Implement bubble sort in C# the value of array given by the users. C# Projects with Source Code.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace code
{
class sort
{
public void bubblesort(int[] ar)
{
int temp;
for (int i = (ar.Length-1); i >= 0; i–)
{
for (int j = 1; j <= i ; j++)
{
if (ar[j-1] > ar[j])
{
temp = ar[j-1];
ar[j-1] = ar[j];
ar[j] = temp;
}
}
}
Console.WriteLine(“Array After Sorting”);
for (int i = (ar.Length – 1); i >= 0; i–)
{
Console.WriteLine(ar[i]);
}
}
}
class Program
{
static void Main(string[] args)
{
sort s = new sort();
int num;
Console.WriteLine(“How Many No. of Array Elements You Enter:”);
num = Convert.ToInt32(Console.ReadLine());
int[] ar = new int[num];
Console.WriteLine(“Enter Elements here:”);
for (int i = 0; i < num; i++)
{
ar[i] = Convert.ToInt32(Console.ReadLine());
}
s.bubblesort(ar);
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace code
{
class sort
{
public void bubblesort(int[] ar)
{
int temp;
for (int i = (ar.Length-1); i >= 0; i--)
{
for (int j = 1; j <= i ; j++)
{
if (ar[j-1] > ar[j])
{
temp = ar[j-1];
ar[j-1] = ar[j];
ar[j] = temp;
}
}
}
Console.WriteLine("Array After Sorting");
for (int i = (ar.Length - 1); i >= 0; i--)
{
Console.WriteLine(ar[i]);
}
}
}
class Program
{
static void Main(string[] args)
{
sort s = new sort();
int num;
Console.WriteLine("How Many No. of Array Elements You Enter:");
num = Convert.ToInt32(Console.ReadLine());
int[] ar = new int[num];
Console.WriteLine("Enter Elements here:");
for (int i = 0; i < num; i++)
{
ar[i] = Convert.ToInt32(Console.ReadLine());
}
s.bubblesort(ar);
Console.ReadKey();
}
}
}
Output:
Another C# Program: C# Program Calculating the Area of a Rectangle
Another C# Program: C# Program that Subtracts two user-defined numbers
Another C# Program: C# program to count vowels in a string
Another Program: Write a C# program that reads two arrays and checks whether they are equal.
Another Program: Develop a C# app that will determine the gross pay for each of three employees
Another Program: Write a C# program of throwing an exception when dividing by zero condition occurs
Pingback: C# how to handle exceptions to illustrate try-catch action with Exceptions