






"""simple docstring"""


class 						__UpperCamelCase     :  # Public class to implement a graph
					def __init__(							self							:	int     , UpperCAmelCase							:	int     , UpperCAmelCase							:	int     , UpperCAmelCase							:	list[list[bool]]   )	->       Any:
											lowerCAmelCase   :Dict						=			row
											lowerCAmelCase   :Optional[Any]						=			col
											lowerCAmelCase   :List[Any]						=			graph
					def        UpperCAmelCase__ (							self							:	List[str]     , UpperCAmelCase							:	int     , UpperCAmelCase							:	int     , UpperCAmelCase							:	list[list[bool]]   )	->       List[str]:
											return (
											    0 <= i < self.ROW
											    and 0 <= j < self.COL
											    and not visited[i][j]
											    and self.graph[i][j]
											)
					def        UpperCAmelCase__ (							self							:	Tuple     , UpperCAmelCase							:	int     , UpperCAmelCase							:	int     , UpperCAmelCase							:	list[list[bool]]   )	->       Optional[Any]:
											lowerCAmelCase   :Optional[Any]						=			[-1, -1, -1, 0, 0, 1, 1, 1]  # Coordinate order
											lowerCAmelCase   :Optional[int]						=			[-1, 0, 1, -1, 1, -1, 0, 1]
											lowerCAmelCase   :Optional[int]						=			True  # Make those cells visited
											for k in range(8   ):
																	if self.is_safe(i + row_nbr[k]     , j + col_nbr[k]     , __lowerCamelCase   ):
																							self.diffs(i + row_nbr[k]     , j + col_nbr[k]     , __lowerCamelCase   )
					def        UpperCAmelCase__ (							self							:	str   )	->       str:  # And finally, count all islands.
											lowerCAmelCase   :Tuple						=			[[False for j in range(self.COL   )] for i in range(self.ROW   )]
											lowerCAmelCase   :int						=			0
											for i in range(self.ROW   ):
																	for j in range(self.COL   ):
																							if visited[i][j] is False and self.graph[i][j] == 1:
																													self.diffs(__lowerCamelCase     , __lowerCamelCase     , __lowerCamelCase   )
																													count += 1
											return count




"""simple docstring"""






import operator




def 			snake_case		(		lowerCAmelCase_  ,						lowerCAmelCase_ = False  ,						lowerCAmelCase_ = None	)						->				list:
  _snake_case   	=      operator.lt if reverse else operator.gt
  _snake_case   	=      solution or []

  if not arr:
    return solution

  _snake_case   	=      [arr.pop(0	)]
  for i, item in enumerate(lowerCAmelCase_	):
    if _operator(lowerCAmelCase_  ,						sublist[-1]	):
      sublist.append(lowerCAmelCase_	)
      arr.pop(lowerCAmelCase_	)

    #  merging sublist into solution list
  if not solution:
    solution.extend(lowerCAmelCase_	)
  else:
    while sublist:
      _snake_case   	=      sublist.pop(0	)
      for i, xx in enumerate(lowerCAmelCase_	):
        if not _operator(lowerCAmelCase_  ,						lowerCAmelCase_	):
          solution.insert(lowerCAmelCase_  ,						lowerCAmelCase_	)
          break
      else:
        solution.append(lowerCAmelCase_	)

  strand_sort(lowerCAmelCase_  ,						lowerCAmelCase_  ,						lowerCAmelCase_	)
  return solution


if __name__ == "__main__":
     assert strand_sort([4, 3, 5, 1, 2]) == [1, 2, 3, 4, 5]
     assert strand_sort([4, 3, 5, 1, 2], reverse=True) == [5, 4, 3, 2, 1]





