Java Semantics - Including a Quick Sort Implementation + Linked List Class!
Without using any of Java's built-in sorting functions, I was able to implement a Java sorting algorithm from scratch. The custom sort implementation uses a similar implementation to the Bubble Sort algorithm (common sorting algorithm).
Bubble Sort works by repeatedly swapping adjacent elements if they are in the wrong order, until the entire list is sorted. While this algorithm is not as efficient as Quicksort or Mergesort, Bubble Sort is a simple algorithm that demonstrated the core concepts of sorting for my Java implementation.
Generics in Java:
To make our sort implementation generic, I've used Java's generics feature. Our JavaSort class takes a generic type T that implements the Comparable interface. This allows our sort to work with any data type that can be compared, without needing to know the specific type at compile-time. This is one of the benefits of Generics in Java. Java does not offer templates like the C language does, however it does offer generics that can provide flexibility for programs to operate with different object types.
Instead of using Java's built-in LinkedList class, I've implemented my version of a generic JavaLinkedList data structure. The implementation uses a singly-linked list, with each node containing the generic data type and a reference to the next node.
To achieve polymorphism, I've defined a LinkedList interface that declares the common methods for linked list operations, such as push, append, pop, remove, find, and size (as according to our project requirements). Our JavaLinkedList class then implements this interface, allowing it to be used interchangeably with any other implementation of the LinkedList interface.
By using Java generics, my JavaLinkedList can work with any data type, as long as that type is specified when creating a new instance of the list. This provides type safety and eliminates the need for explicit casting in the Java language.