public static void Sort<T>(T[] array) where T : IComparable
{
for (int i = 0; i < array.Length; i++)
{
for (int j = 0; j < array.Length - 1; j++)
{
if (array[j].CompareTo(array[j + 1]) > 0)
{
Swap(array, j, j + 1);
}
}
}
}
private static void Swap<T>(T[] array, int first, int second)
{
T temp = array[first];
array[first] = array[second];
array[second] = temp;
}
int[] arr = { 800, 11, 50, 771, 649, 770, 240, 9 };
int temp = 0;
for (int write = 0; write < arr.Length; write++) {
for (int sort = 0; sort < arr.Length - 1; sort++) {
if (arr[sort] > arr[sort + 1]) {
temp = arr[sort + 1];
arr[sort + 1] = arr[sort];
arr[sort] = temp;
}
}
}
for (int i = 0; i < arr.Length; i++)
Console.Write(arr[i] + " ");
Console.ReadKey();
// C# program for implementation
// of Bubble Sort
using System;
class GFG
{
static void bubbleSort(int []arr)
{
int n = arr.Length;
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - i - 1; j++)
if (arr[j] > arr[j + 1])
{
// swap temp and arr[i]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
/* Prints the array */
static void printArray(int []arr)
{
int n = arr.Length;
for (int i = 0; i < n; ++i)
Console.Write(arr[i] + " ");
Console.WriteLine();
}
// Driver method
public static void Main()
{
int []arr = {64, 34, 25, 12, 22, 11, 90};
bubbleSort(arr);
Console.WriteLine("Sorted array");
printArray(arr);
}
}
// This code is contributed by Sam007
using System;
public class Program {
//Bubble sort function
private static float[] BubbleSort(float[] Array){
bool Sorted = false;
while (!Sorted){
Sorted = true;
for (int i = 0; i < Array.Length-1; i++)
{
if (Array[i] > Array[i+1]){
//swap Array[i] and Array[i+1]
Sorted = false;
float Temp = Array[i+1];
Array[i+1] = Array[i];
Array[i] = Temp;
}
}
}
return Array;
}
public static void Main() {
float[] UnSortedList = {1,56,3,6,7,543,542,1,555,4,1091};
float[] SortedList = BubbleSort(UnSortedList);
//prints the Sorted array
for (int i = 0; i < SortedList.Length; i++)
{
Console.Write(SortedList[i] + " ");
}
Console.ReadKey();
}
}