Home python Front sorting with lists. I can not come up with how to...

# Front sorting with lists. I can not come up with how to maintain the nesting of arrays for an arbitrary number of discharges

Author

Date

Category

Digital sorting array with lists. I can not come up with how to maintain an array nesting for an arbitrary number of discharges. It turned out only for two discharges, and then with the help of conventions.

Sorting is similar to MSD (MOST Significant Digit Radix Sort), only here are used invested lists (as a result, a multidimensional array is obtained):

1. Distribution of the original array of numbers in 10 arrays depending on the number of senior discharge (from 0 to 9)

2. The next discharge is distributed according to the same principle (10 arrays), but already in the results of the arrays in the first step. etc

3. In the end, we obtain a multidimensional array, and after it combined into one array, we obtain a sorted sequence

``````transformation of a multidimensional array in one array
DEF SET_MASS (MASS):
arr = []
For i in Mass:
IF Type (i)! = List:
Arr.APPEND (I)
ELSE:
arr + = set_mass (i)
Return Arr.
# Starting program
list_0 = [[] for i in range (10)] # 10 lists for sorting numbers (baskets: 0, 1, 2 ... 9)
Mass = [1, 22, 50, 4, 66, 5, 1, 0] # source array
LENGTH = LEN (STR (MAX (MASS)) # maximum number of discharges
# conversion array for sorting by discharge
Mass_num = []
For Num in Mass:
Count_Zero = Length - Len (STR (NUM))
Mass_Num.APPEND ('0' * Count_zero + Str (NUM))
MASS_F = []
For i in Range (Length):
# Sort by first category
IF i == 0:
For Num in Mass_Num:
discharge = int (num [i] # calculate the number of the array to which you want to insert the number
List_0 [Discharge] .APPEND (NUM)
# Sort by second category
IF i == 1:
FOR MASS IN LIST_0:
List_2 = [[] for j in Range (10)]
For Num in Mass:
DISCHARGE = INT (NUM [I])
List_2 [discharge] .append (int (num))
MASS_F.APPEND (List_2)
# sorted array
Print (Set_mass (Mass_f))
``````

If you do everything as Stanislav Volodarskiy said, an error will appear, for example, with such an array:

``````Mass = [1, 22, 10123, 4, 66, 5, 1000, 0]
``````

We get:

``````mass = [0, 1000, 1, 22, 4, 5, 66, 10123]
``````

Most Significant Digit Radix Sort Recursive. So recursion.

Following the ideas from the question, we turn entirely without a sign in a string of equal length. Short rows are complemented on the left (`rjust `) zeros:

``````# & lt; - [1, 22, 50, 4, 66, 5, 1, 0]
# - & gt; 2, ['01', '22', '50', '04', '66', '05', '01', '00']
Def Align (A):
AA = List (MAP (STR, A))
MAX_LEN = MAX (MAP (LEN, AA))
RETURN MAX_LEN, [V.RJUST (max_len, '0') for v in aa]
``````

Now sorting. She is recursive. We sort the older numbers in the resulting baskets we sort the following numbers and so on:

``````# & lt; - 0, 2, ['01', '22', '50', '04' , '66', '05', '01', '00']
# - & gt; [[['00'], ['01', '01'], ['04'], ['05']], [['22']], [['50']], [[' 66 ']]]
DEF Sort_BY_DIGIT (I, N, A):
IF i & gt; = n:
Return A.
Bins = [] for _ in Range (10)]
for v in a:
BINS [INT (V [I])]. Append (V)
RETURN [SORT_BY_DIGIT (I + 1, N, B) for B In Bins IF Len (B) & GT; 0]
``````

The result of sorting is a balanced tree (all subtrees of one height). It needs to be bypassed:

``````# & lt; - 2, [[['00'], ['01', '01'], ['04'], ['05']], [['22']], [['50']], [['66']]]]
# - & gt; ['00', '01', '01', '04', '05', '22', '50', '66']
DEF MERGE (N, BINS):
IF n == 0:
Yield From Bins.
ELSE:
For b in bins:
Yield from Merge (n - 1, b)
``````

Turn the rows back to numbers:

``````# & lt; - ['00', '01', '01', '04', '05' , '22', '50', '66']
# - & gt; [0, 1, 1, 4, 5, 22, 50, 66]
List (MAP (int, ...))
``````

all together:

``````# & lt; - [1, 22, 50, 4, 66, 5, 1, 0]
# - & gt; [0, 1, 1, 4, 5, 22, 50, 66]
n, aa = align (a)
Tree = Sort_BY_DIGIT (0, N, AA)
RETURN LIST (MAP (int, Merge (N, Tree)))
``````

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.