Bubble sorting

This commit is contained in:
Alex Thomassen 2018-12-13 14:21:34 +01:00
parent 1b01ebe03f
commit 260dc58b77

View File

@ -1,4 +1,5 @@
using System; using System;
using System.CodeDom.Compiler;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
@ -13,23 +14,57 @@ namespace ClassDemo1
//var morDi = new Person(154, 300, "svart"); //var morDi = new Person(154, 300, "svart");
//Console.WriteLine(morDi.Description("Mordi")); //Console.WriteLine(morDi.Description("Mordi"));
var tall = new[] { 5, 10, 15, 20, 25 }; var tall = new[] { 30, 27, 71, 100, 22, 1, 5, 92, 57, 13 };
SkrivUtVersjonEn(tall); var sortertTall = SortMyBitchUp(tall, reverse: false);
SkrivUtVersjonTo(tall);
SkrivUtVersjonTre(tall);
int sum = SumMyBitchUp(420, 69); SkrivUtVersjonEn(sortertTall);
Write($"Sum: {sum}"); //SkrivUtVersjonTo(tall);
int nyVerdi = SumMyBitchUp(sum, 45678); //SkrivUtVersjonTre(tall);
Write($"Ny verdi: {nyVerdi}");
int allMyBitches = SumAllMyBitchesUp(tall); //int sum = SumMyBitchUp(420, 69);
int allMyBitchesV2 = SumAllMyBitchesUpV2(tall); //Write($"Sum: {sum}");
Write($"DÆTTA ER ALLI SOMMA LAGT SAMMEN, TO FORSKJELLIGE GANGER: {allMyBitches} & {allMyBitchesV2}"); //int nyVerdi = SumMyBitchUp(sum, 45678);
//Write($"Ny verdi: {nyVerdi}");
//int allMyBitches = SumAllMyBitchesUp(tall);
//int allMyBitchesV2 = SumAllMyBitchesUpV2(tall);
//Write($"DÆTTA ER ALLI SOMMA LAGT SAMMEN, TO FORSKJELLIGE GANGER: {allMyBitches} & {allMyBitchesV2}");
Console.ReadLine(); Console.ReadLine();
} }
/// <summary>
/// Bubble sorting
/// </summary>
/// <param name="bitches"></param>
/// <param name="reverse"></param>
/// <returns></returns>
static int[] SortMyBitchUp(int[] bitches, bool reverse = false)
{
int temp;
for (int i = bitches.Length; i >= 1; i--)
{
for (int x = 0; x < i - 1; x++)
{
if (bitches[x] < bitches[x + 1])
{
continue;
}
temp = bitches[x + 1];
bitches[x + 1] = bitches[x];
bitches[x] = temp;
}
}
if (reverse)
{
bitches = bitches.Reverse().ToArray();
}
return bitches;
}
static int SumAllMyBitchesUp(int[] bitches) static int SumAllMyBitchesUp(int[] bitches)
{ {
return bitches.Sum(); return bitches.Sum();