Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

optimizationed bubble sort optimizationed

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 :: how to search for a data in excel pandas 
Python :: python for android 
Python :: image blur in python 
Python :: Longest Common Prefix Method 2 
Python :: python remove duplicates from list of dict 
Python :: how to encode emoji to text in python 
Python :: how to make a numpy array of certain values 
Python :: python time limit for input 
Python :: python find string count in str 
Python :: python list of list to list of string 
Python :: create pandas dataframe 
Python :: python pass 
Python :: browser = webdriver.firefox() error 
Python :: python counter nested dictionary 
Python :: cos inverse in python numpy 
Python :: how to hide ticks in python 
Python :: create a window using tkinter 
Python :: python pathlib 
Python :: random choice sampling numpy 
Python :: jupyter notebook GET 500 
Python :: reverse array python 
Python :: clear list 
Python :: how to load user from jwt token request django 
Python :: random chars generator python 
Python :: python convert string to list 
Python :: Code to implement iterative Binary Search 
Python :: batch gradient descent 
Python :: selenium do not open browser window 
Python :: python check if number in string 
Python :: uninstall a python package from virtualenv 
ADD CONTENT
Topic
Content
Source link
Name
4+9 =