Strings Variables Lists Tuples Dictionary Control Function Files Exceptions OOP Algorithm Data Structure back
Algorithms
Sorting Algorithms
Bubble Sort
- define a function bubble_sort that takes a list as an argument.
- The outer loop runs n times, where n is the length of the list.
- The inner loop compares adjacent elements and swaps them if they are in the wrong order.
- This process is repeated until the entire list is sorted.
- Finally, we call the bubble_sort function with an example list and print the sorted list.
def bubble_sort(lst):
n = len(lst)
# Traverse through all elements in the list
for i in range(n):
# Last i elements are already in place
for j in range(0, n-i-1):
# Compare adjacent elements
if lst[j] > lst[j+1]:
# Swap elements if they are in the wrong order
lst[j], lst[j+1] = lst[j+1], lst[j]
# Example
my_list = [64, 34, 25, 12, 22, 11, 90]
bubble_sort(my_list)
print("Sorted list:", my_list)
# OUTPUT
# Sorted list: [11, 12, 22, 25, 34, 64, 90]
Insertion Sort
Selection Sort
Merge Sort
Quick Sort
### Searching Algorithms
- Linear Search
```python
- Binary Search
Write a Python function to reverse a string.
def reverse_string(input_string):
return input_string[::-1]