I came across the following question while I was doing some exercise:
A sorting algorithm starts from start of the list, scan until two succeeding items that are in the wrong order are found. Swap those items and go back to the beginning. The algorithm ends when the end of the list is reached.
What is the worst-case running time for a list of size n?
I feel that it is similar with bubble sort, but probably worse that that because it doesn't finish the whole pass of scanning the list. But I can't figure out how to calculate its time complexity. I am not sure if the code I came up below for this algorithm is correct. Many thanks for your help!
for (int i=0, i<n , i++){//n is the size of the array
if (array[i]>array[i+1]){
swap (array[i], array[i+1]);
i=0;
}
}
