Question

write a python program Implement a “bouncing” bubble sort algorithm. In this version if bubble sort,...

write a python program

Implement a “bouncing” bubble sort algorithm. In this version if bubble sort, instead of making passes through a list that starts at the beginning andirons through to the end, you should reverse the direction each pass.That is , if the first pass starts at the beginning of the list and runs through to the end, the second pass out run from the end of the list back to the beginning and then the third pass would start at the beginning again.
Assume all items in the list are integers.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

# function for Pass from beginning of list
def passFromBegin(arr,index):
    # N is length of list
    N=len(arr)
    for i in range(0,int(N-index/2-1)):
        if(arr[i]>arr[i+1]):
            # swapping
            temp=arr[i]
            arr[i]=arr[i+1]
            arr[i+1]=temp
          
# function for Pass from end of list
def passFromEnd(arr, index):
    # N is length of list
    N=len(arr)
    for i in range(N-1,int(N-(N-index/2-1)),-1):
        if(arr[i-1]>arr[i]):
            #swapping
            temp=arr[i]
            arr[i]=arr[i-1]
            arr[i-1]=temp

# function for bubblesort
          
def bubbleSort(arr):
    # N is length of list
    N=len(arr)
    for i in range(0,N):
        # When i is even --> Pass from beginning of list
        if(i%2==0):
            passFromBegin(arr,i)
        # When i is odd --> pass from end of list
        else:
            passFromEnd(arr,i)

# function to print array/list         
def printArray(arr):
    # N is length of list
    N=len(arr)
    for i in range(0,N):
        print(arr[i],end = ' ')
    print(" ")
      

  
# List of integer
arr=[10,4,-7,3,12,9,90,23,653,20,-3,33,-222,67,4,678,8,110,222,0];
# call for bubblesort() for sorting
print("List before soring: ")
printArray(arr)
bubbleSort(arr)
# print sorted array
print("List after sorting")
printArray(arr)

Add a comment
Know the answer?
Add Answer to:
write a python program Implement a “bouncing” bubble sort algorithm. In this version if bubble sort,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT