简体   繁体   中英

Python bubble sort breaks with large numbers only

Im trying to create a bubble sort test algorithm that generates x amount of random integers and outputs to console and a text file. The number of numbers created as well as the max value for the random integers is determined by the variable bigsize. The code seems to work up to around when big size is ~2300, sometimes its more and sometimes it's less. I can always get 2000 to work though.

Edit: Also worth noting, it seems that the code breaks during the sorting process, as I get get a file to output unsorted numbers with no issues.

import random
import sys

bigsize = 2000

def main():
    sys.setrecursionlimit(7000)
    array = create_list()
    print_array(array)
    bubble_sort(array)
    display_out(array)
      
def create_list():
    array = [0] * bigsize
    for x in range(bigsize):
        array[x] = random.randint(0, bigsize)
    return array

def bubble_sort(array):
    increment = 0
    buffer = 0
    for x in range(len(array) - 1):
        if (array[increment + 1] <= array[increment]):
            buffer = array[increment + 1]
            array[increment + 1] = array[increment]
            array[increment] = buffer
        increment = increment + 1
    increment = 0
    for x in range(len(array) - 1):
        if (array[increment + 1] >= array[increment]):
            increment = increment + 1
        elif (array[increment + 1] < array[increment]):
            bubble_sort(array)

def display_out(array):
    for x in range(bigsize):
        print(array[x])
 
main()

You have a dysfunctional sort. First and foremost, there is nothing useful about your recursion: you don't reduce the task -- you simply use recursion in place of the sort's outer loop. At that, you have implemented it incorrectly. I strongly recommend that you get more practice with the more basic skills of programming before you tackle recursion.

The first (non-)problem is a simple inefficiency: your loop has x as the index, but the loop body ignores x , while it maintains increment with the same value . There is no reason to have two separate variables. You can see how this is used in almost any bubble sort on the web:

for pos in range(len(array) - 1):
    if array[pos+1] < array[pos]:
        # switch the elements

You have a similar inefficiency in the second loop:

increment = 0
for x in range(len(array) - 1):
    if (array[increment + 1] >= array[increment]):
        increment = increment + 1

Again, you ignore x and maintain increment at the same value... up until you find elements out of order:

    elif (array[increment + 1] < array[increment]):
        bubble_sort(array)

When you do so, you recur, but without altering increment . When you return from this recursion, the array must be properly sorted (assuming that the recursion logic is correct), and then you continue with this loop, iterating through the now-sorted array, ensuring up to bigsize times that the array is in order.

This entire loop is silly: if you simply set a flag when you make a switch in the first loop, you'll know whether or not you need to sort again. You'll do one extra iteration, but that doesn't affect the time complexity. For instance:

done = True
for pos in range(len(array) - 1):
    if array[pos+1] < array[pos]:
        array[pos], array[pos+1] = array[pos+1], array[pos]
        
# Replace the second loop entirely
if not done:
    bubble_sort(array)

I strongly recommend that you check the operation of your program by properly tracing the results. First, however, clean up the logic. Remove (for now) the superfluous code that writes to files, put in some basic tracing print statements, and study existing bubble sorts to see where you're making this all too "wordy". In fact, remove the recursion and simply repeat the sorting until done .

When I try this with bigsize=5000 , it recurs to 3818 levels and quits. I'll leave the tracing up to you, if the problem is still there once you've cleaned up the program. There's not much point to fixing the "silent death" until you tighten the logic and trace the operation, so that you know you're fixing an otherwise working program. The current code does not "Make it easy for others to help you", as the posting guidelines say.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM