So I am creating a function that will return an array of indexes sorted by there value. I have written the code but it only works for a max array size of 8. I would like the method to be able to sort any array size. My code as of right now works great but I feel it could be greatly improved, there is a of code being duplicated. I am just not sure how I can optimize the code.
[source]
private int[] ArrayShift(int[] array,int startingPoint)
{
for (int i = startingPoint; i > 0; --i)
{
array = array[i - 1];
}
return array;
}
private int[] ArrayMaxValue(int[] array)
{
int[] arrayRanks = new int[8] {-1,-1,-1,-1,-1,-1,-1,-1};
int[] stack = new int[8];
for (int Index = 0; Index < array.Length; Index++)
{
if (array[Index] > 0)
{
if (array[Index] >= array[stack[0]])
{
stack=ArrayShift(stack,7); //shift the array down from highest index;
stack[0] = Index;
arrayRanks = ArrayShift(arrayRanks,7);
arrayRanks[0] = array[Index];
continue;
}
if ((array[Index] <= arrayRanks[0]) && (array[Index] >= arrayRanks[1]))
{
stack = ArrayShift(stack, 6);
stack[1] = Index;
arrayRanks = ArrayShift(arrayRanks, 6);
arrayRanks[1] = array[Index];
continue;
}
if ((array[Index] <= arrayRanks[1]) && (array[Index] >= arrayRanks[2]))
{
stack = ArrayShift(stack, 5);
stack[2] = Index;
arrayRanks = ArrayShift(arrayRanks, 5);
arrayRanks[2] = array[Index];
continue;
}
if ((array[Index] <= arrayRanks[2]) && (array[Index] >= arrayRanks[3]))
{
stack = ArrayShift(stack, 4);
stack[3] = Index;
arrayRanks = ArrayShift(arrayRanks, 4);
arrayRanks[3] = array[Index];
continue;
}
if ((array[Index] <= arrayRanks[3]) && (array[Index] >= arrayRanks[4]))
{
stack = ArrayShift(stack, 3);
stack[4] = Index;
arrayRanks = ArrayShift(arrayRanks, 3);
arrayRanks[4] = array[Index];
continue;
}
if ((array[Index] <= arrayRanks[4]) && (array[Index] >= arrayRanks[5]))
{
stack = ArrayShift(stack, 2);
stack[5] = Index;
arrayRanks = ArrayShift(arrayRanks, 2);
arrayRanks[5] = array[Index];
continue;
}
if ((array[Index] <= arrayRanks[5]) && (array[Index] >= arrayRanks[6]))
{
stack = ArrayShift(stack, 1);
stack[6] = Index;
arrayRanks = ArrayShift(arrayRanks, 1);
arrayRanks[6] = array[Index];
continue;
}
if ((array[Index] <= arrayRanks[6]) && (array[Index] >= arrayRanks[7]))
{
stack[7] = Index;
arrayRanks[7] = array[Index];
continue;
}
}
}
return stack;
}
[/source]
Thanks for the help.
agisler