Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QuickSort implementation on undirected graph #14

Open
st235 opened this issue Mar 2, 2024 · 2 comments
Open

QuickSort implementation on undirected graph #14

st235 opened this issue Mar 2, 2024 · 2 comments

Comments

@st235
Copy link

st235 commented Mar 2, 2024

Hello everybody,

Can someone, please, explain to me a small quicksort implementation detail in the UndirectedGraph class.

In the method selectPivotIndex there is an early return block

	if (startIndex - endIndex <= 1)
		return startIndex;

It seems that this condition is always true. That's totally fine for the quicksort implementation as the pivot could be any element in between startIndex and endIndex, though the heuristic after this block would be obsolete. Is it correct?

@rohanmohapatra
Copy link
Owner

Ah now I get it, do you see a case where it is failing?

@st235
Copy link
Author

st235 commented Sep 9, 2024

Haha, it took some time from me to remember the context 😅

I believe instead of looking at this equation as

	if (startIndex - endIndex <= 1)
		return startIndex;

it is better to take a look at it as

	if (startIndex <= endIndex + 1)
		return startIndex;

which seems to be always or almost always true.

I have not measured the real performance, but it is known that quick sort degrades in performance if deals with unbalanced partition splits.

For this implementation, it seems, that if the array is sorted in reverse order then there is a linear amount of swaps (because of the startIndex choice) on each step of the recursion. In that case, the performance will be O(n^2).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants