Home > Java > javaTutorial > JAVA core data structure and algorithm implementation

JAVA core data structure and algorithm implementation

PHPz
Release: 2023-11-08 12:35:15
Original
1154 people have browsed it

JAVA core data structure and algorithm implementation

Due to the limited space of the article, I will provide implementation examples of some key data structures and algorithms. First, several core data structures and algorithms are introduced, and then corresponding Java code examples are given.

  1. Array

    • Implement a dynamically expanded array
    • Implement the addition, deletion, modification and query operations of the array
public class DynamicArray<T> {
    private Object[] array;
    private int size;
    private int capacity;

    public DynamicArray() {
        capacity = 10;
        array = new Object[capacity];
        size = 0;
    }

    public void add(T element) {
        if (size == capacity) {
            capacity *= 2;
            Object[] newArray = new Object[capacity];
            System.arraycopy(array, 0, newArray, 0, size);
            array = newArray;
        }
        array[size++] = element;
    }

    public T get(int index) {
        if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
        return (T) array[index];
    }

    public void remove(int index) {
        if (index < 0 || index >= size) throw new IndexOutOfBoundsException();
        for (int i = index; i < size - 1; i++) {
            array[i] = array[i + 1];
        }
        size--;
    }
}
Copy after login
  1. Linked list

    • Implementing a single linked list
    • Realizing the addition, deletion, modification and query operations of the linked list
public class ListNode {
    int val;
    ListNode next;

    ListNode(int val) {
        this.val = val;
    }
}

public class LinkedList {
    private ListNode head;

    public void addAtHead(int val) {
        ListNode newHead = new ListNode(val);
        newHead.next = head;
        head = newHead;
    }

    public void addAtTail(int val) {
        if (head == null) {
            head = new ListNode(val);
        } else {
            ListNode current = head;
            while (current.next != null) {
                current = current.next;
            }
            current.next = new ListNode(val);
        }
    }

    public void deleteAtIndex(int index) {
        if (index == 0) {
            head = head.next;
            return;
        }
        int count = 0;
        ListNode current = head;
        ListNode prev = null;
        while (current != null && count < index) {
            prev = current;
            current = current.next;
            count++;
        }
        if (current != null) {
            prev.next = current.next;
        }
    }

    public ListNode get(int index) {
        ListNode current = head;
        int count = 0;
        while (current != null && count < index) {
            current = current.next;
            count++;
        }
        return current;
    }
}
Copy after login
  1. Stack

    • Implement an array-based stack
    • Implement stack push and pop operations
public class ArrayStack {
    private int[] array;
    private int top;
    private int capacity;

    public ArrayStack(int capacity) {
        this.capacity = capacity;
        array = new int[capacity];
        top = -1;
    }

    public void push(int value) {
        if (top == capacity - 1) throw new IllegalStateException("Stack is full");
        array[++top] = value;
    }

    public int pop() {
        if (top == -1) throw new IllegalStateException("Stack is empty");
        return array[top--];
    }

    public int peek() {
        if (top == -1) throw new IllegalStateException("Stack is empty");
        return array[top];
    }

    public boolean isEmpty() {
        return top == -1;
    }
}
Copy after login
  1. Queue

    • Implementing an array-based queue
    • Realizing operations such as queue entry and exit
public class ArrayQueue {
    private int[] array;
    private int front;
    private int rear;
    private int capacity;

    public ArrayQueue(int capacity) {
        this.capacity = capacity;
        array = new int[capacity];
        front = 0;
        rear = -1;
    }

    public void enqueue(int value) {
        if (rear == capacity - 1) throw new IllegalStateException("Queue is full");
        array[++rear] = value;
    }

    public int dequeue() {
        if (isEmpty()) throw new IllegalStateException("Queue is empty");
        int value = array[front];
        front++;
        return value;
    }

    public int peek() {
        if (isEmpty()) throw new IllegalStateException("Queue is empty");
        return array[front];
    }

    public boolean isEmpty() {
        return front > rear;
    }
}
Copy after login
  1. Sort algorithm

    • Implement bubble sort, insertion sort, selection sort, quick sort and other algorithms
public class Sort {
    public static void bubbleSort(int[] array) {
        int length = array.length;
        for (int i = 0; i < length - 1; i++) {
            for (int j = 0; j < length - 1 - i; j++) {
                if (array[j] > array[j + 1]) {
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
    }

    public static void insertionSort(int[] array) {
        for (int i = 1; i < array.length; i++) {
            int current = array[i];
            int j = i - 1;
            while (j >= 0 && array[j] > current) {
                array[j + 1] = array[j];
                j--;
            }
            array[j + 1] = current;
        }
    }

    public static void selectionSort(int[] array) {
        for (int i = 0; i < array.length - 1; i++) {
            int minIndex = i;
            for (int j = i + 1; j < array.length; j++) {
                if (array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            int temp = array[i];
            array[i] = array[minIndex];
            array[minIndex] = temp;
        }
    }

    public static void quickSort(int[] array, int low, int high) {
        if (low < high) {
            int mid = partition(array, low, high);
            quickSort(array, low, mid - 1);
            quickSort(array, mid + 1, high);
        }
    }

    private static int partition(int[] array, int low, int high) {
        int pivot = array[high];
        int i = low - 1;
        for (int j = low; j < high; j++) {
            if (array[j] < pivot) {
                i++;
                int temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
        }
        int temp = array[i + 1];
        array[i + 1] = array[high];
        array[high] = temp;
        return i + 1;
    }
}
Copy after login

The above are Java implementation examples of some core data structures and algorithms. I hope they can be helpful to you.

The above is the detailed content of JAVA core data structure and algorithm implementation. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template