- Prepare
All Platforms Programming Aptitude Syllabus Interview Preparation Interview Exp. Off Campus - Prime Video
- Prime Mock

- Interview Experience
- Prime VideoNew
- Prime Mock
- Interview Prep
- Nano Degree
- Prime Video
- Prime Mock

# Algorithms

**What is an Algorithm?**

An algorithm is a set of limited or finite procedures that are used to solve a specific set of problem. It can be whether complex or of Simple type depending upon the problem given.

Here, in the page we will discuss more about the Algorithms that are used in java.

**Algorithms in Java:**

In Java, there are numerous amount of Algorithms and inbuilt function are available in the collection Framework, these Java algorithms are static methods that can be used to manipulate collections in many ways.

These static methods are also known as “generic algorithms” as they can be applied to a variety of collections in java.

**Types of Algorithms:**

several types of Algorithms are available in java collection. Some of the important algorithms are listed below:

**Brute Force Algorithms :**It is the simplest approach for a problem. A brute force algorithm is the first approach that comes to our mind when we see a problem.**Divide and conquer Algorithms :**This algorithm divides a problem into smaller parts, we have to resolves each smaller problem in turn, then combines the results to provide the overall answer.**Greedy Algorithms :**This kind of algorithm builds the solution one by one.

The immediate benefit of the following section serves as the foundation for the solution of that section.The answer for the following section will be the one offering the greatest benefit.**Searching Algorithms :**The algorithms used for searching individual components or groups of elements from a specific data structure are called searching algorithms. Depending on how they go about things or whatever data structure the element needs to be in, they can be of several forms.**Sorting Algorithms :**Sorting is the process of organizing a group of facts in a specific way in accordance with the needs. Sorting algorithms are the ones, that assist in carrying out this duty. A group of data such as an array or a list are often sorted using sorting algorithms in an increasing or decreasing order.**and many more Algorithms :**There are many more algorithms that are used in the collection.

**Example for Sorting Algorithm:**

**sort()** method of java collection is used for sorting the elements of a group of elements of an array or list.

import java.util.*; public class Main{ public static void main(String[] args){ int[] arr = {2,4,7,3,4,1,9}; System.out.print("Before Sorting : "); for(int i = 0;i<arr.length;i++){ System.out.print(arr[i] + " "); } Arrays.sort(arr); // using sort function of collection System.out.println(); System.out.print("After Sorting : "); for(int i = 0;i<arr.length;i++){ System.out.print(arr[i] + " "); } } }

Output: Before Sorting : 2 4 7 3 4 1 9 After Sorting : 1 2 3 4 4 7 9

**Example for Searching Algorithm:**

The **binarySearch()**` method of collection framework in java can be used for searching an element in an array or arraylist. The one and only condition for using binarysearch function is that the data should be sorted and we can do this using the sort fucntion of collection in java.`

import java.util.*; public class Main{ public static void main(String[] args){ // Sample Array int[] arr = {2,4,6,7,8,11,27}; // element that needs to be searched int n = 11; // using binarysearch method of java collection. int ans = Arrays.binarySearch(arr, n); System.out.println("Element is available at " + ans + " Position."); } }

Output: Element is available at 5 position.

### Prime Course Trailer

### Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

**Get over 200+ course One Subscription**

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Login/Signup to comment