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]
# Implement the Bubble Sort algorithm to this code. Add output statements to the code to...
# Implement the Bubble Sort algorithm to this code. Add output statements to the code to verify that it is working properly. Then, add code that will short circuit the sort process if at any time an intermediate pass through the list identifies that the entire list is already sorted. def bubbleSort(theSeq): n = len(theSeq) #perform n - 1 bubble operations on the sequence for i in range(n - 1): #bubble largest item to end for j in range(i +...
[Lab Task HTML/JAVASCRIPT] Please read 10 numbers that are list below, sort the numbers and then...
[Lab Task HTML/JAVASCRIPT] Please read 10 numbers that are list below, sort the numbers and then print those numbers. 10 numbers = { 9, 3, 2, 1, 10, 30, 4, 6, 7, 8} Output should have a sorted list [Reference JavaScript code] <html> <body>     <H1>prompt()</h1>     <p id="pro"></p>     <script>        // Array creation        var num= new Array();               var ix = 0;        // Read 10 numbers        for (ix = 0; ix < 10; ix++)...
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...
Write an insertion sort algorithm in C language by performing following steps .make sure code is...
Write an insertion sort algorithm in C language by performing following steps .make sure code is work. thanks Prompt the user to enter the number of array elements (say, N). Read the number of elements (N). Implement the insertion sort algorithm (note that insertion sort is an in-place sort that modifies the original array). Print the sorted array.
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)
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT