Posts

Showing posts from October, 2023

Sorting Algorithms

Bubble Sort: Works by repeatedly swapping the adjacent elements if they are in the wrong order.  Not suitable for large data sets. In this algorithm,  traverse from left and compare adjacent elements and the higher one is placed at right side.  In this way, the largest element is moved to the rightmost end at first.  This process is then continued to find the second largest and place it and so on until the data is sorted. def bubbleSort(arr):      n = len (arr)             # Traverse through all array elements      for i in range (n-1):          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 (swapped = = False ):              break W