-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShuffleList.cs
More file actions
36 lines (34 loc) · 984 Bytes
/
ShuffleList.cs
File metadata and controls
36 lines (34 loc) · 984 Bytes
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
using System;
using System.Collections.Generic;
namespace DA.Algorithms.Problems
{
public static class ShuffleList
{
/// <summary>
/// An array [a1, a2, a3, a4, b1, b2, b3, b4]
/// to convert into [a1, b1, a2, b2, a3, b3, a4, b4]
/// </summary>
/// <param name="array"></param>
public static void ShuffleArray (char[] array)
{
int halfLength = array.Length / 2;
for (int i = 1; i < halfLength; i++)
{
for (int j = 0; j < i; j++)
{
int index = halfLength - i + 2 * j;
Swap (ref array[index], ref array[index + 1]);
}
}
}
/// <summary>
/// Swap two elements.
/// </summary>
public static void Swap<T> (ref T first, ref T second)
{
T temp = first;
first = second;
second = temp;
}
}
}