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
  • My tests for sorting algorithms are not working. sorting.py has 6 sorting algorithms and test_sorting.py has...

    My tests for sorting algorithms are not working. sorting.py has 6 sorting algorithms and test_sorting.py has test cases to run those algorithms, but test cases are not working. please correct the errors in test_sorting.py file so that it can test all the sorting algorithms. WARNING: DON'T COPY AND PASTE THE QUESTION IN ANSWER. I WILL REPORT YOU. sorting.py # 1. selection sort # 2. insertion sort # 3. shell sort # 4. heap sort # 5. merge sort # 6....

  • Here is the code given for this problem: **And please do this in Python** def mergesort(mlist): if len(mlist)<2: ret...

    Here is the code given for this problem: **And please do this in Python** def mergesort(mlist): if len(mlist)<2: return mlist else: mid=len(mlist)//2 return merge(mergesort(mlist[:mid]),mergesort(mlist[mid:])) Problem 1 (30 points) stable merge sort Consider the following unsorted list of integers containing duplicates where the relative position of each duplicated integer in the unsorted list is noted as a subscript. For example, 1, is at a smaller index than 12. The subscript is ignored when comparing two values since it would not actually...

  • The language is python thr language is python I Expert Q&A Done The language is python....

    The language is python thr language is python I Expert Q&A Done The language is python. 10. The following is the algorithm for Merge Sort, one of the best sorting algorithms and one hat is recursive. Wine the merge sort function and draw the stack and heap dagrams for execution on the list |5, 8,9,1,4. 10 Algorithm: Mergesort Ingut an unsorted list b 1. If there is only one iten L. It is sorted, return L 2, Split L Sn...

  • Objective: GUI Layout manager Download one of the sample GUI layout program. Use any GUI layout...

    Objective: GUI Layout manager Download one of the sample GUI layout program. Use any GUI layout to add buttons to start each sort and display the System.nanoTime in common TextArea panel. The question is a bit confusing so i will try to simplify it. Using the GUI ( I made a unclick able one so you have to make it clickable), allow a user to sort the text file based on what they click on. example: if i click merge...

  • Question 5 (10 Points) Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection-Sort...

    Question 5 (10 Points) Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection-Sort segments the list into sorted and unsorted elements. The algorithm continues to remove the smallest element of the unsorted list and adds it to the sorted segment. Implement the algorithm that accepts an unsorted list and returns a sorted one - in ascending order. 5 3 8 Initial List 4 6 18 4 6 Minimum Swaps Position: Sorted List Length 1 Minimum Swaps Position:...

  • please help with python Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection...

    please help with python Write a well-documented, Python program, hmwk305.py that implements the Selection-Sort algorithm. Selection Sort segments the list into sorted and unsorted elements. The algorithm continues to remove the smallest element of the unsorted list and adds it to the sorted segment. Implement the algorithm that accepts an unsorted list and returns a sorted one - in ascending order. 5 3 8 4 6 Initial List Minimum Swaps Position: Sorted List Length 1 Minimum Swaps Position: Sorted List...

  • Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. E...

    Modify the sorts (selection sort, insertion sort, bubble sort, quick sort, and merge sort) by adding code to each to tally the total number of comparisons and total execution time of each algorithm. Execute the sort algorithms against the same list, recording information for the total number of comparisons and total execution time for each algorithm. Try several different lists, including at least one that is already in sorted order. ---------------------------------------------------------------------------------------------------------------- /** * Sorting demonstrates sorting and searching on an...

  • Can I get only answers pls 46) 40) Which of the following finds the position of...

    Can I get only answers pls 46) 40) Which of the following finds the position of the second character in the string You can assume word does have at least two Qs in it. word find wordt word find .11) .find word word. rind t Q') .find word. rind 47) Suppose we start with the list 13, 10, 5, 7, 9, 2, 1, 81 After Two (2) iterations of the selection sort algorithm (as seen in class). what does the...

  • #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include<time.h> void...

    #include <sys/types.h> #include <sys/ipc.h> #include <sys/shm.h> #include <sys/wait.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include<time.h> void insertionSort(int arr[], int n); void merge(int a[], int l1, int h1, int h2); void mergeSort(int a[], int l, int h) { int i, len=(h-l+1); //Using insertion sort for small sized array if (len<=5) { insertionSort(a+l, len); return; } pid_t lpid,rpid; lpid = fork(); if(lpid<0) { //Lchild proc not created perror("Left Child Proc. not created\n"); _exit(-1); } else if (lpid==0) { mergeSort(a,l,l+len/2-1); _exit(0); } else...

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