83.0k views
4 votes
Given an array of letters and an array of positions, use Bubble sort to sort the arrays to build the word. To find out where you put the letters, you will use the positions contained in the second given array. The code for the arrays is given to you, please DO NOT CHANGE THIS.

Sample Arrays 1

letters[] = {'l', 'o', 'e', 'h', 'l'};

positions[] = {2, 4, 1, 0, 3};

Sample Output 1

hello

User ViliusL
by
8.2k points

1 Answer

5 votes

Final answer:

To sort the given word using Bubble sort, we swap adjacent elements if they are in the wrong order.

Step-by-step explanation:

To sort the given word using Bubble sort, we need to swap adjacent elements if they are in the wrong order. In this case, we need to sort the 'letters' array based on the positions given in the 'positions' array. Here is how we can do it:

  1. Start a loop to iterate through the 'positions' array.
  2. Inside the loop, start another loop from the last element towards the current element of the outer loop.
  3. Check if the position of the current element is greater than the position of its adjacent element. If it is, swap the elements in both the 'letters' and 'positions' arrays.
  4. Continue this process until the arrays are sorted.
  5. After sorting the arrays, construct the word by concatenating the elements of the 'letters' array in the order specified by the 'positions' array.
  6. The final word will be the sorted word.

In the given example, the sorted word is 'hello'.

User Bilal Yasar
by
8.0k points