ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCC-213Bubble Sort
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    CC-213

    Data Structures & Algorithms

    Progress0 / 37 done
    Topics37
    1Abstract Data Types2Complexity Analysis3Big Oh Notation4Stacks (Linked Lists and Array Implementations)5Recursion and analyzing recursive algorithms6Divide and Conquer Algorithms7Sorting Algorithms8Selection Sort9Insertion Sort10Merge Sort11Quick Sort12Bubble Sort13Heap Sort14Shell Sort15Radix Sort16Bucket Sort17Queue18Dequeuer19Priority Queues (linked and array implementations of queues)20Linked List and Its Various Types21Sorted Linked List22Searching an Unsorted Array23Binary Search for Sorted Arrays24Hashing and Indexing25Open Addressing and Chaining26Trees and Tree Traversals27Binary Search Trees28Heaps29M-Way Trees30Balanced Trees31Graphs32Breadth-First Traversal33Depth-First Traversal34Topological Order35Shortest Path36Adjacency Matrix and Adjacency List Implementations37Memory Management and Garbage Collection
    3 min read
    Topic 12 of 37
    Topic Study Note Illustration
    12

    Bubble Sort

    Description : Bubble sort is one of the simplest sorting algorithms. It works by repeatedly stepping through the list, comparing adjacent elements, and swapping them if they...

    Bubble Sort

    Description: Bubble sort is one of the simplest sorting algorithms. It works by repeatedly stepping through the list, comparing adjacent elements, and swapping them if they are in the wrong order. This process is repeated until the list is sorted. The name "bubble sort" comes from the way smaller elements "bubble" to the top (beginning) of the list.

    How It Works

    1. Initial Pass: Start at the beginning of the array.
    2. Comparison: Compare each pair of adjacent elements.
    3. Swap: If the first element is greater than the second, swap them.
    4. Repeat: Continue this process for each pair until the end of the array is reached.
    5. Multiple Passes: Repeat the entire pass over the array until no swaps are needed (indicating that the array is sorted).

    Time Complexity

    • Worst Case: O(n2)O(n^2)O(n2) — occurs when the array is sorted in reverse order.
    • Average Case: O(n2)O(n^2)O(n2)
    • Best Case: O(n)O(n)O(n) — occurs when the array is already sorted (with an optimized version).

    Space Complexity

    • Space Complexity: O(1)O(1)O(1) — bubble sort is an in-place sorting algorithm, requiring a constant amount of additional storage.

    Stability

    • Stability: Bubble sort is a stable sorting algorithm. Equal elements maintain their relative order after sorting.

    Example Code

    Here's a C++ implementation of bubble sort:

    #include <iostream>
    using namespace std;
    
    void bubbleSort(int arr[], int n) {
        bool swapped; // Flag to check if a swap was made
        for (int i = 0; i < n - 1; i++) {
            swapped = false; // Reset swap flag for this pass
            for (int j = 0; j < n - i - 1; j++) {
                // Compare adjacent elements
                if (arr[j] > arr[j + 1]) {
                    swap(arr[j], arr[j + 1]); // Swap if they're in the wrong order
                    swapped = true; // Mark that a swap has occurred
                }
            }
            // If no swaps occurred, the array is already sorted
            if (!swapped)
                break;
        }
    }
    
    int main() {
        int arr[] = {64, 34, 25, 12, 22, 11, 90};
        int n = sizeof(arr) / sizeof(arr[0]);
        bubbleSort(arr, n);
    
        cout << "Sorted array: ";
        for (int i = 0; i < n; i++) {
            cout << arr[i] << " ";
        }
        cout << endl;
    
        return 0;
    }
    

    Explanation of the Code

    1. Function Definition: The bubbleSort function takes an array and its size as parameters.
    2. Outer Loop: Iterates through the array, making passes to sort it.
    3. Inner Loop: Compares each pair of adjacent elements and swaps them if necessary.
    4. Swap Flag: The swapped flag tracks whether any swaps were made during a pass. If no swaps occur, the array is already sorted, and the loop can exit early.
    5. Output: After sorting, the program prints the sorted array.

    Conclusion

    Bubble sort is a straightforward algorithm that is easy to understand and implement. However, its O(n2)O(n^2)O(n2) time complexity makes it inefficient for large datasets compared to more advanced sorting algorithms like quick sort or merge sort. Despite its simplicity, bubble sort is often used in educational contexts to introduce sorting concepts.

    ← Previous topic 11
    Quick Sort
    Next topic 13 →
    Heap Sort

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    3mremaining
    589words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Data Structures & Algorithms