OregonMike
BAN USER
This sample code uses some temp variables, aka "extra space"
using static ArraySorting.LastUpdatedSide;
namespace ArraySorting
{
public enum LastUpdatedSide { LeftSide = 0, RightSide = 1 };
public class ArraySort
{
public static void Sort01(int[] array)
{
if (array == null || array.Length == 1)
return;
if (array.Length == 2)
{
Swap(array, 0, 1);
return;
}
int left = -1;
int right = array.Length;
int index = right - 1;
LastUpdatedSide lastUpdatedSide = RightSide;
for (; index > left && index > -1; index--)
{
if (lastUpdatedSide == RightSide)
{
// Updating the left side...
Swap(array, index, ++left);
lastUpdatedSide = LeftSide;
}
else
{
// Updating the right side..
Swap(array, index, --right);
lastUpdatedSide = RightSide;
}
}
// At this point for even lengthed arrays, the majority of the Array
// should be sorted less its middle two elements.
if (array.Length % 2 == 0)
Swap(array, --right, ++left);
}
private static void Swap(int[] array, int indexA, int indexB)
{
// TODO: validate input
int temp = array[indexA];
array[indexA] = array[indexB];
array[indexB] = temp;
}
}
}
This sample code uses no temp variables, aka "extra space"
- OregonMike August 25, 2016