Day 41: Sorting Algorithms with Python

Day 41: Sorting Algorithms with Python

ยท

2 min read

Today is Forty-one blog of my Python learning and In this blog, we will discuss sorting algorithms. Sorting algorithms are fundamental in computer science and are used to arrange data in a specified order. Python provides built-in functions as well as libraries for implementing various sorting algorithms efficiently.

List of Algorithms

  • Selection sort: This algorithm employs a strategy of selecting the minimum element and placing it at the beginning of the list. This process is repeated iteratively until the entire list becomes sorted.

      def selection_sort(arr):
          n = len(arr)
          for i in range(n):
              min_idx = i
              for j in range(i + 1, n):
                  if arr[j] < arr[min_idx]:
                      min_idx = j
              arr[i], arr[min_idx] = arr[min_idx], arr[i]
          return arr
    
      # Example usage
      my_list = [5, 2, 9, 1, 5]
      sorted_list = selection_sort(my_list)
      print(sorted_list)  # Output: [1, 2, 5, 5, 9]
    
  • Bubble Sort: Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. It continues this process until the list is sorted.

      def bubble_sort(arr):
          n = len(arr)
          for i in range(n - 1):
              for j in range(0, n - i - 1):
                  if arr[j] > arr[j + 1]:
                      arr[j], arr[j + 1] = arr[j + 1], arr[j]
          return arr
    
      # Example usage
      my_list = [5, 2, 9, 1, 5]
      sorted_list = bubble_sort(my_list)
      print(sorted_list)  # Output: [1, 2, 5, 5, 9]
    
  • Insertion Sort: Insertion sort builds the final sorted list one item at a time by inserting each element into its correct position relative to the already sorted part of the list.

      def insertion_sort(arr):
          for i in range(1, len(arr)):
              key = arr[i]
              j = i - 1
              while j >= 0 and key < arr[j]:
                  arr[j + 1] = arr[j]
                  j -= 1
              arr[j + 1] = key
          return arr
    
      # Example usage
      my_list = [5, 2, 9, 1, 5]
      sorted_list = insertion_sort(my_list)
      print(sorted_list)  # Output: [1, 2, 5, 5, 9]
    

Conclusion

his blog explores three key sorting algorithms in Python: Selection Sort, Bubble Sort, and Insertion Sort.

  1. Selection Sort: Repeatedly selects the smallest element and places it at the start until the list is sorted.

  2. Bubble Sort: Compares adjacent elements, swapping them if in the wrong order, gradually moving larger elements to the end.

  3. Insertion Sort: Builds the sorted list by inserting each element into its correct position relative to the sorted part of the list.

Understanding these algorithms is vital for efficient data sorting in Python. Python's built-in functions and libraries further enhance sorting capabilities for various data types and sizes.

Thank you๐Ÿ’•๐Ÿ’•

ย