Pseudo Code Generator

Bubble Sort Algorithm Pseudo Code in Java

This pseudo code demonstrates the Bubble Sort algorithm, sorting an integer array in ascending order with clear steps for initialization, display, sorting logic, and output.


Empty image or helper icon

Prompt

public class BubbleSortSamplel{
public static void main(String[] args) {
 int nums[] = {18,15,9,12,6,3};
 int size = nums.length;
 int temp = 0;
System.out.println("Before sorting");
for(int num : nums){                        
System.out.println(num);     
     }
System.out.println();  
for (int k = 0; k < size; k++) {
for (int a = 0; a < size - k - 1; a++) {
if (nums[a] > nums[a + 1]) {
temp = nums[a];
nums[a] = nums[a + 1];
nums[a + 1] = temp; 
            }
          }
       }
System.out.println("After sorting");
for(int num : nums){                        
System.out.println(num); 
     }
  
  }


}

Answer

Pseudo Code for Bubble Sort Implementation

Overview

This pseudo code represents a Bubble Sort algorithm implemented in Java. The algorithm sorts an array of integers in ascending order and prints the array both before and after sorting.

Main Components

  1. Initialization: Define the array and its size.
  2. Display Before Sorting: Print the array before sorting.
  3. Bubble Sort Algorithm: Implement the sorting algorithm.
  4. Display After Sorting: Print the sorted array.

Pseudo Code

BEGIN BubbleSortSample

    // Step 1: Initialize array
    DECLARE nums AS ARRAY OF INTEGER
    nums = [18, 15, 9, 12, 6, 3]
    DECLARE size AS INTEGER
    size = LENGTH OF nums
    DECLARE temp AS INTEGER

    // Step 2: Print array before sorting
    PRINT "Before sorting"
    FOR EACH num IN nums DO
        PRINT num
    END FOR
    PRINT NEW LINE

    // Step 3: Bubble Sort Algorithm
    FOR k FROM 0 TO size - 1 DO
        FOR a FROM 0 TO size - k - 2 DO
            IF nums[a] > nums[a + 1] THEN
                // Swap the elements
                temp = nums[a]
                nums[a] = nums[a + 1]
                nums[a + 1] = temp
            END IF
        END FOR
    END FOR

    // Step 4: Print array after sorting
    PRINT "After sorting"
    FOR EACH num IN nums DO
        PRINT num
    END FOR

END BubbleSortSample

Key Points

  • Initialization: The array is defined and its size is calculated.
  • Printing: Before and after sorting, the elements of the array are displayed.
  • Nested Loop Structure: The outer loop controls the number of passes, while the inner loop performs the comparison and swapping of adjacent elements.
  • Swapping Logic: A temporary variable is used to facilitate the swapping of elements.

This pseudo code serves as a clear and structured representation of the Bubble Sort algorithm, making it easy to understand and implement in various programming environments.

Create your Thread using our flexible tools, share it with friends and colleagues.

Your current query will become the main foundation for the thread, which you can expand with other tools presented on our platform. We will help you choose tools so that your thread is structured and logically built.

Description

This pseudo code demonstrates the Bubble Sort algorithm, sorting an integer array in ascending order with clear steps for initialization, display, sorting logic, and output.