Algorithm codes in java

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

All Algorithms implemented in Java

License

TheAlgorithms/Java

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Co-authored-by: Ranjeet Kumar Jena Co-authored-by: Andrii Siriak

Git stats

Files

Failed to load latest commit information.

Читайте также:  Masonry grid layout css

README.md

You can run and edit the algorithms, or contribute to them using Gitpod.io (a free online development environment) with a single click.

All algorithms are implemented in Java (for educational purposes)

These implementations are intended for learning purposes. As such, they may be less efficient than the Java standard library.

Please read our Contribution Guidelines before you contribute to this project.

Our directory has the full list of applications.

Источник

Java Algorithms

The Java collections framework provides various algorithms that can be used to manipulate elements stored in data structures.

Algorithms in Java are static methods that can be used to perform various operations on collections.

Since algorithms can be used on various collections, these are also known as generic algorithms.

Let’s see the implementation of different methods available in the collections framework.

1. Sorting Using sort()

The sort() method provided by the collections framework is used to sort elements. For example,

import java.util.ArrayList; import java.util.Collections; class Main < public static void main(String[] args) < // Creating an array list ArrayListnumbers = 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); > > 
Unsorted ArrayList: [4, 2, 3] Sorted ArrayList: [2, 3, 4] 

Here the sorting occurs in natural order (ascending order). However, we can customize the sorting order of the sort() method using the Comparator interface.

2. Shuffling Using shuffle()

The shuffle() method of the Java collections framework is used to destroy any kind of order present in the data structure. It does just the opposite of the sorting. For example,

import java.util.ArrayList; import java.util.Collections; class Main < public static void main(String[] args) < // Creating an array list ArrayListnumbers = 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); > > 
Sorted ArrayList: [1, 2, 3] ArrayList using shuffle: [2, 1, 3] 

When we run the program, the shuffle() method will return a random output.

Читайте также:  Php get auth header

The shuffling algorithm is mainly used in games where we want random output.

3. Routine Data Manipulation

In Java, the collections framework provides different methods that can be used to manipulate data.

  • 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
import java.util.Collections; import java.util.ArrayList; class Main < public static void main(String[] args) < // Creating an ArrayList ArrayListnumbers = 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 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); > > 
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] 

Note: While performing the copy() method both the lists should be of the same size.

4. Searching Using binarySearch()

The binarySearch() method of the Java collections framework searches for the specified element. It returns the position of the element in the specified collections. For example,

import java.util.Collections; import java.util.ArrayList; class Main < public static void main(String[] args) < // Creating an ArrayList ArrayListnumbers = 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); > > 

Note: The collection should be sorted before performing the binarySearch() method.

Читайте также:  Python работа с pdf файлами

5. Composition

  • 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
import java.util.Collections; import java.util.ArrayList; class Main < public static void main(String[] args) < // Creating an ArrayList ArrayListnumbers = 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 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); > > 
ArrayList1: [1, 2, 3, 2] Count of 2: 2 ArrayList2: [5, 6] Two lists are disjoint: true 

6. Finding Extreme Values

The min() and max() methods of the Java collections framework are used to find the minimum and the maximum elements, respectively. For example,

import java.util.Collections; import java.util.ArrayList; class Main < public static void main(String[] args) < // Creating an ArrayList ArrayListnumbers = 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); > > 
Minimum Element: 1 Maximum Element: 3 

Table of Contents

Источник

Оцените статью