Why This Algorithm Can Sort Data In Descending Order
Solution 1:
This is insertion sort https://en.wikipedia.org/wiki/Insertion_sort
You can imagine it as if sorting a streamed list of items:
foriinrange(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:ifj<mn:# if found its place, insert it heremn=jdata[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)
Solution 2:
There are 2 errors in your code:
- It is better in the loop to do
for i in range(len(data))
, so if the size of the variable data change, your code will work. - Your code sort data in descending order, so you should print:
print('descending order1:')
Now, let's talk the algorithm part. In fact, your code is the implementation of the sorting algorithm bubble sort also knows as sinking sort. This algorithm steps through a list repeatedly and compares adjacent elements. It swaps the adjacent elements if they are in the wrong order (i.e if the first element is inferior to the second). It does that untill the list is sorted (in the descending order). You can get more understanding here
The code data[i], data[data.index(mn)] = data[data.index(mn)], data[i]
is just the swap part. It is a Pythonic way to swap elements. For instance:
a = 5
b = 15
a, b = b, a # swap 'elegantly a and bprint(a) # display 15print(b) # display 5
Comments on the code:
for i inrange(30): # first cursor: steps through the indexes of the list
mn = data[i] # assigns the data at index i to the variable mnfor j in data: # second cursor: steps through the data of the listif j < mn: # compares adjacent elements
mn = j
data[i], data[data.index(mn)] = data[data.index(mn)], data[i] # swap adjacent elementselse: # if the first data superior to the second, don't do anythingpass
Post a Comment for "Why This Algorithm Can Sort Data In Descending Order"