Question

Problem 3 Selection sort (15 points) To gain familiarity with selection sort, add an extra parameter k to the selection sort

def selectionSortK(alist, k):
for i in range(0,len(alist) - 1):
min = i
for j in range(i + 1, len(alist)):
if alist[j] < alist[min]:
min = j
temp = alist[i]
alist[i] = alist[min]
alist[min] = temp

ergesort Heres how these functions would sort a list of integers: [7 2 8 5 1 3 6 4] mergesort [7 2 8 5] [1 3 6 4] mergesort

P3: Sanity Test: Is selectionSortK callable? ... ok
test_doNothing (__main__.TestProblem3)
P3: Does sorting the first k elements with k=0 do nothing? ... ok
test_onePass (__main__.TestProblem3)
P3: Sorting a portion of a decreasing list ... FAIL
test_severalPasses (__main__.TestProblem3)
P3: Sorting a decreasing list in several stages ... FAIL

Problem 3 Selection sort (15 points) To gain familiarity with selection sort, add an extra parameter k to the selection sort procedure presented in class in Lecture 19 slide 45. Modify the loop so that selection sort runs in linear time for fixed k by retuming early with only the first k items in the list sorted.
ergesort Here's how these functions would sort a list of integers: [7 2 8 5 1 3 6 4] mergesort [7 2 8 5] [1 3 6 4] mergesort 17 2] [8 5] [1 3] [6 4] mergesort 7 2
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find the program to sort a list of first k items and return only the first k sorted items.

Note: I have ignored merge sort because there is not questions asked about it here...

Program:

def selectionSortK(alist, k):
if k == 0:
print("K is 0, no alteration in the list")
return
  
if k > len(alist):
print("K is greater than the length of list, no alteration in list")
return
  
#traverse the list based on k
for i in range(0,k - 1):
min = i
#compare the list elements with k range not with len of list
for j in range(i + 1, k):
if alist[j] < alist[min]:
min = j
temp = alist[i]
alist[i] = alist[min]
alist[min] = temp
  

#delete the remainng elements in a list based on k
if k < len(alist):
for i in range(k-1, len(alist)-1):
del alist[k]
  
lsit=[7, 2, 8, 5, 1, 3, 6, 4]
print("Input list is ")
print(lsit)
selectionSortK(lsit, 3)

print("Sorted list is ")
print(lsit)

Output:

Input list is
[7, 2, 8, 5, 1, 3, 6, 4]
Sorted list is
[2, 7, 8]


Screen shot:

1 def selectionSortK (alist, k): print(K is 0, no alteration in the list 4 return if k len(alist): print (K is greater tha

Add a comment
Know the answer?
Add Answer to:
def selectionSortK(alist, k): for i in range(0,len(alist) - 1): min = i for j in range(i + 1, len(alist)): if alist[j] < alist[min]: min = j temp = alist[i] alist[i] = alist[min] alist[min] = temp...
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