Algorithm:
- Set
n
to the length of the list. - Repeat the following steps
n
times:- Iterate through the list from index 0 to
n - 1
. - Compare each pair of adjacent elements.
- If the elements are in the wrong order, swap them.
- Iterate through the list from index 0 to
- After
n
iterations, the list will be sorted.
Time Complexity:
- Best Case: O(n) (when the list is already sorted)
- Worst Case: O(n^2) (when the list is sorted in reverse order)
Space Complexity:
- O(1) (in-place sorting)
Algorithm:
- Set
n
to the length of the list. - Repeat the following steps
n
times:- Find the minimum element in the unsorted part of the list.
- Swap the minimum element with the first unsorted element.
- Move the boundary of the sorted part one element to the right.
- After
n
iterations, the list will be sorted.
Time Complexity:
- Best Case: O(n^2)
- Worst Case: O(n^2)
Space Complexity:
- O(1) (in-place sorting)
Algorithm:
- Set
n
to the length of the list. - Iterate through the list from index 1 to
n - 1
.- Take the current element and insert it into the correct position in the sorted part of the list.
- Move all elements greater than the current element one position to the right.
- After the iteration, the list will be sorted.
Time Complexity:
- Best Case: O(n) (when the list is already sorted)
- Worst Case: O(n^2)
Space Complexity:
- O(1) (in-place sorting)
Algorithm:
- If the list has fewer than two elements, return the list (base case).
- Divide the list into two halves.
- Recursively sort the left half.
- Recursively sort the right half.
- Merge the two sorted halves to obtain a sorted list.
Time Complexity:
- Best Case: O(n log n)
- Worst Case: O(n log n)
Space Complexity:
- O(n) (requires additional space for merging)
Algorithm:
- If the list has fewer than two elements, return the list (base case).
- Select a pivot element from the list.
- Partition the list into two sublists: elements less than the pivot and elements greater than the pivot.
- Recursively sort the sublists created in the previous step.
- Concatenate the sorted sublists with the pivot to obtain a sorted list.
Time Complexity:
- Best Case: O(n log n)
- Worst Case: O(n^2) (rare, but can occur if the pivot selection is not optimal)
Space Complexity:
- O(log n) (recursive stack space)
Algorithm:
- Build a max-heap from the list.
- Swap the root (maximum element) with the last element of the heap and decrease the heap size.
- Heapify the reduced heap to maintain the max-heap property.
- Repeat steps 2 and 3 until the heap is empty.
- The elements extracted in step 2 form a sorted list.
Time Complexity:
- Best Case: O(n log n)
- Worst Case: O(n log n)
Space Complexity:
- O(1) (in-place sorting)
Algorithm:
- Find the maximum element in the list and determine the range of values.
- Initialize a count array of size equal to the range, and set all elements to zero.
- Count the number of occurrences of each unique element and store the count in the count array.
- Modify the count array to contain the cumulative sum of the counts.
- Create an output array of the same size as the input array.
- Iterate through the input array from right to left:
- Use the count array to determine the correct position of each element in the output array.
- Decrease the count for each element.
- The output array will contain the sorted elements.
Time Complexity:
- Best Case: O(n + k) (when the range of input values is small)
- Worst Case: O(n + k)
Space Complexity:
- O(n + k) (requires additional space for the count and output arrays)
Algorithm:
- Find the maximum element in the list and determine the number of digits in it.
- Initialize 10 buckets (0-9).
- Iterate through each digit position from the least significant to the most significant:
- Distribute the elements into the buckets based on the current digit.
- Collect the elements from the buckets in the original order.
- After processing all the digits, the list will be sorted.
Time Complexity:
- Best Case: O(d * (n + k)) (d is the number of digits, k is the range of input values)
- Worst Case: O(d * (n + k))
Space Complexity:
- O(n + k) (requires additional space for the buckets)
Algorithm:
- Choose a gap sequence (e.g., Knuth's sequence or Hibbard's sequence).
- Starting with the largest gap, divide the list into sublists of elements separated by the gap.
- Sort each sublist using Insertion Sort.
- Reduce the gap and repeat steps 2 and 3 until the gap becomes 1.
- Perform a final pass of Insertion Sort to ensure the list is fully sorted.
Time Complexity:
- Best Case: O(n log n) (depends on the gap sequence)
- Worst Case: O(n^2)
Space Complexity:
- O(1) (in-place sorting)
##Bucket Sort
Algorithm:
- Create an array of empty buckets.
- Iterate through the input list and distribute each element into the corresponding bucket based on its value.
- Sort each bucket individually, either using a different sorting algorithm or recursively applying the bucket sort.
- Concatenate the sorted elements from all the buckets to obtain the sorted list.
Time Complexity:
- Best Case: O(n + k), where n is the number of elements and k is the number of buckets.
- Worst Case: O(n^2) (if all the elements are placed in the same bucket and require additional sorting)
Space Complexity:
- O(n + k) (requires additional space for the buckets)
Algorithm:
- Set the gap value
gap
to the length of the list. - Repeat the following steps until the gap becomes 1:
- Set
gap
to the integer division ofgap
by a shrink factor (e.g., 1.3). - Iterate through the list from index 0 to
n - gap - 1
. - Compare each pair of elements that are
gap
positions apart. - If the elements are in the wrong order, swap them.
- Set
- Perform a final pass using the gap value of 1 (similar to Bubble Sort) to ensure the list is completely sorted.
Time Complexity:
- Best Case: O(n log n)
- Worst Case: O(n^2)
Space Complexity:
- O(1) (in-place sorting)
Algorithm:
- Iterate through each element in the list.
- For each element, do the following until the element is in its correct position:
- Find the correct position for the current element by counting the number of elements that are smaller than it.
- If there are no smaller elements, move to the next element.
- Otherwise, swap the current element with the element at its correct position.
- Repeat step 2 for all remaining elements in the list.
Time Complexity:
- Best Case: O(n^2)
- Worst Case: O(n^2)
Space Complexity:
- O(1) (in-place sorting)
Algorithm:
- Set the current position
pos
to 0. - Repeat the following steps until
pos
reaches the end of the list:- If
pos
is 0, incrementpos
. - If the element at position
pos
is greater than the previous element, incrementpos
. - Otherwise, swap the current element with the previous element and decrement
pos
.
- If
- After reaching the end of the list, it will be sorted.
Time Complexity:
- Best Case: O(n) (when the list is already sorted)
- Worst Case: O(n^2)
Space Complexity:
- O(1) (in-place sorting)
Algorithm:
- Repeat the following steps until the list is sorted:
- Perform a Bubble Sort pass on the odd-indexed elements (starting from index 1).
- Perform a Bubble Sort pass on the even-indexed elements (starting from index 0).
- After the iterations, the list will be sorted.
Time Complexity:
- Best Case: O(n) (when the list is already sorted)
- Worst Case: O(n^2)
Space Complexity:
- O(1) (in-place sorting)
Algorithm:
- Set the
start
andend
indices of the unsorted part of the list. - Repeat the following steps until the list is sorted:
- Perform a forward pass from
start
toend
, swapping adjacent elements if they are in the wrong order. - Decrement
end
. - Perform a backward pass from
end
tostart
, swapping adjacent elements if they are in the wrong order. - Increment
start
.
- Perform a forward pass from
- After the iterations, the list will be sorted.
Time Complexity:
- Best Case: O(n) (when the list is already sorted)
- Worst Case: O(n^2)
Space Complexity:
- O(1) (in-place sorting)
Algorithm:
- Start with the entire list of elements.
- Find the index of the maximum element in the current unsorted part of the list.
- Flip the list from the start to the maximum element's index to bring the maximum element to the front.
- Flip the entire list to move the maximum element to its correct position at the end.
- Repeat steps 2-4 for the remaining unsorted part of the list.
- After the iterations, the list will be sorted.
Time Complexity:
- Best Case: O(n^2)
- Worst Case: O(n^2)
Space Complexity:
- O(1) (in-place sorting)
Algorithm:
- Create an array of empty "rails" equal to the maximum element in the list.
- For each element in the list, add "beads" to the corresponding rail by incrementing the value at that position.
- For each rail, let the beads fall under gravity and collect them in sorted order.
- The collected beads represent the sorted list.
Time Complexity:
- Best Case: O(n)
- Worst Case: O(n^2)
Space Complexity:
- O(k) (requires additional space for the rails, where k is the maximum element in the list)
##Shell Merge Sort
Algorithm:
- Define a sequence of gap values to be used for initial sorting.
- For each gap value in the sequence:
- Perform Shell Sort with the current gap value to partially sort the list.
- Merge the partially sorted sublists using the regular merge operation of Merge Sort.
- After merging all the sublists, the list will be sorted.
Time Complexity:
- Best Case: O(n log^2 n)
- Worst Case: O(n log^2 n)
Space Complexity:
- O(n) (requires additional space for merging)
Algorithm:
- Create a Leonardo heap data structure to represent the input list.
- Build the Leonardo heap from the input list.
- Repeat the following steps until the heap is empty:
- Extract the maximum element from the Leonardo heap.
- Recursively restore the heap property on the remaining elements.
- The extracted elements form the sorted list.
Time Complexity:
- Best Case: O(n)
- Worst Case: O(n log n)
Space Complexity:
- O(1) (in-place sorting)
Algorithm:
- If the input list is empty, return an empty list (base case).
- Identify a sublist of elements that are in increasing order from the input list.
- Remove the identified sublist from the input list.
- Recursively repeat steps 2-4 with the remaining elements of the input list.
- Merge the sorted sublists obtained from the recursive calls to obtain the sorted list.
Time Complexity:
- Best Case: O(n)
- Worst Case: O(n^2)
Space Complexity:
- O(n) (requires additional space for merging)
Algorithm:
- Divide the input list into small sublists (called runs) using Insertion Sort or other sorting algorithms.
- Merge the runs using a merging algorithm (such as the merging algorithm of Merge Sort).
- Repeat steps 1-2 until a single sorted list is obtained.
Time Complexity:
- Best Case: O(n)
- Worst Case: O(n log n)
Space Complexity:
- O(n) (requires additional space for merging)