Home python Merge sorting on Python

Merge sorting on Python

Author

Date

Category

I decided to explore the algorithms and tried to make a merge sorting.
I am granted a wizard where Timofey Hiryanov explained.

n = 0
# G = [INT (INPUT ()) for m in range (n)]
G = [3,9,1,8,4,26,5,1,136]
DEF MERGE (A, B):
 i = 0.
 k = 0.
 J = 0.
 C = [0] * (Len (A) + Len (B))
 While i & lt; len (a) and k & lt; len (b):
  IF A [I] & lt; = B [k]:
   C [j] = a [i]
   I + = 1
   j + = 1
  ELSE:
   C [J] = B [k]
   j = + 1
   K + = 1
 While I & LT; Len (A):
  C [j] = a [i]
  j = + 1
  I + = 1
 While K & LT; Len (B):
  C [J] = B [n]
  n = + 1
  j + = 1
DEF Merge_Sort (A):
  IF LEN (A) & LT; = 1:
    Return.
  Middle = Len (A) // 2
  L = [A [I] FOR I IN RANGE (0, MIDDLE)]
  R = [A [I] for i in Range (Middle, Len (A))]
  MERGE_SORT (L)
  Merge_Sort (R)
  C = Merge (L, R)
  For i in g:
    A [I] = G [i]
    Return A.
Merge_Sort (G)

Here is a mistake
Traceback (MOST Recent Call Last):
File “C: /Ussers/daniil/PycharmProjects/program1/1.py”, Line 39, In
Merge_Sort (G)
File “C: /Ussers/daniil/PycharmProjects/program1/1.py”, Line 33, in Merge_Sort
MERGE_SORT (L)
File “C: /Ussers/daniil/PycharmProjects/program1/1.py”, Line 33, in Merge_Sort
MERGE_SORT (L)
File “C: /Ussers/daniil/PycharmProjects/program1/1.py”, Line 37, in Merge_Sort
A [I] = C [i]
Typeerror: ‘NONETYPE’ OBJECT IS NOT SubScriptable

Process Finished With Exit Code 1


Answer 1

If I understand correctly, at the end of the merge function, you need to return the value, and you will not return anything. Need to do this:

def merge (a, b):
# ...
  Return C.

If you do not return anything from the merge function, then after the assignment

c = merge (L, R)

You have in the variable C will be none , what is your program in the end and swears.

And you still seem to have an excess tab in front of Return a in the merge_sort function, and in general it is not clear to you why the result of this function is not asked for anything . And the confusion between C and G is also incomprehensible. In general, “I need to change the entire system.”


Answer 2, Authority 100%

If you were not “Voski”, and thoughtfully studied the theory, they would have written their algorithm something like this:

g = [3,9,1,8,4,26,5,1,1,136]
DEF MERGE (A, B):
  Res = []
  i = 0.
  J = 0.
  While I & LT; Len (a) and j & lt; Len (b):
    IF A [I] & LT; = B [J]:
      RES.APPEND (A [I])
      I + = 1
    ELSE:
      Res.APPEND (B [J])
      j + = 1
  Res + = A [I:] + B [J:]
  Return Res.
DEF Merge_Sort (A):
  IF LEN (A) & LT; = 1:
    Return A.
  ELSE:
    L = A [: LEN (A) // 2]
    R = A [Len (A) // 2:]
  Return Merge (Merge_Sort (L), Merge_Sort (R))
Merge_Sort (G)
Out [5]: [1, 1, 3, 4, 5, 8, 9, 26, 136]

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions