Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to store each step of Bubble Sort into a list of list

I need to store the image of array after every inner loop so that I can process it later somewhere else.

def bubbleSort(arr):
    sort_list = [[]]
    n = len(arr)
    # optimize code, so if the array is already sorted, it doesn't need
    # to go through the entire process
    swapped = False
    print(arr)
    # Traverse through all array elements
    for i in range(n - 1):
        # range(n) also work but outer loop will
        # repeat one time more than needed.
        # Last i elements are already in place
        for j in range(0, n - i - 1):
            sort_list.append(arr)
            # traverse the array from 0 to n-i-1
            # Swap if the element found is greater
            # than the next element
            if arr[j] > arr[j + 1]:
                swapped = True
                arr[j], arr[j + 1] = arr[j + 1], arr[j]

        if not swapped:
            # if we haven't needed to make a single swap, we
            # can just exit the main loop.
            return
    print("sorted arr", sort_list)
    
arr = [2,1,5,7,3]        
bubbleSort(arr)

The output I am getting is this:

[2, 1, 5, 7, 3]
sorted arr [[], [1, 2, 3, 5, 7], [1, 2, 3, 5, 7], [1,2, 3, 5, 7], [1, 2, 3, 5, 7],
[1, 2, 3, 5, 7], [1, 2, 3, 5, 7], [1, 2, 3, 5, 7], [1, 2, 3, 5, 7], [1, 2, 3, 5, 7],
[1, 2, 3, 5, 7]]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I have a feeling that I am doing something stupid but the logic seems so obvious that I cannot imagine why it doesn’t work.

>Solution :

Does this work?

def bubbleSort(arr):
    sort_list = []
    n = len(arr)
    # optimize code, so if the array is already sorted, it doesn't need
    # to go through the entire process
    swapped = False
    print(arr)
    # Traverse through all array elements
    for i in range(n - 1):
        # range(n) also work but outer loop will
        # repeat one time more than needed.
        # Last i elements are already in place
        for j in range(0, n - i - 1):
            # traverse the array from 0 to n-i-1
            # Swap if the element found is greater
            # than the next element
            if arr[j] > arr[j + 1]:
                swapped = True
                arr[j], arr[j + 1] = arr[j + 1], arr[j]
        sort_list.append(arr.copy())
        if not swapped:
            # if we haven't needed to make a single swap, we
            # can just exit the main loop.
            return
    print("sorted arr", sort_list)
    
arr = [7,5,3,2,1]        
bubbleSort(arr)

OUTPUT:

[[5, 3, 2, 1, 7], [3, 2, 1, 5, 7], [2, 1, 3, 5, 7], [1, 2, 3, 5, 7]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading