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]
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
First, understand the Selection-sort algorithm below: Selection-sort(A: Array [1..n] of numbers) 1       for i=n down to...
First, understand the Selection-sort algorithm below: Selection-sort(A: Array [1..n] of numbers) 1       for i=n down to 2 2                position=i 3                for j=1 to (i–1) 4                          if A[j]>A[position] then position=j 5                if position ≠ i then 6                          temp=A[i] 7                          A[i]=A[position] 8                          A[position]=temp (4 points) If input A=[12,5,11,6,10,7,9,8], what will A be after the 3rd iteration of the outermost for loop of the Selection-sort algorithm completes? A=[__, __, __, __, __, __, __, __] (8 points) Modify the algorithm to solve the...
Sorting – Insertion Sort Sort the list 0, 3, -10,-2,10,-2 using insertion sort, ascending. Show the...
Sorting – Insertion Sort Sort the list 0, 3, -10,-2,10,-2 using insertion sort, ascending. Show the list after each outer loop. Do this manually, i.e. step through the algorithm yourself without a computer. This question is related to data structure and algorithm in javascript (.js). Please do not copy from stackabuse or stackoverflow, Please explain that algorithm with comments. i really want to learn this concept.
An online/anytime sorting algorithm is one that reads its input numbers one at a time, and...
An online/anytime sorting algorithm is one that reads its input numbers one at a time, and maintains a sorted array of all the inputs it has seen so far, so that if it is interrupted at any time, it will still output the correct answer for the inputs that it has processed. Not all sorting algorithms are amenable to modification to make them anytime. But one sorting algorithm, Bubble Sort, is easy to so modify. First, understand the ascending order...
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...
I am trying to add an item, insert and sort to a list without using built...
I am trying to add an item, insert and sort to a list without using built in functions such as .append or insert or extend I have to write def function. I want to add maria to the student name list. and write another def function to sort it using index. studentname=["kay","joe"] def addname(studentname): newlist = studentname[0] for i in range(name): name=input("enter name") newlist =studentname +[name] print(studentname) addname(newlist)
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...
Test each algorithm (Counting Sort, Radix Sort, and Bucket Sort algorithms) on three different arrays of...
Test each algorithm (Counting Sort, Radix Sort, and Bucket Sort algorithms) on three different arrays of 1000 elements each with the following properties: By the way: To perform bucket sort the right way, convert the array elements to a value between 0 and 1 Array1: integers only in the range 0 through 999, already sorted in increasing order Array2: integers only in the range 0 through 999, already sorted in decreasing order Array3: integers only in the range 0 through...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT