115k views
5 votes
Suppose you have the array of numbers [3 2 5 4 1]. In the style we showed in class, show the contents of the array during a selection sort.

User Norio
by
8.1k points

1 Answer

3 votes

Final answer:

During a selection sort, the array [3 2 5 4 1] would be sorted as follows: [1 2 3 4 5].

Step-by-step explanation:

  1. Step 1: Find the smallest element in the array. In this case, it is 1.
  2. Step 2: Swap the smallest element (1) with the first element (3). The new array becomes [1 2 5 4 3].
  3. Step 3: Repeat steps 1 and 2 for the remaining elements. The smallest element in the remaining array is 2, so we swap it with the second element. The new array becomes [1 2 5 4 3].
  4. Step 4: Repeat steps 1 and 2 for the remaining elements. The next smallest element is 3, so we swap it with the third element. The new array becomes [1 2 3 4 5].
  5. Step 5: Repeat steps 1 and 2 for the remaining elements. The next smallest element is 4, so we swap it with the fourth element. The new array becomes [1 2 3 4 5].
  6. Step 6: Repeat steps 1 and 2 for the remaining elements. The last remaining element is 5, so no swaps are necessary. The final sorted array is [1 2 3 4 5].
User Keem
by
7.8k points