GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.hackerrank.com — Sorting: Bubble Sort | HackerRank 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. Consider the following version of Bubble Sort: for (int i = 0; i < n; i++) { for (int j = 0; j < n - 1; j++) { // Swap adjacent elements if they are in decreasing order if (a[j] > a[j + 1]) { swap(a[j], a[j + 1]); } } } Given an array of integers, sort the array in ascending order using the Bubble Sort algorithm above. Once sorted, print the following three lines:
Hint: To complete this challenge, you must add a variable that keeps a running tally of all swaps that occur during execution. Example
The steps of the bubble sort are shown above. It took swaps to sort the array. Output is: Array is sorted in 3 swaps. First Element: 1 Last Element: 6 Function Description Complete the function countSwaps in the editor below. countSwaps has the following parameter(s):
Prints Print the three lines required, then return. No return value is expected. Input Format The first line contains an integer, , the size of the array . Constraints Output Format Sample Input 0
Sample Output 0
Explanation 0 Sample Input 1
Sample Output 1
Explanation 1 At this point the array is sorted and the three lines of output are printed to stdout. Cookie support is required to access HackerRankSeems like cookies are disabled on this browser, please enable them to open this website |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |