Python Tutorial
Python Variable
Python Operators
Python Sequence
Python String
Python Flow Control
Python Functions
Python Class and Object
Python Class Members (properties and methods)
Python Exception Handling
Python Modules
Python File Operations (I/O)
Bubble Sort is a simple sorting algorithm that works by repeatedly stepping through the list, comparing adjacent elements and swapping them if they are in the wrong order. Here's an example of Bubble Sort implemented in Python using nested loops:
def bubble_sort(arr): n = len(arr) # Traverse through all elements in the list for i in range(n): # Last i elements are already in place, so we don't need to check them for j in range(0, 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] # Example usage: arr = [64, 34, 25, 12, 22, 11, 90] print("Unsorted array is:", arr) bubble_sort(arr) print("Sorted array is:", arr)
In this example, the function bubble_sort
takes a list arr
as input and sorts it using the Bubble Sort algorithm. The outer loop (controlled by i
) iterates through all elements in the list, while the inner loop (controlled by j
) compares adjacent elements and swaps them if they're in the wrong order. The inner loop doesn't need to check the last i
elements, as they are already in their correct positions after each pass.
When running the example code, the output would be:
Unsorted array is: [64, 34, 25, 12, 22, 11, 90] Sorted array is: [11, 12, 22, 25, 34, 64, 90]
Keep in mind that Bubble Sort has a worst-case and average time complexity of O(n^2), where n is the number of items being sorted. Therefore, it's not the most efficient sorting algorithm for large datasets. Other algorithms, like Quick Sort or Merge Sort, are more suitable for larger datasets.
How to write a bubble sort algorithm in Python:
def bubble_sort(arr): n = len(arr) for i in range(n): for j in range(0, n-i-1): if arr[j] > arr[j+1]: arr[j], arr[j+1] = arr[j+1], arr[j] my_list = [64, 34, 25, 12, 22, 11, 90] bubble_sort(my_list) print("Sorted list:", my_list)
Nested loops for bubble sort in Python:
def bubble_sort(arr): n = len(arr) for i in range(n): for j in range(0, n-i-1): # Comparison and swapping logic if arr[j] > arr[j+1]: arr[j], arr[j+1] = arr[j+1], arr[j]
Comparing elements in nested loops for bubble sort:
def bubble_sort(arr): n = len(arr) for i in range(n): for j in range(0, n-i-1): if arr[j] > arr[j+1]: # Swap elements arr[j], arr[j+1] = arr[j+1], arr[j]
Optimizing bubble sort with Python nested loops:
def bubble_sort(arr): n = len(arr) for i in range(n): swapped = False for j in range(0, n-i-1): if arr[j] > arr[j+1]: arr[j], arr[j+1] = arr[j+1], arr[j] swapped = True if not swapped: break
Efficient bubble sort implementation using nested loops:
def bubble_sort(arr): n = len(arr) for i in range(n): swapped = False for j in range(0, n-i-1): if arr[j] > arr[j+1]: arr[j], arr[j+1] = arr[j+1], arr[j] swapped = True if not swapped: break
Sorting lists and arrays with bubble sort in Python:
my_list = [64, 34, 25, 12, 22, 11, 90] bubble_sort(my_list) print("Sorted list:", my_list) my_array = np.array([64, 34, 25, 12, 22, 11, 90]) bubble_sort(my_array) print("Sorted array:", my_array)
Handling edge cases and variations in bubble sort:
def bubble_sort(arr, reverse=False): n = len(arr) for i in range(n): swapped = False for j in range(0, n-i-1): compare_result = arr[j] > arr[j+1] if not reverse else arr[j] < arr[j+1] if compare_result: arr[j], arr[j+1] = arr[j+1], arr[j] swapped = True if not swapped: break