So from what I understand Bubble sort should be slower at sorting reversed data than any other type. I am currently going through sorting algorithms and have implemented my own Bubble algorithm and it is about half as slow again at sorting random data and data with multiple values which are identical than reversed data (Timed with java's System.nanoTime()). I'm intrigued by this result and I can't account for it.
Here is the code for my algorithm:
public static void bubbleSort(int[] arr)
{
//Will only check elements which haven't been sorted yet. checks and newChecks will handle this.
int checks = arr.length, newChecks;
//Continues comparing elements until a swap does not occur, indicating a sorted list.
do
{
newChecks = 0;
for (int i = 1; i < checks; i++)
{
if (arr[i - 1] > arr[i])
{
swapElements(arr, i - 1, i);
//Gives newChecks the position of the element just switched
newChecks = i;
}
}
//The last switched element indicates after which point the array is sorted. Thus the assigning of newChecks to checks.
checks = newChecks;
} while (checks > 0);
}