QDBordtoshred / WEni

MIT License
0 stars 0 forks source link

Learnings #10

Open QDBordtoshred opened 9 months ago

QDBordtoshred commented 9 months ago

My Learnings (Java)

Initially, my mindset was centered around mastering the skills that captivated my interest, particularly in areas like CSS and UX design. These domains held a unique allure for me, and I dedicated considerable time and effort to hone my expertise in them. However, a pivotal realization dawned upon me with the guidance of my teacher. I understood the importance of aligning my learning path with the broader curriculum of the class. Recognizing the significance of Java, a language that the rest of the class was delving into, I embraced the idea of broadening my skill set. With newfound determination, I have embarked on the journey to grasp the intricacies of Java, diligently following along with the class materials.

Below is a simple implementation of the Bubble Sort algorithm in Java, showcasing my understanding of object-oriented principles:

public class BubbleSort {
    public static void main(String[] args) {
        int[] arrayToSort = {64, 34, 25, 12, 22, 11, 90};

        // Creating an instance of the BubbleSort class
        BubbleSort bubbleSort = new BubbleSort();

        // Invoking the sort method to perform bubble sort
        bubbleSort.sort(arrayToSort);

        System.out.println("Sorted array:");
        bubbleSort.printArray(arrayToSort);
    }

    // The sort method, implementing the Bubble Sort algorithm
    void sort(int[] arr) {
        int n = arr.length;
        for (int i = 0; i < n-1; i++) {
            for (int j = 0; j < n-i-1; j++) {
                if (arr[j] > arr[j+1]) {
                    // Swap arr[j] and arr[j+1]
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
    }

    // A utility method to print the elements of an array
    void printArray(int[] arr) {
        int n = arr.length;
        for (int i=0; i<n; ++i)
            System.out.print(arr[i] + " ");
        System.out.println();
    }
}

Class Definition (BubbleSort):

The code begins with the definition of the BubbleSort class, which encapsulates the sorting algorithm.

Main Method:

This is the entry point of the program. Inside the main method, an array is created, and an instance of the BubbleSort class is instantiated.

Sort Method:

The heart of the Bubble Sort algorithm. This method takes an array as a parameter and sorts it in ascending order using the Bubble Sort technique. It uses nested loops to iterate through the array and compare adjacent elements, swapping them if they are in the wrong order.

PrintArray Method:

A utility method to print the elements of an array. It takes an array as a parameter and prints its elements.

Object Instantiation:

In the main method, an instance of the BubbleSort class is created using the line BubbleSort bubbleSort = new BubbleSort();. This follows the object-oriented concept of creating objects from classes.

Method Invocation:

The sort and printArray methods are invoked on the created instance (bubbleSort) to perform the sorting and display the sorted array.



Bubble Sort

Passes through the List:

Iteration:

Complete Passes:

Termination: