Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

bubble sort with code optimization

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

PREVIOUS NEXT
Code Example
::  
Python :: python dataframe to excel 
Python ::  
Python :: how to print data type in python 
Python :: how do a plot on matplotlib python 
Python :: data where values in column starts with particular value 
::  
::  
:: Rectangle with python 
Python :: return max value in list python 
Python ::  
Python :: read ms word with python 
::  
Python :: List Delete a Element 
Python ::  
Python :: savefig matplotlib python 
Python ::  
Python :: read pickle file 
::  
Python :: use mongo replica set python 
Python :: dataframe check for nan in iterrows 
:: python rotate list 
:: python code for extracting data from pdf 
Python :: pandas create sample dataframe 
:: how to automatically install python packages 
Python ::  
:: how to add custom prefix in discord.py 
Python ::  
Python :: text from xml doc in python 
Python ::  
ADD CONTENT
Topic
Content
Source link
Name
6+5 =