We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Hi,
I think the Bubble sort Algorithm specified, has a little drawback. The inner loop should not be running to complete length it should be (length-1 - outer loop index) and outer loop also should be running to length-1. i.e. pseudo - algorithm should be this:
for (int i = 0; i < n-1; i++) {
int numberOfSwaps = 0;
for (int j = 0; *j < n -i - 1 *; j++) {
if (a[j] > a[j + 1]) {
swap(a[j], a[j + 1]);
numberOfSwaps++;
}
}
if (numberOfSwaps == 0) {
break;
}
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Day 20: Sorting
You are viewing a single comment's thread. Return to all comments →
Hi, I think the Bubble sort Algorithm specified, has a little drawback. The inner loop should not be running to complete length it should be (length-1 - outer loop index) and outer loop also should be running to length-1. i.e. pseudo - algorithm should be this: