Jul 31, 2020

Linear Search - Binary Search - Searching Algorithms - Coding Winds

LINEAR SEARCH AND BINARY SEARCH ALGORITHM

Hey guys, we are back with another blog and today we are going talk about linear and binary searching in java. So primarily let us see what is searching, searching basically refers to finding out a specific element from an array. Linear search is a searching algorithm that runs to find out the element from the array by checking each and every array element one by one. Whereas, binary search divides the array into two halves and then searches for the element. So let's look further at the differences and the algorithm of the two.


LINEAR SEARCH


In linear search the element to be found out is searched for, throughout the array and the as soon as the element is found the loop breaks. So the time complexity of this algorithm is O(n2). Now let's see how to implement this in code.

 

public class LinearSearch {

     static String linearSearch(int ar[],int num) {

        

         result="";

         for(int i=0; i<ar.length; i++) {

     if(num==ar[i]) {

     result= "Number found at index "+i;

     break;

    

     else

     result= "Number not found";

    

     return result;

    

 

     public static void main(String[] args) {

        

         int ar[] = {12, 45, 9,23, 6,11};

         int min=ar[0];

        int num = 23;

       out.println(ar,num));

}

}

 

BINARY SEARCH


Binary search is possible in a sorted array, since in this the array is divided into two halves. And then the element to be searched (or key value),

Let's us see this in code

     import java.util.*;

 

public class BinarySearch {

 

     public static void binarySearch(int arr[], int first, int last, int key){

         int mid = (first + last)/2;

         while( first <= last ){

         if ( arr[mid] < key ){

         first = mid + 1;

         else if ( arr[mid] == key ){

         out.println("Element is found at index: " + mid);

         break;

         else{

         last = mid - 1;

        

         mid = (first + last)/2;

        

         if ( first > last ){

         out.println("Element is not found!");

        

        

         public static void main(String args[]){

              int arr[] = {10,20,30,40,50};

         int num = 40;

         int last=arr.length-1;

         arr,0,last,num);

        

        

          

 

OUTPUT: Element is found at index: 3

 

Note that, for binary search an array needs to be sorted, else we cannot divide the array into two halves and hence this algo cannot be executed.


Jul 30, 2020

Functions in C++ | C++ Language | Coding Winds

Functions in C++

Hello People, today we are going to study about the main part of our code, We are going to read about Functions in this article. We can use functions when we want our code to perform particular tasks again and again, we can reduce the task of typing a part of code again and again, by making a function of that particular task and calling the function when required.

So, by now you must have got an idea that function is a part of code which executes only when we call them. We can also pass data in a function, known as parameters.

Creating a FUNCTION

To create a function, specifying a function name followed by parentheses’()’. A function declaration also must have a return type and parameter in it.

Return keyword is specified before the name of the function and parameters are specified after the name of function, inside the parentheses,  separated by commas (if there are more than one.)

Parameter is some information which might require for the execution of the function, this parameters are of some data-type. We will study more about this parameter further.

There can be more than one parameter in a single function.

For Example:

Calling a FUNCTION

Declared function does not execute immediately, they are saved for later use, to execute that particular function(when required) we need to call the function in our main code. For executing a particular function we need to call it by the function’s name followed by parentheses

Let us look at the below given example for the better understanding :


We can call a function multiple times with different parameter value everytime :


In C++ it is important to declare a user defined function[ sum() ]

However, it is possible if we the declaration the function before the “ main() “ function and give the definition of the code after the  “ main() “ function, this will increase the readability of the code. For Example :


C++ Function Parameters

Information can be passed to a function in the form of paramters. This parameter is passed to a function in the form of variable. Parameters are specified after the name of function inside the parentheses, separated by commas (if there are more than one).

SYNTAX :

void FunctionName ( parameter1 , parameter2 , parameter3)

{

            ---Body of function---

}

 DEFAULT PARAMETERS

We can set a default for a parameter value in a function. This can be done by using equals sign ( = ).

If we uses a function without an argument, it uses the default value for it.

Look at the below given example for a better understanding :


Return Values

At the time of declaration we specify the return type. If we want a function to return a value we can use the data-type ( e.g., int, string,etc.) of which type we want function to return value. Also, if we don’t want any value in return we can use “void” keyword.

This is all about the basics of functions in C++ hope you gained some knowledge after reading this.

Thanks for giving it a read.

Keep Coding!