Why this algorithm can sort data in descending order

2024/10/6 5:22:11

I study python programming and try to sort data in descending order.#sort1 below is successfully sorted but I cannot understand why this happen. Also, data[i], data[data.index(mn)] = data[data.index(mn)], data[I] is suspicious point.

data = [-1.48,  4.96,  7.84, -4.27,  0.83,  0.31, -0.18,  3.57,  1.48,  5.34,9.12,  7.98, -0.75,  2.22, -1.16,  6.53, -5.38,  1.63, -2.85,  7.89,-5.96, -8.23,  8.76, -2.97,  4.57,  5.21,  9.43,  3.12,  6.52,  1.58 ]
#sort1for i in range(30):mn = data[i]for j in data:if j < mn:mn = jdata[i], data[data.index(mn)] = data[data.index(mn)], data[i]else:pass
print('ascending order1:')
print(data)
Answer

This is insertion sort https://en.wikipedia.org/wiki/Insertion_sort

You can imagine it as if sorting a streamed list of items:

for i in range(30): # for each item streamed heremn = data[i]    # take the new item (if exists new item)for j in data:  # find its place in the sorted data, and insert it there:if j < mn:  # if found its place, insert it heremn = j  data[i], data[data.index(mn)] = data[data.index(mn)], data[i] 

UPDATE

The intuition behind the insertion sort is that you update the previously sorted list each time you get to a new item. So, you don't need to worry about the sorting position of future items.

Since the data before time i is sorted, then after the finding the first swap all items will swap until it reaches the time i again.

Now, the problem with the swapping command:

data[i], data[data.index(mn)] = data[data.index(mn)], data[i] 

This swapping command, in fact, ignores future swappings (when data.index(mn) is larger than the current time i). However, since it works when time i is larger than data.index(mn), that is enough for insertion sort. This is an example of:

# two attempts to swapping x and y: 
data = ['x', 'y']# ignored (target of swap is at time i, found position in future!):
i = 0; mn = 'y' # data.index(mn) == 1
data[i], data[data.index(mn)] = data[data.index(mn)], data[i] 
print('ignored swap', data) # success (target of swap is at time i, found position in past (before i)):
i = 1; mn = 'x' # data.index(mn) == 0
data[i], data[data.index(mn)] = data[data.index(mn)], data[i] 
print('success swap', data)
https://en.xdnf.cn/q/118871.html

Related Q&A

Processing.py - Unknown Error on Class Definition

I have no idea how to fix this error. Maybe theres an open parenthesis or quotation mark somewhere before this line?What is wrong with this code?Class Ribbon: # I got an error on this line! def __ini…

How can I implement a stopwatch in my socket Python program

I am creating a very simple ping program in Python that will send 16- 64 bytes of information to a local server, once the server has received all the bytes, it will send back a 1 Byte message back to t…

Making a quiz with shuffled questions

I am wanting to join the Royal Air Force and thought as a good way to prepare I should code myself a quiz about their aircraft.There are 28 aircraft that I have added to the quiz. For example - Where i…

How to create a timer that resets for quiz games?

I trying to create a timer for my quiz game. It should reset after every right question. But problem with my code is that it keeps increasing speed after every time it resets. timeCount = 30 def countd…

Stucking of python multiprocessing.pool

i have multiprocessing script with "pool.imap_unordered". I ran into a problem when the script got stuck but i check CPU usage — nothing happening (by "top" command on ubuntu). Goi…

python: merge two csv files

I have a problem while Im doing my assignment with python. Im new to python so I am a complete beginner.Question: How can I merge two files below?s555555,7 s333333,10 s666666,9 s111111,10 s999999,9and…

Generate a custom formated string with python

I have a Javascript code that generates a string (similar to uuid) stringHere it is the js code: var t = "xxxxxxxx-xxxx-xxxx-xxxx-xxxx-xxxxxxxx", i = (new Date).getTime(); return e = t.repla…

PySide-6.6: clicked signal sends extra boolean argument to slot

I just upgraded PySide6 (PySide6==6.6.0 from 6.2.2) and a new behavior is wreaking havoc with my GUI program. Every place where I have a clicked signal, the hooked up slot is receiving an extra bool==F…

SMTPConnectError when using Django

Im using django-registration for handling of users registration. I tried to signup in order to test it, after testing it, I got this errorSMTPConnectError at /accounts/register/Being trying to find a s…

how to have a single search API using path parameters (No form used)

I have been using this view for searching a word as:db refers mongo connection (just for ref)@app.route(/) def index():return render_template(index.html)@app.route(/words-<word>, methods=[GET, PO…