I made some tests on BubbleSort algorithm. I firstly entered sorted array then reversely sorted array then randomly sorted array. I run the code for 10000, 100000, and 1000000 input sizes
Here are the results for 1000000 input size:
Sorted array: Actual running time = 304618 ms.
Reversely sorted array: Actual running time = 742047 ms.
Randomly sorted array: Actual running time = 1375477 ms.
code :
void BubbleSort (int A[], int size)
{
int i, j;
for (i=0; i<n-1; i++)
for (j=n-1; j>i; j--)
if (A[j]<A[j-1])
swap (A[j], A[j-1]);
}
Why does randomly sorted data take more time than reversely sorted data ???