Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

buble short

# Optimized Bubble sort in Python

def bubbleSort(array):
    
  # loop through each element of array
  for i in range(len(array)):
        
    # keep track of swapping
    swapped = False
    
    # loop to compare array elements
    for j in range(0, len(array) - i - 1):

      # compare two adjacent elements
      # change > to < to sort in descending order
      if array[j] > array[j + 1]:

        # swapping occurs if elements
        # are not in the intended order
        temp = array[j]
        array[j] = array[j+1]
        array[j+1] = temp

        swapped = True
          
    # no swapping means the array is already sorted
    # so no need for further comparison
    if not swapped:
      break

data = [-2, 45, 0, 11, -9]

bubbleSort(data)

print('Sorted Array in Ascending Order:')
print(data)
Comment

PREVIOUS NEXT
Code Example
Python :: replace NaN value in pandas data frame 
Python :: asyncioevents.py", line 504, in add_reader raise NotImplementedError 
Python :: transcript with timestamps in python 
Python :: join mulitple dataframe pandas index 
Python :: python aus liste tuple machen 
Python :: pydantic numpy ndarray type 
Python :: python programming online editor 
Python :: supercharged python 
Python :: Python program for getting url, hostname, port numbers 
Python :: if statement collection python 
Python :: python goose 
Python :: summary r language equivalent in python 
Python :: python glob sort numerically 
Python :: receive ouput subprocess call 
Python :: nltk python text from url 
Python :: adding if statements and for loop in pyhton with tuple 
Python :: python generic class inheritance 
Python :: plotly change marker symboly sequence 
Python :: pillow update image 
Python :: What is the expected value of print to this program X = 2 Y = 3 Z = X + Y print(Y) #Z 
Python :: how to put quotes in string python 
Python :: Filter by len() 
Python :: crop a video opencv 
Python :: flask get summernote text 
Python :: cvhaardetectobjects 
Python :: ler arquivo xls no pandas 
Python :: How to put a header title per dataframe after concatenate using pandas in python 
Python :: nlp.Defaults.stop_words.add spacy 
Python :: ya mom 
Python :: how to do fibonacci sequence in python 
ADD CONTENT
Topic
Content
Source link
Name
8+3 =