Question

The following code implements this algorithm to sort a list of numbers in ascending order. But...

The following code implements this algorithm to sort a list of numbers in ascending order. But some code is missing as indicated by '?'.

def sort_in_place(list_num):
    for i in range(?):
        for j in range(?):
            if ?:
                temp = list_num[j]
                list_num[j] = list_num[i]
                list_num[i] = temp

my_list = [23,1,45,20,13,-34]
sort_in_place(my_list)
print(my_list)

Homework Answers

Answer #1

The missing code that should come in place of '?' is given below :

for i in range(6):
for j in range(i+1,6):
if (list_num[i]>list_num[j]):

Here in our case, 6 is the number of elements in the list. You can change this value depending upon the number of elements in any list.

Complete Python program with output is given below.

Python Code :

def sort_in_place(list_num):
for i in range(6):
for j in range(i+1,6):
if (list_num[i]>list_num[j]):
temp = list_num[j]
list_num[j] = list_num[i]
list_num[i] = temp

my_list = [23,1,45,20,13,-34]
sort_in_place(my_list)
print(my_list)

Output :

If you have any doubt/query regarding the solution then let me know in comment. If it helps, kindly give an upVote to this answer.

Know the answer?
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
Similar Questions
PYTHON The following code implements this algorithm to sort a list of numbers in ascending order....
PYTHON The following code implements this algorithm to sort a list of numbers in ascending order. But some code is missing as indicated by '?'. def sort_in_place(list_num): for i in range(?): for j in range(?): if ?: temp = list_num[j] list_num[j] = list_num[i] list_num[i] = temp my_list = [23,1,45,20,13,-34] sort_in_place(my_list) print(my_list) Modify the three lines of code in the program below so that the output is [-34, 1, 13, 20, 23, 45]
Consider the following insertion sort algorithm. void insertion_sort(element a[], int n) // Put a[0]..a[n-1] into ascending...
Consider the following insertion sort algorithm. void insertion_sort(element a[], int n) // Put a[0]..a[n-1] into ascending order by insertion sort. { for (int k = 1; k < n; k++) { // At this point, a[0]..a[k-1] are already in order. // Insert a[k] where it belongs among a[0]..a[k]. You need to write code for this insertion as the body of the for-k loop. }//endfor k } a) Write the code for the body of the for-k loop to complete the...
Question2: The following function is supposed to receive a list of numbers and a number as...
Question2: The following function is supposed to receive a list of numbers and a number as the input parameters. The function finds all occurrences of number inside the list and removes them from the list and eventually returns the results. Do you think this function will work as expected? If yes, please mention and if no, please fix it. def function(listOfNumbers, number): for i in range(len(listOfNumbers)): if listOfNumbers[i]==number: listOfNumbers.remove(number) return listOfNumbers newList = function([3,4,1,3,4,7,8,1,2,3,4,6,8], 3) print("After removing the occurrences of...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort algorithm to sort this list. Fill in this table with each iteration of the loop in the selection sort algorithm. Mark the place from which you are looking for the 'next smallest element'. In this display, the upper numbers are the indices, the lower numbers are in the corresponding positions. Use the several rows provided to show the sequence of steps. 0 1 2...
Write an R code to print out all even numbers from the following numbers list in...
Write an R code to print out all even numbers from the following numbers list in the same order they are received. Write the code so it does not print any numbers that come after 83. numbers = [951, 40, 84, 51, 60, 69, 48, 19, 61, 85, 98, 50, 72, 47, 44, 61, 83, 65, 41, 51, 63, 61, 65, 75, 21, 30, 84, 92, 23]
1. What will the following python code display? num = list(range(5)) print(num) 2. Answer the following...
1. What will the following python code display? num = list(range(5)) print(num) 2. Answer the following questions using the list below. You can record your answers in the essay textbox. data = [5,3,7] A. Write code to replace the value in the 0 position with the number 8. B. Add the value 10 to the end of the list. C. Insert the value 22 after position 1 in the list. D. Remove the value at position 2. E. Sort the...
Generate test cases for the following code by using Basic Path Testing. void sort(int a[ ],int...
Generate test cases for the following code by using Basic Path Testing. void sort(int a[ ],int N) {     int i,j,p,t;        for(i=0;i<N-1;i++)        {     p=i;               for(j=i+1;j<N;j++)                      if(a[j]>a[p]) p=j;               if(p!=i)               {     t=a[p];  a[p]=a[i];     a[i]=t;   }        } } a)       Draw CFG. b)       How many basic paths for the CFG? c)        List the basic paths. d)       Generate test cases from it.
Perform Bubble Sort on the following list of numbers. show visually all the steps clearly in...
Perform Bubble Sort on the following list of numbers. show visually all the steps clearly in a tabular format. 9, 15, 3, 4, 3, 15, 7
# Fill in the missing parts in the following code. # It should compute the same...
# Fill in the missing parts in the following code. # It should compute the same result as map(lambda x: n*x, L). def multAll(n, L): '''Assuming n is a number and L is a list of numbers, make a list by multiplying each element of L by n. For example, multAll(3,[3,5,7,9]) is [9,15,21,27].''' if L==[]: return None # TO-DO replace None else: return None # TO-DO replace None
Finish the code wherever it says TODO /**    * Node type for this list. Each...
Finish the code wherever it says TODO /**    * Node type for this list. Each node holds a maximum of nodeSize elements in an    * array. Empty slots are null.    */    private class Node {        /**        * Array of actual data elements.        */        // Unchecked warning unavoidable.        public E[] data = (E[]) new Comparable[nodeSize];        /**        * Link to next node.       ...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT