Question

identify the code that sorts the numbers in descending order. def selection_sort(numbers): for i in range(len(numbers) - 1):
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The inner loop of the selection sort must run from i+1 till length because it compares the current element with the next element

So, eliminate option B and D

Now, for sorting is descending order we compare if numbers[j]>numbers[index] that is if later number is greater

swap it with current

So, correct answer is option A

The first option

Add a comment
Know the answer?
Add Answer to:
identify the code that sorts the numbers in descending order. def selection_sort(numbers): for i in range(len(numbers)...
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
  • *MATLAB CODE* 3)Write a function 'mydsort' that sorts a vector in descending order (using a loop,...

    *MATLAB CODE* 3)Write a function 'mydsort' that sorts a vector in descending order (using a loop, not the built-in sort function). 4)write a function that will receive a vector and will return two index vectors: one for ascending order and one for descending order. Check the function by writing a script that will call the function and then use the index vectors to print the original vector in ascending and descending order. **Please make sure they work. I have found...

  • without coding Give the Big O run-time of the following algorithms. Binary Search: def binary-search (arr,...

    without coding Give the Big O run-time of the following algorithms. Binary Search: def binary-search (arr, low, high, x): # Check base case if low > high : return None else: mid = (high + low) // 2 element arr[mid] == X: if element return mid elif element > X: return binary-search(arr, low, mid 1, x) else: return binary_search(arr, mid + 1, high, x) Selection Sort: def selection_sort (arr): for i in range (len(arr)): smallest index = i smallest value...

  • Run-able, Corrected source code The following code will sort an array in descending order. Keep it...

    Run-able, Corrected source code The following code will sort an array in descending order. Keep it as Descending. However it has two logic bugs. //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /*    The sorting algorithm is to be left as descending    There are two logical bugs, the number do not sort correctly    Fix it    Add analysis output as per requirements */ #include <stdio.h> main() { char wait; short m[]={3,5,7,2,5,1,2,2,              6,5,7,2,4,1,3,3,              7,7,3,2,5,7,1,9}; unsigned char temp, i, j; unsigned char numElements =...

  • Consider the following python code that will sort the list of numbers using the Bubble Sort...

    Consider the following python code that will sort the list of numbers using the Bubble Sort algorithm def bubbleSort(alist): print(alist) for j in range (len(alist) - 1, 8, -1): for i in range(): if alist[i] > alist[i + 1]: temp = alist[i] alist[i] = alist[i+1] alist[i+1] = temp print(alist) alist = [52, 25, 94, 17, 25, 52] bubbleSort (alist) print(alist) Sort the following series of values into ascending order using the Bubble Sort algorithm. Write out the complete row of...

  • Python3 Modify the original Bubble Sort code shown below so that it will print out the...

    Python3 Modify the original Bubble Sort code shown below so that it will print out the length of the list, total number of comparisons, and total number of swaps needed to sort the list given. def compare(data, a, b): """Returns True if element at index a > element at index b""" return data[a] > data[b] def swap(data, a, b): """Swaps the element at index a with element at index b""" data[a], data[b] = data[b], data[a] def bubble_sort( data ): """Sorts...

  • I'm trying to code a C program so it sorts an array of integer numbers of...

    I'm trying to code a C program so it sorts an array of integer numbers of size n in ascending order. My code is written below but its not working properly, its giving me errors, I think my sort and swap functions aren't done right maybe, please help and fix. It says "undefined reference to "SelectionSort" as an error. But the question asks to not change the PrintArray and Main function. #include <stdio.h> void PrintArray(int size, int array[]) { for...

  • pointsConsider this code def f (n,p): for i in range(n): for j in range(i,p): for k...

    pointsConsider this code def f (n,p): for i in range(n): for j in range(i,p): for k in range(n*p): dosomething(i,j,k) + dosomething(j,i,k) Write down the number of calls to dosomething T(n, p) in summation notation

  • I'm trying to sort a list of students from a text file in python(3.7) with three separate sorting functions (Bubble, selection, insert) I'm not sure to why as its not working I'm going to...

    I'm trying to sort a list of students from a text file in python(3.7) with three separate sorting functions (Bubble, selection, insert) I'm not sure to why as its not working I'm going to guess its because I'm not using the swap function I built. Every time I run it though I get an error that says the following Traceback (most recent call last): File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 146, in <module> main() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py", line 122, in main studentArray.gpaSort() File "C:/Users/tkoto/Desktop/SearchAndSortLab.py",...

  • 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...

    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 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...

  • The following program sorts the array of numbers entered by the user and prints out the...

    The following program sorts the array of numbers entered by the user and prints out the elements in descending order. It has many errors. Debug the code and show the output. #include <stdio.h> #define N 4 void desc(float *a, float *b) float tp; tp=a; a=b; b=tp; int main() int i, j, ii, mark[N] ; for (ii==0;ii<N;ii++) printf("Enter the %d th element of your array: \n",ii); scanf("%d", mark[ii]); printf("You have entered the following array: \n"); for (ii=0,ii<N,ii++) printf("%d ", mark[ii]); printf("\n...

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