Bubble sort
Bubble sort
using System;
public class Bubble_Sort
{
public static void Main(string[] args)
{
int[] a = { 3, 0, 2, 5, -1, 4, 1 };
int t;
Console.WriteLine("Original array :");
foreach (int aa in a)
Console.Write(aa + " ");
for (int p = 0; p <= a.Length - 2; p++)
{
for (int i = 0; i <= a.Length - 2; i++)
{
if (a[i] > a[i + 1])
{
t = a[i + 1];
a[i + 1] = a[i];
a[i] = t;
}
}
}
Console.WriteLine("\n"+"Sorted array :");
foreach (int aa in a)
Console.Write(aa + " ");
Console.Write("\n");
}
}
Selection Sort
The selection sort algorithm sorts an array by repeatedly finding the minimum
element (considering ascending order) from unsorted part and putting it at the
beginning. The algorithm maintains two subarrays in a given array.
1) The subarray which is already sorted.
2) Remaining subarray which is unsorted.
In every iteration of selection sort, the minimum element (considering ascending
order) from the unsorted subarray is picked and moved to the sorted subarray.
Following example explains the above steps:
arr[] = 64 25 12 22 11
Insertion sort -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CommonInsertion_Sort
{
class Program
{
static void Main(string[] args)
{
int[] numbers = new int[10] {2, 5, -4, 11, 0, 18,
22, 67, 51, 6};
Console.WriteLine("\nOriginal Array Elements :");
PrintIntegerArray(numbers);
Console.WriteLine("\nSorted Array Elements :");
PrintIntegerArray(InsertionSort(numbers));
Console.WriteLine("\n");
}