The program doesn't do anything.



  • I'll make sure I'm a newcomer in programming. A problem had arisen with the task of the Summerfield Python (aveage2) book, where the assignment was to sort the list from the smaller to the larger. Looked at some algorithms, decided to use the match. He wrote the code, and he doesn't do anything with the list, like he's infinitely stuck somewhere, and he's doing the N-activated endless collo-wall. Please tell me what and where to fix it, I'm not really grateful. If anything, the list is just for him to be in the main program, the user will forget the numbers.

    Note: The task should be accomplished without the use of list.sort(s)

    List = [12, 56, 3, 38, 2, 16]
    

    x = 0

    while x < len(List):
    try:

    <= List[x + 1]:

    x += 1
    else:
    if x - 1 < 0:
    a = 0
    List.insert(a, List[x + 1])
    List.remove(List[x + 2])
    x = 0
    else:
    List.insert(x - 1, List[x + 1])
    List.remove(List[x + 2])
    x = 0
    except x + 1 == len(List):
    print(List)
    break



  • Are you a bubble or something?

    Then it looks strange to have an algorithm. insertremove It's not good in the cycle, but it's more like this:

    List = [12, 56, 3, 38, 2, 16]
    

    for j in range(len(List) - 1):
    flag = False
    index_min = j

    for i in range(j, len(List) - 1 - j):
        if List[i] &gt; List[i + 1]:
            flag = True
            List[i], List[i + 1] = List[i + 1], List[i]
    
        if List[i] &lt; List[index_min]:
            index_min = i
    
    if not flag:
        break
    
    if index_min != j:
        List[j], List[index_min] = List[index_min], List[j]
    

    print(List)

    Well, or if the soul demands something shorter,

    for j in range(len(List) - 1):
    flag, index_min = False, j

    for i in range(j, len(List) - 1 - j):
        List[i], List[i + 1], flag = (List[i + 1], List[i], True) if List[i] &gt; List[i + 1] else (List[i], List[i + 1], flag)
        index_min = i if List[i] &lt; List[index_min] else index_min
    
    if not flag:
        break
    
    List[j], List[index_min] = (List[index_min], List[j]) if index_min != j else (List[j], List[index_min])
    


Log in to reply
 

Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2