Skip to main content

Posts

Showing posts with the label Java

Simple Java Programs (Part 1)

 Q. Define a class to declare a character array of size ten, accept the character into the array and perform the following:  • Count the number of uppercase letters in the array and print.  • Count the number of vowels in the array and print. Q. Define a class to declare an array of size 20 of double datatype, accept the elements into the array and perform the following:  • Calculate and print the sum of all the elements.  • Calculate and print the highest value of the array. Q. Define a class to accept two strings, convert them into uppercase, check and display whether two strings are equal or not, if the two strings are not equal, print the string with the highest length or print the message both the strings are of equal length. Q. Define a class to accept a string, convert it into lowercase and check whether the string is a palindrome or not.  A palindrome is a word which reads the same backward as forward.  Example:  madam, racecar etc...

Binary Search in Java - Explained

Binary Search in Java - Explained You are looking for an explanation of how binary search works in Java ? If so, then search no more! What is Binary search? In Java programming language, binary search is an advanced searching algorithm used to search through a single or multiple arrays for a search element. How does the binary search algorithm works? In binary search, the array has to be sorted first. In this method, when the user searches for a value, the number of elements to be searched is reduced, because it locates the middle number and then either moves to the upper half or the lower half to continue the search. Program for Binary Search Suppose I have to search for a number and return its position (index) in the array. int arr[] = {5,10,15,20,25,30,35,40,45,50} For example if I search for say, 35 The program has been given below for binary search: Points to Note: Binary search only works with an array already sorted This is effective for very large arrays, howeve...