I have written a function to sort time stamps (hh:mm:ss) in order from oldest to newest. I am interested in knowing the approximate worst case running time of my code but i don't know how to determine that.
My rough guess is O(n-1)^2 because of nested for loop. Am i correct ?
If not, then can someone determine what would be the approximate running time of my code in Big O notation ?
public void sortTimeStamp(SortTime timestamps[])
{
for(int i=0;i<timestamps.length-1;i++)
{
for(int j=0;j<timestamps.length-1;j++)
{
if(timestamps[j].hour > timestamps[j+1].hour)
{
swap_timestamps(timestamps, j);
}
else
if(timestamps[j].hour == timestamps[j+1].hour)
{
if(timestamps[j].minutes > timestamps[j+1].minutes)
{
swap_timestamps(timestamps, j);
}
else
if(timestamps[j].minutes == timestamps[j+1].minutes && timestamps[j].seconds > timestamps[j+1].seconds)
{
swap_timestamps(timestamps, j);
}
}
}
}
}
Swap function
public void swap_timestamps(SortTime timestamps[], int index)
{
SortTime temp = timestamps[index];
timestamps[index] = timestamps[index+1];
timestamps[index+1] = temp;
}
for(int i=0;i<4;i++)? this means that your array will always have 4 elementd?O(n^2), assuming that your loops intend to iterate over the full length of theSortTimeobject/collection.