Bubble Sorting Python3

import signal
import sys

def sort(input):
    total = 0
    isSorted = False
    ran = len(input) - 1
    while not isSorted:
        isSorted = True
        for j in range(0,ran):
            print(isSorted)
            total +=1
            if input[j] > input[j+1]:
                isSorted = False
                tmp = input[j]
                input[j] = input[j+1]
                input[j+1] = tmp
        ran -= 1
    print(input)
    print(total)

def signal_handler(signal, frame):
        print('You pressed Ctrl+C!')
        sys.exit(0)

def main():
    signal.signal(signal.SIGINT, signal_handler)
    print('Press Ctrl+C to stop, input the array split with space')
    for line in sys.stdin:
        print(line)
        arr = [int(elem) for elem in line.split()]
        sort(arr)

if __name__ == "__main__": main()

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s