Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

bubble sort python

def bubble(st):
    for i in range(len(st),1,-1):
        for j in range(0,i-1):
            if st[j]>st[j+1]:
                st[j],st[j+1]=st[j+1],st[j]
            else:
                pass
    print(st)
bubble([64, 34, 25, 12, 22, 11, 90] )
Comment

bubble sort python

s= [1,2,3,4,5,6,7,8,9,10]
for i in range(len(s)-1):
    for j in range(len(s)-1-i):
        if s[j]>s[j+1]:
            s[j],s[j+1] = s[j+1],s[j]
print(s)
Comment

python bubble sort

def bubble_sort(li_to_sort):
    # Looping from size of array from last index[-1] to index [0]
    for n in range(len(li_to_sort)-1, 0, -1):
        for i in range(n):
            if li_to_sort[i] > li_to_sort[i + 1]:
                # swapping data if the element is less than next element in the array
                li_to_sort[i], li_to_sort[i + 1] = li_to_sort[i + 1], li_to_sort[i]


li = [39, 12, 18, 85, 72, 10, 2, 18]

print("Unsorted list: ", li)
bubble_sort(li)
print("Sorted List: ", li)
Comment

Bubble sort python

for i in range(x - 1):
  for j in range(x - 1 - i):
    if s[j]>s[j + 1]:
      s[j],s[j + 1] = s[j + 1],s[j]
print(*s, sep=' ')
Comment

bubble sort in python

arr = [12,1,6,23,234,456,2,35,687,34]
# arry consists of 9 elements

n = len(arr)
#conts the element of the arry 

for j in range(n-1):            #this is for list to get shorted properly && you dont need to go full as it may take more steps to exicute
    for i in range(n-j-1):      #if left with n the there will be array error because it will try to add 1 to n in below leading to array being bigger                                             
        if arr[i]>arr[i+1]:                                         
            arr[i],arr[i+1]=arr[i+1],arr[i]
        else:
            pass
#       arry starts from last and eventually decrease the number lower and lower which leads to lesser steps
#       #above took 125 steps to eully exicute
#################################################################        
#       #this takes 217 steps to run and end code
# for i in range(n):
#     if arr[i]>arr[i+1]:
#         arr[i],arr[i+1]=arr[i+1],arr[i]        
#     else:
#         pass
#################################################################    
print("the sorted array is : "arr)
Comment

python bubble sort

def bubble_sort(nums):
    n = len(nums)
    for i in range(n):
        swapped = False
        for j in range(1, n - i):
            if nums[j] < nums[j - 1]:
                nums[j], nums[j - 1] = nums[j - 1], nums[j]
                swapped = True
        if not swapped: break
    return nums
print(bubble_sort([9, 8, 7, 6, 5, 4, 3, 2, 1]))
Comment

bubble sort in python

def bubbleSort(lis):
    length = len(lis)
    for i in range(length):
        for j in range(length - i):
            a = lis[j]
            if a != lis[-1]:
                b = lis[j + 1]
                if a > b:
                    lis[j] = b
                    lis[j + 1] = a
    return lis
Comment

python bubble

"""Bubblesort
"""
## Un-optimised--------------------------------------------------------------
def bubble_1(lst):
    n = len(lst) - 1
    for i in range(n):
        # Within the unsorted portion
        for j in range(n - i):
            # If curr > next, swap
            if lst[j] > lst[j+1]:
                lst[j], lst[j+1] = lst[j+1], lst[j]
    return lst # for easy testing

def bubble_2(lst):
    n = len(lst) - 1
    # Within the unsorted portion, except the last number
    for unsorted in range(n, 0, -1):
        for i in range(unsorted):
            # If curr > next, swap
            if lst[i] > lst[i+1]:
                lst[i], lst[i+1] = lst[i+1], lst[i]
    return lst # for easy testing

## Optimised-----------------------------------------------------------------
def bubble_3(lst):
    n = len(lst) - 1
    
    # Within the unsorted portion, except the last number
    for unsorted in range(n, 0, -1):
        swapped = False
        for i in range(unsorted):
            # If curr > next, swap
            if lst[i] > lst[i+1]:
                lst[i], lst[i+1] = lst[i+1], lst[i]
                swapped = True
        
        # Check if its sorted by this time
        if not swapped:
            break
    return lst # for easy testing
Comment

bubble sort python

# Python3 Optimized implementation
# of Bubble sort
 
# An optimized version of Bubble Sort
def bubbleSort(arr):
    n = len(arr)
  
    # Traverse through all array elements
    for i in range(n):
        swapped = False
 
        # 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] :
                arr[j], arr[j+1] = arr[j+1], arr[j]
                swapped = True
 
        # IF no two elements were swapped
        # by inner loop, then break
        if swapped == False:
            break
          
# Driver code to test above
arr = [64, 34, 25, 12, 22, 11, 90]
  
bubbleSort(arr)
  
print ("Sorted array :")
for i in range(len(arr)):
    print ("%d" %arr[i],end=" ")
 
# This code is contributed by Shreyanshi Arun
Comment

PREVIOUS NEXT
Code Example
Python :: why wont my python input accept string inputs 
Python :: python3 format leading 0 
Python :: add list of dictionaries to pandas dataframe 
Python :: python program to find sum of natural numbers using recursion 
Python :: python zip files 
Python :: download youtube video 
Python :: how to chang your facebook name 
Python :: python print every character in list as string 
Python :: how to make a grid in python 
Python :: import library to stop warnings in jupyter 
Python :: counter library python 
Python :: turtle python 
Python :: python library to convert decimal into octal and hexadecimal 
Python :: how to replace a string in python 
Python :: how to tell python to go back to a previous line 
Python :: Difference between append() and extend() method in Python 
Python :: how to check uppercase in python 
Python :: python plot speichern 
Python :: render to response django 
Python :: anaconda python 3.6 download 
Python :: python - gropuby based on 2 variabels 
Python :: IndexError: invalid index to scalar variable. 
Python :: python class destroying 
Python :: reverse a string in python 
Python :: for in list start with index python 
Python :: Using Python-docx to update cell content of a table 
Python :: Mac: Access your iCloud Documents folder with Jupyter Notebook or JupyterLab 
Python :: list comprehensions in python 
Python :: python loop nest shorthand 
Python :: Python - Sort Lists 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =