Header Ads Widget

Responsive Advertisement

Bubble Sort with each iteration how it is work

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;
/**
 *
 * @author MandalKC
 *
 */
public class BubbleSort {
 /**
  *
  * @param array
  */
 public static void bubbleshort(int array[]){
  for (int i = 0; i < array.length; i++) {
   for (int j = i+1; j < array.length; j++) {
    if(array[i]>array[j]){
     swapNumbers(i, j, array);
    }
  
   }
   printNumbers(array);
 
  }
 }
 /**
  *
  * @param i
  * @param j
  * @param array
  */
 private static void swapNumbers(int i, int j, int[] array) {
  int temp;
  temp = array[i];
  array[i] = array[j];
  array[j] = temp;
  }
 /**
  *
  * @param input
  */
 private static void printNumbers(int[] input) {
  for (int i = 0; i < input.length; i++) {
   System.out.print(input[i] + ", ");
   } System.out.println("\n");
  }
 /**
  *
  * @param args
  */
 public static void main(String[] args) {
  //int[] input = { 4, 2, 9, 6, 23, 12, 34, 0, 1 };
  int[] input = { 7,11,3,19,13};
  bubbleshort(input);
  }
}

 

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:

  1. Bubble Sort Algorithm:
    1. The algorithm repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order.
    2. The process is repeated for each element in the array until no more swaps are needed, which indicates that the array is sorted.
  2. bubbleshort Method:
    1. Parameters: Takes an integer array array as input.
    2. Logic:
      1. The outer loop runs through each element in the array.
      2. 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.
      3. After each pass through the array, the current state of the array is printed to the console.
  3. swapNumbers Method:
    1. A utility method to swap the values of two elements in the array.
  4. printNumbers Method:
    1. A utility method to print the current state of the array to the console.
  5. main Method:
    1. Creates an array input and calls the bubbleshort method to sort the array.
    2. 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:

  1. Initial Array: The state of the array before sorting starts.
  2. 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.
  3. Final Sorted Array: After all passes, the array is fully sorted.

Key Points:

  1. 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.
  2. Space Complexity: The space complexity is O(1), as the sorting is done in-place without requiring additional memory.
  3. Stability: Bubble sort is a stable sorting algorithm, meaning that the relative order of equal elements is preserved.
  4. 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:

  1. 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.
  2. 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.

 



Bubble Sort with each iteration
Bubble Sort with each iteration




 

Post a Comment

0 Comments