-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOddEvenSort.cs
59 lines (53 loc) · 1.52 KB
/
OddEvenSort.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
namespace Rextester
{
public static class Program
{
public static void Main(string[] args)
{
new[] {7, 9, 8, 7, 3, 15, 6, 6, 4, 3}.Sort().Flatten().Output();
}
static int[] Sort(this int[] array)
{
var sorted = false;
while(!sorted)
{
sorted = true;
for(var i = 1; i < array.Length-1; i += 2)
{
if(array[i] > array[i+1])
{
Swap(array, i, i+1);
sorted = false;
}
}
for(var i = 0; i < array.Length-1; i += 2)
{
if(array[i] > array[i+1])
{
Swap(array, i, i+1);
sorted = false;
}
}
}
return array;
}
static void Swap(int[] array, int i, int j)
{
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
static string Flatten(this int[] array)
{
return array.Select(x => x.ToString()).Aggregate((acc, x) => acc + ";" + x);
}
static void Output(this string output)
{
Console.WriteLine(output);
}
}
}