Found 7442 Articles for Java

Java program to implement binary search

Lakshmi Srinivas
Updated on 26-Jul-2024 21:36:20

2K+ Views

Binary search is a fast searching algorithm with run-time complexity of Ο(log n). This search algorithm works on the principle of divide and conquer. For this algorithm to work properly, the data collection should be in the sorted form. The binary search looks for a particular item by comparing the middle most item of the collection. If a match occurs, then the index of the item is returned. If the middle item is greater than the item, then the item is searched in the sub-array to the left of the middle item. Otherwise, the item is searched for in the sub-array to the ... Read More

Java program to implement binary search

Lakshmi Srinivas
Updated on 26-Jul-2024 21:36:20

2K+ Views

Binary search is a fast searching algorithm with run-time complexity of Ο(log n). This search algorithm works on the principle of divide and conquer. For this algorithm to work properly, the data collection should be in the sorted form. The binary search looks for a particular item by comparing the middle most item of the collection. If a match occurs, then the index of the item is returned. If the middle item is greater than the item, then the item is searched in the sub-array to the left of the middle item. Otherwise, the item is searched for in the sub-array to the ... Read More

Java program to implement insertion sort

karthikeya Boyini
Updated on 11-Sep-2024 12:23:46

2K+ Views

In this article, we will learn to implement insertion sort in Java. The insertion sort is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained which is always sorted. For example, the lower part of an array is maintained to be sorted. An element to be inserted in this sorted sub-list must find its appropriate place and then be inserted there. Hence the name, insertion sort. The array is searched sequentially and unsorted items are moved and inserted into the sorted sub-list (in the same array). Problem Statement For a given array write a program in Java to implement ... Read More

Java program to implement insertion sort

karthikeya Boyini
Updated on 11-Sep-2024 12:23:46

2K+ Views

In this article, we will learn to implement insertion sort in Java. The insertion sort is an in-place comparison-based sorting algorithm. Here, a sub-list is maintained which is always sorted. For example, the lower part of an array is maintained to be sorted. An element to be inserted in this sorted sub-list must find its appropriate place and then be inserted there. Hence the name, insertion sort. The array is searched sequentially and unsorted items are moved and inserted into the sorted sub-list (in the same array). Problem Statement For a given array write a program in Java to implement ... Read More

Java program to implement selection sort

Arjun Thakur
Updated on 13-Mar-2020 05:44:07

4K+ Views

Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list.The smallest element is selected from the unsorted array and swapped with the leftmost element, and that element becomes a part of the sorted array. This process continues moving unsorted array boundary from one element to the right.Algorithm1.Set MIN to location 0 2.Search the minimum element in the ... Read More

Java program to implement selection sort

Arjun Thakur
Updated on 13-Mar-2020 05:44:07

4K+ Views

Selection sort is a simple sorting algorithm. This sorting algorithm is an in-place comparison-based algorithm in which the list is divided into two parts, the sorted part at the left end and the unsorted part at the right end. Initially, the sorted part is empty and the unsorted part is the entire list.The smallest element is selected from the unsorted array and swapped with the leftmost element, and that element becomes a part of the sorted array. This process continues moving unsorted array boundary from one element to the right.Algorithm1.Set MIN to location 0 2.Search the minimum element in the ... Read More

Java program to print the factorial of the given number

Shriansh Kumar
Updated on 13-Sep-2024 15:55:01

5K+ Views

Given a number of type integer, write a Java program to print its factorial. The factorial of a positive integer n is the product of all values from n to 1. For example, the factorial of 3 is (3 * 2 * 1 = 6). Let's understand the problem statement with examples − Example Scenario 1: Input: int n = 4; Output: res = 24 Calculation: 4! = 4 * 3 * 2 * 1 = 24 Example Scenario 2: Input: int n = 0; Output: res = 1 Factorial of 0 is always 1. Finding Factorial ... Read More

Java program to print the factorial of the given number

Shriansh Kumar
Updated on 13-Sep-2024 15:55:01

5K+ Views

Given a number of type integer, write a Java program to print its factorial. The factorial of a positive integer n is the product of all values from n to 1. For example, the factorial of 3 is (3 * 2 * 1 = 6). Let's understand the problem statement with examples − Example Scenario 1: Input: int n = 4; Output: res = 24 Calculation: 4! = 4 * 3 * 2 * 1 = 24 Example Scenario 2: Input: int n = 0; Output: res = 1 Factorial of 0 is always 1. Finding Factorial ... Read More

Java program to print a Fibonacci series

Samual Sam
Updated on 18-Nov-2024 22:31:46

3K+ Views

The Fibonacci Series generates subsequent numbers by adding two previous numbers. The Fibonacci series starts from two numbers − F0 & F1. The initial values of F0 & F1 can be taken as 0, 1, or 1, 1 respectively. Fn = Fn-1 + Fn-2 Problem Statement Write a program in Java program to print a Fibonacci series. Input Run the program Output 1 1 2 3 5 8 13 21 34 55 The above output is obtained when the values of n=10. Different approaches to print a Fibonacci series Following are the different approaches to printing a Fibonacci series − ... Read More

compareTo() definition mistake?

Pythonista
Updated on 30-Jul-2019 22:30:22

183 Views

The example works correctly. The compareTo() method is called by a string object and takes another string object as argument. The return value is integer and is difference in Unicode values of characters of respective strings when they are not equal. The value can be -ve, 0 or +ve.

Advertisements