Average case complexity analysis of insertion and bubble sort - algorithm

This site already has some questions on this topic but I am confused after reading some of the answers.
https://cs.stackexchange.com/questions/20/evaluating-the-average-time-complexity-of-a-given-bubblesort-algorithm
In the above link, answer by "Joe" says that number of swaps in bubble sort on average is same as number of inversions on average which is (n)(n-1) / 4.
However, Insertion sort vs Bubble Sort Algorithms says that in bubble sort average number of swaps is n^2 /2 and in insertion sort it is n^2/4 and that's the reason for insertion sort being better than bubble sort.
Which one is correct? Can someone please help me?

Your first link counts the expected number of inversions (i.e. swaps) assuming a uniform distribution.
Your second link is counting iterations i.e. inspections of elements.
Both are correct.

Related

Best sorting method for this?

Question;
You have to sort an array of bank transactions by date. Most of them
are in order (by date), only a few are out of order.
Which sorting algorithm will you use between insertion sort, selection
sort and merge sort in order to take advantage of the fact that the
array is almost sorted?
My answer (not sure if its correct)
Assuming that N >= 5, i would go with Merge Sort since its avg. time complexity would be O(n * log n) which would be more efficient than insertion sort O(n^2). However, since multiple transactions will be on the same dates, insertion sort would be a good STABLE sorting method.
Which one is better in this case? Merge or insertion sort? Am i in the right direction?
You should pick insertion sort, not because of stability, but because it's adaptive (see here) and will outperform due to the fact the input is almost sorted to begin with.
The meaning of this "adaptive" characteristic is that elements that are already in place are processed at O(1) time, and elements very close to their sorted position can also be considered O(1) (up to some k distance).

The complexity of bubble sort and insertion sort for a list with a given number of inversions

Let the length of a list be n, and the number of inversions be d. Why does insertion sort run in O(n+d) time and why does bubble sort not?
When I consider this problem I am thinking of the worst case scenario. Since the worse case for inversions is n(n-1)\2, both bubble and insertion sort run in the same time. But then I don't know how to answer the question since I find them the same. Can someone help me with this?
For bubble sort, if the last element needs to get to the first position (n inversions) you need to loop over the entire array n times, each time moving the element one position forward so you get n^2 steps, so you get O(N^2) regardless of the value of d.
The same setup in insertion sort will do only n+n steps to get everything sorted (O(N+d)). d is actually the total number of swaps insertion sort will need to do to get the thing sorted.
You went wrong when you assumed the worst case value of d is n(n-1)/2. While this is true, if you want to express the complexity in terms of d you can't replace it with it's worst value case, unless you're ok with a higher bound.

Explaining average time complexity of Bubble Sort [duplicate]

This question already has answers here:
Why is bubble sort O(n^2)?
(6 answers)
Closed 7 years ago.
I know that bubble sort has average time complexity O(n^2). Can anyone explain how to calculate this complexity? I usually find just people saying this is the average complexity but I don't know why. (In other words what is the average complexity for random permutation of numbers from 1 to n)
If the complexity is O(n^2), that would suggest that an algorithm must perform some operation on every combination of two elements of the input.
First of all, note that Bubble Sort compares adjacent items and swaps them over if they are out of order.
In the best case Bubble Sort is O(n). This is when the list is already sorted. It can pass over the list once and only needs to compare each item once with its neighbour before establishing that it is already sorted.
O(n^2) is the worst case for Bubble Sort. This is when the input list is already reverse sorted. Think about how the algorithm moves the first item from index 0 to it's sorted position at index n-1. It will have to compare that item to every other item once (n operations). It repeats this process with every item, hence O(n^2).

appropriate sorting algorithm

I am a little unsure of my answer to the question below. Please help:
Suppose you are given a list of N integers. All but one of the integers are sorted in numerical order. Identify a sorting algorithm which will sort this special case in O(N) time and explain why this sorting algorithm achieves O(N) runtime in this case.
I think it is insertion sort but am not sure why that is the case.
Thanks!!
Insertion sort is adaptive, and is efficient for substantially sorted data set. It can sort almost sorted data in O(n+d) where d is number of inversions and in your case d is 1.

Running time of counting and radix sort

I know that counting and radix sorts are generally considered to run in O(n) time, and I believe I understand why. I'm being asked in an assignment, however, to explain why these sorts may not necessarily sort a list of distinct, positive integers in O(n) time. I can't come up with any reason.
Any help is appreciated!
To say that counting or radix sort is O(n) is actually not correct.
Counting sort is O(n+k) where k is the maximum value of any element in the array.
The reason is the you have to step through the entire list to populate the counts (O(n)), then step through the counts (O(k)).
Radix sort is O(mn) where m is the maximum number of digits of a number.
The reason is that you have to step through the array once for each digit (O(n) m times).

Resources