Built with Tutify.io

    AIStudyLabs

    Sort a List Without Built-in Functions

    Sort a List Without Built-in Functions

    Introduction

    In this lesson, we will explore list sorting algorithms and implement them without relying on Python's built-in functions. Understanding the mechanics behind sorting is crucial for optimizing performance in your programs. We will cover basic sorting algorithms such as bubble sort, selection sort, and insertion sort, and discuss their efficiencies in different scenarios.

    Understanding List Sorting Algorithms

    Sorting is a fundamental operation in many applications, and different algorithms can be used depending on the complexity and requirements of the task.

    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. The pass through the list is repeated until the list is sorted.

    # Bubble Sort Implementation
    
    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]
        return arr
    
    # Example
    numbers = [64, 34, 25, 12, 22, 11, 90]
    sorted_numbers = bubble_sort(numbers)
    print("Sorted list:", sorted_numbers)
    

    Selection Sort

    Selection sort divides the list into a sorted and unsorted region. It repeatedly selects the smallest (or largest, depending on the order) element from the unsorted region and moves it to the end of the sorted region.

    # Selection Sort Implementation
    
    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
    numbers = [64, 34, 25, 12, 22, 11, 90]
    sorted_numbers = selection_sort(numbers)
    print("Sorted list:", sorted_numbers)
    

    Insertion Sort

    Insertion sort builds the final sorted array one item at a time. It has the advantage of being quite efficient on small data sets or lists that are already partially sorted.

    # Insertion Sort Implementation
    
    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
    numbers = [64, 34, 25, 12, 22, 11, 90]
    sorted_numbers = insertion_sort(numbers)
    print("Sorted list:", sorted_numbers)
    

    Comparing Sorting Algorithm Efficiencies

    These basic algorithms provide a foundation in sorting concepts, but they highlight why using Python's sorted() or list.sort() methods is often ideal in practice due to their sophisticated implementations.

    Summary / Key Takeaways

    In this lesson, you have learned:

    Master Python by Writing 50 Programs — List-Based Programs

    Python Basics Programs

    Number-Based Programs

    Loops and Mathematical Programs

    String-Based Programs

    List-Based Programs

    Dictionary and Set Programs

    Chapter 7: File Handling and Real Programs

    Tutorial Not Found