Hi I'm taking a lecture about Data Structure and Algorithm in my college. I'm doing assignment about analyzing sorting algorithm. The assignments require the report that includes measuring run time of algorithm. TA gave us the three datasets of 30000 integers.(ascending order, descending order, random order)
I thought sorting descending order data would take more than sorting randomly ordered data. But in my bubble sort algorithm, the result is opposite.
It takes 2.453s real time and 2.409s user time to sort the numbers in descending order, and 3.217s real time and 3.159s user time to sort the numbers in random order. This result is also about my Selection Sort Algorithm. Isn't the descending ordered number worst case?
//file is opened at main function
int* bubble_prj4(FILE *fp_in)
{
int i, j, temp;
//arr is declared in header file
arr = (int*)malloc(sizeof(int) * 30000);
fread(arr, sizeof(int), 30000, fp_in);
for(i = 0; i < 29999; i++)
for(j = 29999; j > i; j--)
if(arr[j] < arr[j - 1])
{
temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
}
return arr;
}
It's my first time to ask question here. I don't know I'm doing it in right way.