Hello guys, In the last article, we have seen the iterative implementation of binary search in Java, and in this article, you will learn how to implement binary search using recursion. Recursion is an important topic for coding interviews but many programmers struggle to code recursive solutions. I will try to give you some tips to come up with recursive algorithms in this tutorial. In order to implement a recursive solution, you need to break the problem into sub-problems until you reach a base case where you know how to solve the problem like sorting an array with one element. Without a base case, your program will never terminate and it will eventually die by throwing the StackOverFlowError.
Learn Java and Programming through articles, code examples, and tutorials for developers of all levels.
How to add or view SSL certificate in Java keyStore or trustStore? keytool command examples
The keytool command in Java is a tool for managing certificates into keyStore and trustStore which is used to store certificates and requires during the SSL handshake process. By using the keytool command you can do many things but some of the most common operations are viewing certificates stored in the keystore, importing new certificates into the keyStore, delete any certificate from the keystore, etc. For those who are not familiar keyStore, trustStore, and SSL Setup for Java application Here is a brief overview of What is a trustStore and keyStore in Java.
10 Examples of Comparator, Comparable, and Sorting in Java 8
Hello guys, the Comparator class is used to provide code or logic for comparing objects in Java, while sorting a list of objects or a collection of objects. It's close cousin of Comparable which provides natural order sorting e.g. ascending and descending orders for numbers like int, short, long or float, and lexicographic order for String i.e. the order on which words are arranged in dictionaries. The Comparators are used while sorting arrays, lists and collections. You pass logic to compare objects and sorting methods like Collections.sort() use that logic to compare elements until they are arranged in sorted order.
How to Find Duplicate Characters in String [Java Coding Problems]
Hello guys, today's programming exercise is to write a program to find repeated characters in a String. For example, if given input to your program is "Java", it should print all duplicates characters, i.e. characters appear more than once in String and their count like a = 2 because of character 'a' has appeared twice in String "Java". This is also a very popular coding question on the various level of Java interviews and written tests, where you need to write code. On the difficulty level, this question is at par with the prime numbers or Fibonacci series, which are also very popular on junior level Java programming interviews and it's expected from every programmer to know how to solve them.
10 Programming questions and exercises for Java Programmers
If you have just started learning the basics of Java programming language or are familiar
with programming in either C or C++, then these Java programming questions and exercises are for
you. It doesn't focus on a particular part of Java, but these coding exercises
will switch you into programming mode. These are also great ways to master basic
programming construct like if-else, loops like for and
while break and continue with loop, Java operators e.g., arithmetic
and logical operator, recursion, methods, or functions, and standard Java API. You may also
find these Java programming questions in most Java courses taught in schools,
colleges, and various Java training courses.
How to implement Linear Search Algorithm in Java? Example tutorial
In the last article about searching and sorting, you have learned the binary search algorithm and today I'll teach you another fundamental searching algorithm called Linear search. Linear search is nothing but iterating over the array and comparing each element with the target element to see if they are equal since we search the array sequential from start to end, this is also known as sequential search or linear search. It is very slow as compared to binary search because you have to compare each element with every other element and is definitely not suitable for a large array. It's practically useful only in the case of a small array of up to 10 to 15 numbers. In the worst case, you need to check all elements to confirm if the target element exists in an array or not.
How to implement Radix Sort in Java - Algorithm Example [Solved]
The Radix sort, like counting sort and bucket sort, is an integer-based algorithm (I mean the values of the input array are assumed to be integers). Hence radix sort is among the fastest sorting algorithms around, in theory. It is also one of the few O(n) or linear time sorting algorithms along with the Bucket and Counting sort. The particular distinction for radix sort is that it creates a bucket for each cipher (i.e. digit); as such, similar to bucket sort, each bucket in radix sort must be a growable list that may admit different keys.
How to implement Merge Sort Algorithm in Java [Solved] - Example Tutorial
The merge sort algorithm is a divide and conquers algorithm. In the divide and conquer paradigm, a problem is broken into smaller problems where each small problem still retains all the properties of the larger problem -- except its size. To solve the original problem, each piece is solved individually; then the pieces are merged back together. For example, imagine you have to sort an array of 200 elements using the bubble sort algorithm. Since selection sort takes O(n^2) time, it would take about 40,000-time units to sort the array. Now imagine splitting the array into ten equal pieces and sorting each piece individually still using selection sort. Now it would take 400-time units to sort each piece; for a grand total of 10*400 = 4000.
Bubble sort in Java - Program to sort an Integer Array [Example]
Bubble sort is one of the classic sorting algorithms,s which is used to explain
sorting during various computer and engineering courses. Because of its
algorithmic nature and simplicity, it's often used in various Java
and C++ programming exercises. You may expect questions like the Write Java program to sort integer arrays
using bubble sort during any programming interview. Since algorithmic
questions are always tricky
questions and not easy to code. Even the simplest of them can lead to confusion,
especially if you are not gifted with a natural programming head. I have seen
many developers fumble if asked to code on the spot. That's why it's advisable to
do algorithmic and logical programming during training and learning programming
and OOPS to get this skill of converting logic into code.
21 Frequently Asked Java Interview Questions Answers for 2 to 3 Years Experienced
If you have been to a couple of Java interviews then you know that there are some questions that keep repeating like the difference between == and equals() method and many of its popular cousins like HashMap vs Hashtable, ArrayList vs LinkedList, the difference between equals() and hashCode(), or difference between Comparator and Comparable in Java. I call them frequently asked Java interview questions, and I suggest every Java developer make a list of them for their own reference and revision. I am sure many Java programmer already has such a list of questions handy if you don't have then this is a good time to find and make your own list, or if you are in rush then you can also buy my book Grokking the Java Interview, which contains many such questions.
Counting Sort in Java - Example
The Counting sort algorithm, like Radix sort and Bucket sort, is an integer-based algorithm (i.e. the values of the input array are assumed to be integers), non-comparison, and linear sorting algorithm. Hence counting sort is among the fastest sorting algorithms around, in theory. It is also one of the few linear sorting algorithms or O(n) sorting algorithms. It's quite common in Java interviews nowadays to ask, whether do you know any O(n) sorting algorithm or not. If you face this question in the future, you can mention Radix sort, Bucket sort, or Counting sort algorithms.
What is Constructor in Java and How it works? [with Example]
In simple word, Constructor is a method like a block of code which is called by Java runtime during object creation using new() operator. Constructor are special in the sense that they have the same name as the Class they are part of. They are also special in a sense that they are called by JVM automatically when you create an object. Have you ever thought about Why do you need a constructor? What benefits it provide? One reason is to initialize your object with default or initial state since default values for primitives may not be what you are looking for. One more reason you create constructor is to inform the world about dependencies, a class needs to do its job. Anyone by looking at your constructors should be able to figure out, what he needs in order to use this class. For example, following class OrderProcessor needs a Queue and Database to function properly.
How to Read, Write XLSX File in Java - Apache POI Example
No matter how Microsoft is doing in comparison with Google, Microsoft Office is still the most used application in software world. Other alternatives like OpenOffice and LiberOffice have failed to take off to challenge MS Office. What this mean to a Java application developer? Because of huge popularity of MS office products you often need to support Microsoft office format such as word, Excel, PowerPoint and additionally Adobe PDF. If you are using JSP Servlet, display tag library automatically provides Excel, Word and PDF support. Since JDK doesn't provide direct API to read and write Microsoft Excel and Word document, you have to rely on third party library to do your job. Fortunately there are couple of open source library exists to read and write Microsoft Office XLS and XLSX file format, Apache POI is the best one. It is widely used, has strong community support and it is feature rich.
HelloWorld Program in Java with Example
First of all, welcome to the exciting world of Java programming. If you are ready to write your first Java program i.e. HelloWorld in Java, it means you already crossed major hurdles to start Java programming, in terms of installing JDK and setting PATH for Java. If you haven't done so then you can follow those tutorials to install JDK in Windows 7 and 8 and setting PATH for Java. Before we start writing HelloWorld in Java, few notes about editors. Many Java beginners insist or try to use Eclipse or Netbeans IDE from the very start of the first program, which is not a good idea.
Parallel Array Sorting in Java - Arrays.parallelSort() Example
There are multiple ways to sort array in Java. For example, you can use Array.sort() method to sort any primitive or object array in Java but Java 8 presents a new feature that is useful to sort array’s elements. It can implemented by using the package “java.util.Arrays” which represents several methods to sort the array. The biggest benefit is that parallel array sorting is that its faster than any other sorting method due to usage of multithreading conception. So multiple threads can break the array into parts and work simultaneously to sort it.
How to Check if Given Number is Prime in Java - With Example
Hello guys, today, we are going to discuss one of the most common programming exercises for beginners is, write a program to check if a given number is prime or not? There are many ways to check if a number is prime or not, but the most common of them is the trial division, which is what we will see in this tutorial. In my opinion, these kinds of programs are their first steps towards algorithmic understanding. You first come up with a solution, which is driven by the fact that prime numbers are natural numbers, that are not divisible by any positive number other than 1 and themselves. Then, you write a for loop to check every number, starting from 1 to a given number, to see if the given number is divisible by any positive number or not. This leads you to the solution.
How to find symbolic link or soft link in Linux? ls + find command Example Tutorial
There are two ways you can find a symbolic link or soft link in UNIX based operating systems like Linux, Solaris, BSD, or IBM AIX. The first way is by using the ls command in UNIX which displays files, directories, and links in any directory and the other way is by using UNIX find command which has the ability to search any kind of file e.g. file, directory, or link. In this UNIX command tutorial, we will see examples of both of these UNIX commands for finding a soft link in any directory. If you are new to UNIX operating system and not familiar with the concept of the soft link and hard link, I would recommend getting a good hand on it, as it is one of the most powerful features of UNIX based system.
Subscribe to:
Posts (Atom)