using
System;
using
System.Collections.Generic;
using
System.Text;
namespace sorteringAfObjekter
{
class
Program
{
static void Main(string[] args)
{
Sorter s = new Sorter();
}
}
class
Sorter
{
Bil[]
biler = new Bil[6];
public
Sorter()
{
biler[0] = new Bil(6,
"Karl");
biler[1] = new Bil(5,
"Peter");
biler[2] = new Bil(4,
"Jens");
biler[3] = new Bil(3,
"Allan");
biler[4] = new Bil(2,
"Hans");
biler[5] = new Bil(1,
"Ole");
udskriv(biler);
viSorterer(biler);
udskriv(biler);
}
public void
viSorterer(Bil[] arr)
{
Console.WriteLine();
Console.WriteLine("--------Bobbel Sort start--------------");
Boolean found = false;
int operations = 0;
for (int i = 0; i < arr.Length
- 1; i++)
{
operations++;
if
(arr[i].tal > arr[i + 1].tal)
{
Bil
tmp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = tmp;
found = true;
}
if
(i == arr.Length - 2 && found == true)
{
found = false;
i = -1;
}
}
Console.WriteLine("
Antal iterationer: " + operations);
Console.WriteLine("--------Bobbel
Sort slut--------------");
Console.WriteLine();
}
public void
udskriv(Bil[] biler)
{
Console.WriteLine("------Udskriv
start------");
for (int i
= 0; i < biler.Length; i++)
{
Console.WriteLine(biler[i].tal
+ " " +
biler[i].navn);
}
Console.WriteLine("------Udskriv
slut------");
}
}
class Bil
{
public int
tal;
public string
navn;
public
Bil(int t, String n)
{
tal = t;
navn = n;
}
}
}
|