Bubblesort
Kleines Snippet wie man unter C-Sharp den Bubblesort-Algorithmus implementiert.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int[] liste = new int[] { 6, 3, 5, 7, 1, 2, 4 };
bool sortiert = false;
while (!sortiert)
{
int temp1, temp2;
bool tauschaktion = false;
for (int i = 0; i < liste.Length; i++)
{
if (i > 0)
{
temp1 = liste[i - 1];
temp2 = liste[i];
if (temp1 > temp2)
{
liste[i - 1] = temp2;
liste[i] = temp1;
tauschaktion = true;
}
}
if (i == liste.Length-1 && !tauschaktion)
{
sortiert = true;
}
}
};
foreach (int zahl in liste)
{
Console.WriteLine(zahl);
}
Console.ReadLine();
}
}
}