Your algorithm is not a bubble sort. Instead, it implements a version of selection sort tailored for a singly linked list. Let's break down why this is the case and differentiate it from bubble sort.
How Your Algorithm Works: Selection Sort
-
Initialization:
- You create a dummy node to serve as the starting point of the sorted list.
currentDummy is used to build the sorted list incrementally.
-
Finding the Minimum Node:
- For each iteration, you traverse the unsorted portion of the list starting from
head to find the node with the minimum value (minNode).
- You keep track of the previous node (
minPrev) to easily remove the minNode from its current position.
-
Appending the Minimum Node:
- Once the minimum node is found, it's appended to the sorted portion of the list by updating
currentDummy.
- If the
minNode is not the head, you adjust the next pointers to exclude it from its original position.
- Update
head if the minimum node was the head of the remaining list.
-
Iteration:
- Repeat the process until all nodes are moved from the unsorted portion to the sorted portion.
Why It's Selection Sort and Not Bubble Sort
Your algorithm systematically selects the smallest remaining element and appends it to the sorted list, adhering to the principles of selection sort rather than bubble sort.
Advantages of Selection Sort for Linked Lists
- Minimizes Pointer Changes: Selection sort requires fewer pointer manipulations compared to bubble sort, making it more efficient for singly linked lists.
- Simplicity: The algorithm is straightforward to implement without needing to keep track of multiple pointers for swapping adjacent nodes.
Potential Optimization
While selection sort has a time complexity of (O(n^2)), you might consider more efficient sorting algorithms for linked lists, such as merge sort, which can achieve (O(n \log n)) time complexity and is well-suited for linked list structures.
Summary
- Your Algorithm: Implements selection sort by repeatedly selecting the minimum node from the unsorted portion and appending it to the sorted list.
- Not Bubble Sort: It doesn't involve comparing and swapping adjacent nodes through multiple passes.
If you have further questions or need assistance with optimizing your linked list sorting algorithm, feel free to ask!