Ok so I have an array which contains 6 values for the amount of customers signed up for each month of a 6 month period(jan-jun). Lets say CustomerSavedByMonth[i]. Now these are in order by month, so at index 0 there is the customers for january, 1 febuary etc.
I would like to sort these using a bubble sort so I can display the month with the highest customers. However when I do bubble sort them they will lose there context, as in now they dont have a specific month they are just in order. I dont really know how better to explain it so I hope that makes sense. How would I go about making sure that when its sorted I will still know what month was what number? I apologise if this is a stupid question and whatnot but I just cant figure out how to approach it. Thanks
A simple way of approaching this is to set up an index array, and then you can sort it having the comparison done on values of the corresponding items in the data array, in that manner you won't lose track of the month.
A simple C# implementation of the above would be as follows:
int[] CustomerByMonth = { 60, 50, 40, 30, 20, 10 };
int[] index = { 0, 1, 2, 3, 4, 5 };
int tmp = 0;
for (int i = 0; i < index.Length; ++i)
{
for (int j = 0; j < index.Length - 1; ++j)
{
if (CustomerByMonth[index[j]] > CustomerByMonth[index[j + 1]])
{
tmp = index[j + 1];
index[j + 1] = index[j];
index[j] = tmp;
}
}
}
// Display month number and customer amount for month with highest amount of customers
Console.Write(index[CustomerByMonth.Length - 1] + " : " + CustomerByMonth[index[CustomerByMonth.Length - 1]]);