Các thuật toán trong Java

Trong hướng dẫn này, chúng ta sẽ tìm hiểu về các thuật toán khác nhau được cung cấp bởi khung công tác bộ sưu tập Java với sự trợ giúp của các ví dụ.

Khung tập hợp Java cung cấp các thuật toán khác nhau có thể được sử dụng để thao tác các phần tử được lưu trữ trong cấu trúc dữ liệu.

Các thuật toán trong Java là các phương thức tĩnh có thể được sử dụng để thực hiện các hoạt động khác nhau trên các tập hợp.

Vì các thuật toán có thể được sử dụng trên các bộ sưu tập khác nhau, chúng còn được gọi là thuật toán chung .

Hãy xem việc triển khai các phương thức khác nhau có sẵn trong khung tập hợp.

1. Sắp xếp Sử dụng sort()

Các sort()phương pháp được cung cấp bởi khuôn khổ bộ sưu tập được sử dụng để các yếu tố phân loại. Ví dụ,

import java.util.ArrayList;
import java.util.Collections;

class Main {
    public static void main(String[] args) {

        // Creating an array list
        ArrayList<Integer> numbers = new ArrayList<>();

        // Add elements
        numbers.add(4);
        numbers.add(2);
        numbers.add(3);
        System.out.println("Unsorted ArrayList: " + numbers);

        // Using the sort() method
        Collections.sort(numbers);
        System.out.println("Sorted ArrayList: " + numbers);

    }
}

Đầu ra

Unsorted ArrayList: [4, 2, 3]
Sorted ArrayList: [2, 3, 4]

Ở đây việc sắp xếp diễn ra theo thứ tự tự nhiên (thứ tự tăng dần). Tuy nhiên, chúng ta có thể tùy chỉnh thứ tự sắp xếp của sort()phương pháp bằng giao diện Bộ so sánh .

Để tìm hiểu thêm, hãy truy cập Phân loại Java .

2. Xáo trộn bằng cách sử dụng shuffle()

Các shuffle()phương pháp khuôn khổ bộ sưu tập Java được sử dụng để tiêu diệt bất kỳ loại trật tự hiện diện trong cấu trúc dữ liệu. Nó hoàn toàn ngược lại với việc sắp xếp. Ví dụ,

import java.util.ArrayList;
import java.util.Collections;

class Main {
    public static void main(String[] args) {

        // Creating an array list
        ArrayList<Integer> numbers = new ArrayList<>();

        // Add elements
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        System.out.println("Sorted ArrayList: " + numbers);

        // Using the shuffle() method
        Collections.shuffle(numbers);
        System.out.println("ArrayList using shuffle: " + numbers);

    }
}

Đầu ra

Sorted ArrayList: [1, 2, 3]
ArrayList using shuffle: [2, 1, 3]

Khi chúng ta chạy chương trình, shuffle()phương thức sẽ trả về một kết quả ngẫu nhiên.

Thuật toán xáo trộn chủ yếu được sử dụng trong các trò chơi mà chúng ta muốn đầu ra ngẫu nhiên.

3. Thao tác dữ liệu thường xuyên

Trong Java, khung công tác tập hợp cung cấp các phương thức khác nhau có thể được sử dụng để thao tác dữ liệu.

  • reverse() – reverses the order of elements
  • fill() – replace every element in a collection with the specified value
  • copy() – creates a copy of elements from the specified source to destination
  • swap() – swaps the position of two elements in a collection
  • addAll() – adds all the elements of a collection to other collection

Ví dụ,

import java.util.Collections;
import java.util.ArrayList;

class Main {
    public static void main(String[] args) {
        // Creating an ArrayList
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(2);
        System.out.println("ArrayList1: " + numbers);

        // Using reverse()
        Collections.reverse(numbers);
        System.out.println("Reversed ArrayList1: " + numbers);

        // Using swap()
        Collections.swap(numbers, 0, 1);
        System.out.println("ArrayList1 using swap(): " + numbers);

        ArrayList<Integer> newNumbers = new ArrayList<>();

        // Using addAll
        newNumbers.addAll(numbers);
        System.out.println("ArrayList2 using addAll(): " + newNumbers);

        // Using fill()
        Collections.fill(numbers, 0);
        System.out.println("ArrayList1 using fill(): " + numbers);

        // Using copy()
        Collections.copy(newNumbers, numbers);
        System.out.println("ArrayList2 using copy(): " + newNumbers);
    }
}

Đầu ra

ArrayList1: [1, 2]
Reversed ArrayList1: [2, 1]
ArrayList1 Using swap(): [1, 2]
ArrayList2 using addALl(): [1, 2]
ArrayList1 using fill(): [0, 0]
ArrayList2 using copy(): [0, 0]

Lưu ý : Trong khi thực hiện copy()phương pháp, cả hai danh sách phải có cùng kích thước.

4. Tìm kiếm bằng binarySearch()

Các binarySearch()phương pháp của Java tìm kiếm khuôn khổ bộ sưu tập cho các phần tử cụ thể. Nó trả về vị trí của phần tử trong các bộ sưu tập được chỉ định. Ví dụ,

import java.util.Collections;
import java.util.ArrayList;

class Main {
    public static void main(String[] args) {
        // Creating an ArrayList
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);

        // Using binarySearch()
        int pos = Collections.binarySearch(numbers, 3);
        System.out.println("The position of 3 is " + pos);
    }
}

Đầu ra

The position of 3 is 2.

Lưu ý : Bộ sưu tập nên được sắp xếp trước khi thực hiện binarySearch()phương pháp.

Để biết thêm, hãy truy cập Tìm kiếm nhị phân Java .

5. Thành phần

  • frequency() – returns the count of the number of times an element is present in the collection
  • disjoint() – checks if two collections contain some common element

Ví dụ,

import java.util.Collections;
import java.util.ArrayList;

class Main {
    public static void main(String[] args) {
        // Creating an ArrayList
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        numbers.add(2);
        System.out.println("ArrayList1: " + numbers);

        int count = Collections.frequency(numbers, 2);
        System.out.println("Count of 2: " + count);

        ArrayList<Integer> newNumbers = new ArrayList<>();
        newNumbers.add(5);
        newNumbers.add(6);
        System.out.println("ArrayList2: " + newNumbers);

        boolean value = Collections.disjoint(numbers, newNumbers);
        System.out.println("Two lists are disjoint: " + value);
    }
}

Đầu ra

ArrayList1: [1, 2, 3, 2]
Count of 2: 2
ArrayList2: [5, 6]
Two lists are disjoint: true

6. Tìm giá trị cực đoan

Các phương thức min()và max()phương thức của khung công tác tập hợp Java được sử dụng để tìm phần tử tối thiểu và tối đa tương ứng. Ví dụ,

import java.util.Collections;
import java.util.ArrayList;

class Main {
    public static void main(String[] args) {
        // Creating an ArrayList
        ArrayList<Integer> numbers = new ArrayList<>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);

        // Using min()
        int min = Collections.min(numbers);
        System.out.println("Minimum Element: " + min);

        // Using max()
        int max = Collections.max(numbers);
        System.out.println("Maximum Element: " + max);
    }
}

Đầu ra

Minimum Element: 1
Maximum Element: 3








Gõ tìm kiếm nhanh...