Bubble sort, sometimes referred to as sinking sort, is a
simple sorting algorithm that repeatedly steps through the input list element
by element, comparing the current element with the one after it, swapping their
values if needed. These passes through the list are repeated until no swaps
have to be performed during a pass, meaning that the list has become fully
sorted. The algorithm, which is a comparison sort, is named for the way the
larger elements "bubble" up to the top of the list.
Java
package
com.kartik.sorting; |
The code you've provided is an implementation of the Bubble
Sort algorithm in Java. 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.
Explanation of the Code:
- Bubble
Sort Algorithm:
- The
algorithm repeatedly steps through the list, compares adjacent elements,
and swaps them if they are in the wrong order.
- The
process is repeated for each element in the array until no more swaps are
needed, which indicates that the array is sorted.
- bubbleshort
Method:
- Parameters: Takes an integer array array as input.
- Logic:
- The outer loop runs through each element in the array.
- The inner loop compares the current element with the next element. If the current element is greater than the next, they are swapped using the swapNumbers method.
- After each pass through the array, the current state of the array is printed to the console.
- swapNumbers
Method:
- A utility method to swap the values of two elements in the array.
- printNumbers
Method:
- A utility method to print the current state of the array to the console.
- main
Method:
- Creates
an array input and calls the bubbleshort method to sort the array.
- The
array is printed after each iteration of the outer loop to show the
sorting process step by step.
Example Output:
When you run the code with the input array {7,11,3,19,13},
it will print the array after each pass through the outer loop.
plaintext
3, 11, 7, 19,
13, 3, 7, 11, 19,
13, 3, 7, 11, 19,
13, 3, 7, 11, 13,
19, 3, 7, 11, 13,
19, |
Explanation of the Output:
- Initial
Array: The state of the array before sorting starts.
- Sorting
Steps: After each complete pass through the array, the state of the
array is printed to show how the largest unsorted element "bubbles
up" to its correct position.
- Final
Sorted Array: After all passes, the array is fully sorted.
Key Points:
- Time
Complexity: The time complexity of bubble sort is O(n^2) in the worst
and average cases, where n is the number of elements in the array. This
happens because each element is compared with every other element.
- Space
Complexity: The space complexity is O(1), as the sorting is done
in-place without requiring additional memory.
- Stability:
Bubble sort is a stable sorting algorithm, meaning that the relative order
of equal elements is preserved.
- Inefficiency:
Bubble sort is not efficient for large datasets, and there are more
efficient algorithms like Quick Sort or Merge Sort for such cases.
Potential Enhancements:
- Optimization:
You can optimize the bubble sort by introducing a flag that checks if any
swaps were made during a pass. If no swaps are made, the array is already
sorted, and you can break out of the loop early.
- Generic
Implementation: Consider modifying the implementation to work with
generic types instead of just integers.
This implementation provides a clear understanding of how
bubble sort works by showing each step of the sorting process through printed
output in the console.
0 Comments