Sorting: Bubble Sort

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: 1. Array is sorted in numSwaps swaps., where numSwap is the nu

View Solution →

Sorting: Comparator

Comparators are used to compare two objects. In this challenge, you'll create a comparator and use it to sort an array. The Player class is provided in the editor below. It has two fields: 1. name : a string. 2. score : an integer. Given an array of Player objects, write a comparator that sorts them in order of decreasing score. If 2 or more players have the same score, sort those players alphabetically ascending by name. To do this, you must create a Checker class that implements the Comp

View Solution →

Fraudulent Activity Notifications

HackerLand National Bank has a simple policy for warning clients about possible fraudulent account activity. If the amount spent by a client on a particular day is greater than or equal to 2 x the client's median spending for a trailing number of days, they send the client a notification about potential fraud. The bank doesn't send the client any notifications until they have at least that trailing number of prior days' transaction data. Given the number of trailing days d and a client's tota

View Solution →

Merge Sort: Counting Inversions

In an array, arr , the elements at indices i and j (where i < j ) form an inversion if arr[ i ] > arr[ j ]. In other words, inverted elements arr[ i ] and arr[ j ] are considered to be "out of order". To correct an inversion, we can swap adjacent elements. Function Description Complete the function countInversions in the editor below. countInversions has the following parameter(s): int arr[n]: an array of integers to sort Returns int: the number of inversions Input Format

View Solution →

Strings: Making Anagrams

A student is taking a cryptography class and has found anagrams to be very useful. Two strings are anagrams of each other if the first string's letters can be rearranged to form the second string. In other words, both strings must contain the same exact letters in the same exact frequency. For example, bacdc and dcbac are anagrams, but bacdc and dcbad are not. The student decides on an encryption scheme that involves two large strings. The encryption is dependent on the minimum number of char

View Solution →