Question

.Use the selection sort to put the following list in order. (5 credits) (I’ve put the...

.Use the selection sort to put the following list in order. (5 credits) (I’ve put the list in an array (boxes so you can see the distinct elements clearly.

You can just type the numbers across the page in answering.

Remember to show the sort marker as is done in your book.)

Show the list after each exchange that has an effect on the list ordering.

Unsorted list: 26 17 32 47 56 9 12 72 3 25

26 17 32 47 56 9 12 72 3 25 | <-- sort marker

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Selection Sort: -

Selection sort is a popular sorting algorithm, which repeatedly finds the minimum element of an unsorted array and puts it at the beginning. During every iteration the sort algorithm moves the minimum value from the unsorted array part and moves it to the sorted array part

Suppose the unsorted array is 26, 17, 32, 47, 56, 9, 12, 72, 3, 25, of length 10, and indices 0 to 9

First iteration finds the minimum element from array [0:9] and places it at array [0]

Ie, 3, 26, 17, 32, 47, 56, 9, 12, 72, 25

Second iteration finds the minimum element from array [1:9] and places it at array [1]

Ie, 3, 9, 26, 17, 32, 47, 56, 12, 72, 25

Third iteration finds the minimum element from array [2:9] and places it at array [2]

Ie, 3, 9, 12, 26, 17, 32, 47, 56, 72, 25

This goes on and finally a sorted array is created 3,9,12,17,25,26,32,47,56,72

Python code for selection sort: -

def selection_sort(arr):

    N=len(arr)

    for start in range(N):

        min_pos=start

       

        for j in range(start, N):

            if arr[j] < arr[min_pos]:

                min_pos=j

       

        arr[start],arr[min_pos] =arr[min_pos],arr[start]

   

test_arr=[26, 17, 32, 47, 56, 9, 12, 72, 3, 25]

selection_sort(test_arr)

print(test_arr)

Add a comment
Know the answer?
Add Answer to:
.Use the selection sort to put the following list in order. (5 credits) (I’ve put the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT