Bubble sort

    0

    0

    Mahendra Kumar

    intermediatesortinglogicβ€’β€’β€’

    Bubble sort is a sorting algorithm that compares two adjacent elements and swaps them until they are not in the intended order.

    def bubbleSort(array):
      # loop to access each array element
      for i in range(len(array)):
        # 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 elements if elements
            # are not in the intended order
            temp = array[j]
            array[j] = array[j+1]
            array[j+1] = temp
    data = [-2, 45, 0, 11, -9]
    bubbleSort(data)
    print('Sorted Array in Ascending Order:')
    print(data)
    
    Codiga Logo
    Codiga Hub
    • Rulesets
    • Playground
    • Snippets
    • Cookbooks
    Legal
    • Security
    • Privacy Policy
    • Code Privacy
    • Terms of Service
    soc-2 icon

    We are SOC-2 Compliance Certified

    G2 high performer medal

    Codiga – All rights reserved 2022.