Linear search algorithm for CSE students





Linear search algorithm
Here DATA is an array with N elements and ITEM is to search from the array using linear search algorithm.
1. Repeat for J=0 to N-1
       if (ITEM==DATA[ J ]) then
          Set K=J+1;
          Print item found at position K
          Set FLAG=1;
       end of if
   end of for-loop
2. if (FLAG==0) then
      print item not found
Example 1.1
Let consider an array DATA[ ] where 3, 33, 1, 7 & 3 is stored, so N will be 5 in this case. If ITEM is 3, then find out 3 from the array using linear search algorithm.
Iteration
J=0, N=5, ITEM=3, FLAG=0, K=0
Step 01
for(j=0; j<N; j++)
as j=0 & n=5, so the condition j<n is true
   if (ITEM==DATA[ J ])
   as item=3 and data[0]=3, so the condition item==data[j] is true
      k=0+1=1
      print ‘item is found at position 1’
      flag=1
if(flag==0)
as flag=1 now, so the condition is false
Step 02
j=1, n=3, k=1, flag=1
for(j=0; j<N; j++)
as j=1 & n=5, so the condition j<n is true
   if (ITEM==DATA[ J ])
   as item=3 and data[1]=33, so the condition item==data[j] is false
if(flag==0)
as flag=1 now, so the condition is false
Step 03
j=2, n=3, k=1, flag=1
for(j=0; j<N; j++)
as j=2 & n=5, so the condition j<n is true
   if (ITEM==DATA[ J ])
   as item=3 and data[2]=1, so the condition item==data[j] is false
if(flag==0)
as flag=1 now, so the condition is false
Step 04
j=3, n=3, k=1, flag=1
for(j=0; j<N; j++)
as j=3 & n=5, so the condition j<n is true
   if (ITEM==DATA[ J ])
   as item=3 and data[3]=7, so the condition item==data[j] is false
if(flag==0)
as flag=1 now, so the condition is false

Step 05
j=4, n=3, k=1, flag=1
for(j=0; j<N; j++)
as j=4 & n=5, so the condition j<n is true
   if (ITEM==DATA[ J ])
   as item=3 and data[4]=3, so the condition item==data[j] is true

      k=4+1=5
      print ‘item is found at position 5’
      flag=1
if(flag==0)
as flag=1 now, so the condition is false
Step 06
j=5, n=3, k=1, flag=1
for(j=0; j<N; j++)
as j=5 & n=5, so the condition j<n is false
   exit for loop
if(flag==0)
as flag=1 now, so the condition is false
Recommended posts…

Advertisement



C program to input string and count the characters


The C program given below is able to get input string type data from user. For example: you can input a word or a line. After running the program, first of all a message will appear asking to provide string type data.

If you input something like I am a good boy. then that line will be stored into an array by the gets() function. And soon the length will be counted by the strlen() function and the number of characters will be stored into an integer type variable length.

Remember the program will count the number of characters including spaces, punctuation, special symbols and all others. We have to count the number of characters because later we will print that line or word using a for-loop.

But, we can also print that line using the puts() function simply. Then we have to replace 25th to 28th number line by a single line puts(msg);

/*----------------------------
|C program to input a sentence|
-----------------------------*/
#include <stdio.h>
#include <string.h>
int main(){
//declaring an integer variable
int i;

//declaring an character type arraye
char msg[100];

//message to enter the sentencei
printf("Enter a sentence: ");

//getting data into msg array
gets(msg);

//defining the number of character
int length = strlen(msg);

//Displaying the number of character
printf("There are %d characters!!n",length);

//for-loop to display the data
for(i=0; i<=length; i++){
printf("%c",msg[i]);
}
}


Advertisement


C program to shutdown a windows pc


The c program given below is able to shutdown a windows pc. When you will run the program, then you will be asked to provide your choice. Actually a message Shutdown PC? (Y/N): will appear on the command prompt box.

That means you have press Y or N. If you press y (both in small and capital letter will do the same), then the computer system will shut down soon. But if you press n, then another message will be displayed. Similarly if you press anything else except y/n then another message will be displayed.
//This C program is able to shutdown a Windows PC
//But you have to decide by pressing y/Y
#include <stdio.h>
#include <stdlib.h>

int main(){
//Declaring variable
char decision;

//Displaying message
printf("Shutdown PC? (Y/N): ");

//Getting decision
scanf("%c", &decision);

//Taking action
if(decision == 'y' || decision == 'Y'){
system("C:\WINDOWS\System32\shutdown /s");
} else if(decision == 'n' || decision == 'N'){
printf("Okay, close the window!nn");
} else {
printf("You haven't pressed Y/Nnn");
}

return 0;
}

Let’s see how the above program works!

1. The first two lines of the program is just comments. Similarly all other lines started with double slash (//) is considered as comments.
2. In the third and fourth line there I have included two header files names stdio.h and stdlib.h
Learn what is header file in c programming.
3. In the sixth line there has started the main function of the c program and ended at the line number 26.
4. In the eighth line there I have declared a character type variable named decision, where the decision flag character will be stored after providing by a user. And depending on this flag character, the rest of the codes will be executed.

5. By the 10th line the program will simply display a message to input the choice of the user. And by the 14th line the input will be stored into the variable decision.

6. In the 17th line there is an if condition that will first check if there is y or Y into the variable. If there found y into the variable, then the 18th line will be executed and the computer will shut down soon. Similarly by the 19th line it will checked if there is n or N into the decision variable. If found then the message from the 20th line will be displayed.

Otherwise, the message from the 22nd line will be displayed.


Advertisement


4. Hello world apps in laravel




In the previous three posts we have installed both composer and laravel, and configured the necessary settings so that we can work with laravel. Now in this tutorial we will create our first laravel application. Obviously it will very simple and tiny, but another step to enter the world of laravel. The application we will create will output some texts only as I have said before. Let’s start to work.
 Step 01  Create a new directory inside htdocs
Go to the xampp installation folder, then open the htdocs folder and create a new folder inside it. For example: laraApps.

 Step 02  Change the directory to xampp installation drive
Open the run command prompt and type the drive letter as showed in the image below. For example: if you install xampp in D drive then you have to type d: and need to press the Enter Key.
So, now you are in D drive.

 Step 03  Change the directory to the newly created apps folder
Now we need to change the directory to the folder we have already created inside the htdocs folder. So, we need to type the command cd D:xampphtdocsLaravelApps and also need to press the Enter Key.
 Step 04  Creating new application
Now type the command line laravel new myBlog and hit the Enter Key to create a new application named myBlog. You can try another name as you like.

After pressing the Enter Key, the system will do the listed things below…
1. Will craft the application
2. Will generate optimized class loader
3. Will compile common classes
4. Will set an application key by default
5. Will display the message that application is ready
It will take maximum five minutes to complete all above processes.

 Step 05  Open a browser
Now open a browser and type localhost/LaravelApps, you will get a page of directory. There you will find the newly created application myBlog, you have to click on that.

 Step 06  Get the welcome message
After clicking on the folder myBlog, you will get another folder named public. After clicking on the public folder, the Laravel welcome message will be displayed.

 Step 07  Get the directory
Now go to the directory D:xampphtdocsLaravelAppsmyBlogappHttpControllers, there you will get a php file named WelcomeController.php. Open this .php file in an editor like notepad++, netbeans etc and comment out the line number 33. Add another line after that echo “Hello World”; and save the document. Finally reload the page you have opened, you will must get your own defined welcome message Hello World.

So, you have successfully created your first hello word application using laravel.

« Previous Tutorial || Next Tutorial »

Advertisement



3. Configure laravel path globaly

After completing the installation of laravel and composer, you have to change the path following the steps given below. Otherwise laravel won’t work.

 Step 01  Get the directory

Find out laravel.bat file from the directory  C:UsersUzzwal DhaliAppDataRoamingComposervendorbin. If you don’t fond the AppData folder then change the settings like this. However, we need the directory, copy that.
 Step 02  Change the path
Now go to Start, right click on Computer / My Computer – a pop-up window will open. Click on Properties, then go to Advanced system settings – again a pop-up window will open, now click on Environmental Variables…

After clicking on Environmental Variables… , another pop-up window will open. Now select the line Path and click on the button Edit…

After clicking on the edit button, a pop-up window will open. Click on the text of the Variable value: and press the buttons Ctrl+A together and paste the path you have copied before in the first step.

 Step 03  Check Laravel if it is working or not
Press Windows Key+R together, the run command box will open. Now type cmd as the run command and press the Enter Key, the command prompt will open.

Now type Laravel and press the Enter Key, you will get something as shown in the image given bellow. It means the Laravel is working in your computer system perfectly.

« Previous Tutorial
Next Tutorial »


Advertisement

2. Installing laravel in windows computer

To work with laravel you must install both php composer and laravel. From the previous post, you have learned to install php composer. Now by this post you will learn to install laravel.
 Step 01  Get the command line
Go to the link http://laravel.com/docs/5.0#install-laravel and copy the command line composer global require “laravel/installer=~1.1”

 Step 02  Open command prompt and install laravel
Press Windows Key+R together to open the run command box. Now type cmd as run command and press the Enter Key. Paste the above command line you have copied and press the Enter Key. Soon the installation will start, but it will take few moments to be installed completely.

After completion you will get messages like the image given below.

« Previous Tutorial
Next Tutorial »


Advertisement

1. Installing composer for laravel in windows computer

To work with laravel using windows computer we have to install both of php composer and laravel. In this post I will show how to install composer. The installation process is not so tough, we just need to download composer setup file of 660 KB and later we need to install that.

But, to install composer there should have xampp installed in the computer system. If you don’t have, then install xampp first before starting this installation.

 Step 01  Get the download link
First of all go to the link https://getcomposer.org/download/ and scroll down till you get the link as shown in the image below.

After getting the link, simply click on that.

 Step 02  Download composer
After clicking on the link, a pop-up window will open as like as the image given below.

Now you have to click on the button Save File, the download process will run.

 Step 03  Locate the file and run the setup
After completing download, now locate the composer setup file and double click on to install. Then a pop-up window will appear with two buttons, click on Run to start installation. After clicking on Run button, the Composer Setup window will open, simply click on Next>. A new window will appear, again click on Next>.

 Step 04  Select php.exe file
In this step you will get a window as like as the image given below. Look, there is a browse button where I have kept the mouse pointer. You need to locate and select the php.exe file from the xampp installation folder. So click on the browse button and select the php.exe file.

For example, the directory of the php.exe file will be D:xamppphpphp.exe. After selecting the file click on the Next> button.

 Step 05  Install composer
After a very few moments a new window will appear where you will get a button named Install, click on that. The installation will start and soon you will get a new window with the finish button. Click on that to complete the installation.

Check if composer is installed or not…
Now you have to check if composer is properly installed or not. To do so, first of all press Windows Key+R together, the run command box will open.

Type cmd as command and press the Enter Key. Now type composer and press the Enter Key once again, you will get something as like as the image given below. That means you have successfully installed composer.

Next Tutorial »


Advertisement

Bubble sort program in java language


Bubble sorting program using java programming language

The java program given below is a bubble sort program, which is able to sort a collection of integer type numbers provided by the user. After running the program, the user will be asked to provide limit. That means, how many data he/she want to store. The 8th line of the program will display a message saying, “Enter the limit: “.

After providing the limit, it will be stored into an integer type variable limit by the 14th line. But, if we want to get any input then we must have to create Scanner Object as I have done by the 11th line. And we also import the Scanner package as like as the first line. In the 11th line I have created an Scanner Object named input, but you may name it anything as you like. As I have named it input, that’s why I have used int limit = input.nextInt(); to store the limit.

By the 17th line, the user will be asked to store the data into the array. As I need to store data into an array, that’s why I have declared an integer type array by the 20th line. In 23rd line there is a for-loop to store data into the array.

java program,bubble sorting program
import java.util.Scanner;
public class bubbleSort{
public static void main(String args[]){
//Declaring variables
int i, j, temp;

//Displaying message
System.out.print("Enter the limit: ");

//Creating scanner object
Scanner input = new Scanner(System.in);

//Storing the limit into variable
int limit = input.nextInt();

//Displaying message
System.out.print("Enter the numbers: ");

//Declaring integer type array
int num[] = new int[limit];

//for-loop for storing data into the array
for(i=0; i<limit; i++){
num[i] = input.nextInt();
}

//Displaying message
System.out.print("The unsorted data into the array: ");

//for-loop for displaying the unsorted data
for(i=0; i<limit; i++){
System.out.print(num[i]+", ");
}

//for-loop to sort the entered data
for(i=0; i<limit; i++){
for(j=0; j<limit-1; j++){
if(num[j] > num[j+1]){
//Data Swapping
temp = num[j];
num[j] = num[j+1];
num[j+1] = temp;
}
}
}

//Displaying message
System.out.print ("nThe sorted data into the array: ");

//for-loop for displaying the sorted data
for(i=0; i<limit; i++){
System.out.print(num[i]+", ");
}
}
}

Advertisement


Binary search program in java


Operations of the java program given below
The java program given below is a binary search program. When you will run the program, then you will be asked to provide the limit. The 5th line is to show the message.

In the 8th line a scanner object input has been created, otherwise we couldn’t input anything using keyboard. And most importantly we have to import the packages as like as the first line.

By the 11th line an integer type data will be stored into a variable limit. We have to use limit to define how many data we will store into the array.

Using the 14th line we will again display a message to provide the items/data. 

import java.util.*;
public class bsa{
public static void main(String args[]){
//Message to input the limit
System.out.print("Enter the limit: ");

//Creating scanner object
Scanner input = new Scanner(System.in);

//Getting input into the variable limit
int limit = input.nextInt();

//Message to provide the items
System.out.print("Enter the items: ");

//Declaring int type array
int data[] = new int[limit];

//Storing data into an array
for(int i=0; i<limit; i++){
data[i] = input.nextInt();
}

//Message to provide the expected item
System.out.print("Enter an item to search: ");

//Storing expected item into variable
int searchItem = input.nextInt();

//Initializing beginning and end
int beg = 1, end = limit;

//Defining the mid
int mid = (beg + end)/2;

//While loop to find out the data
while(beg <= end && data[mid] != searchItem){
if(searchItem < data[mid]){
end = mid - 1;
} else {
beg = mid + 1;
}

mid = (beg + end)/2;
}

//Increasing the array position by 1
int arrayPos = mid + 1;

//If statement to display message if data found or not
if(data[mid] == searchItem){
System.out.println("Data found at position: "+arrayPos);
} else {
System.out.println("Data not found!!!");
}
}
}

Advertisement


Linear search program in java for character type data


The java program given below is capable to perform linear search for characters. When you will run the program then you will be asked to provide the limit, that means how many characters do you want to provide. After providing the limit, you will be asked to provide the characters one by one.

And then you will be asked to provide a single character that you want to search. After providing the second for-loop of the program will check if there is the expected character. If found then a message will be displayed saying, “The character found at position: n” and if not found, then another message will be displayed saying, “The character  x has not found!!!

import java.util.*;
public class charSearch{
public static void main(String args[]){
//DECLARING AN INTEGER TYPE VARIABLE
int flag=0;

//CREATING SCANNER OBJECT
Scanner input = new Scanner(System.in);

//MESSAGE TO INPUT THE LIMIT
System.out.print("Enter the limit: ");

//GETTING INPUT
int limit = input.nextInt();

//DECLARING AN STRING TYPE ARRAY
char chars[] = new char[limit];

//MESSAGE TO INPUT CHARACTERS
System.out.print("Input the characters now: ");

//STORING THE CHARACTERS INTO AN ARRAY
for(int i=0; i<limit; i++){
chars[i] = input.next().charAt(0);
}

//MESSAGE TO INPUT A CHARACTER TO SEARCH
System.out.print("Enter the character you want to search: ");

//STORING THE CHARACTER INTO VARIABLE
char searchChar = input.next().charAt(0);

//CHECKING THE EXPECTING CHARACTER INTO THE ARRAY
for(int j=0; j<limit; j++){
if(chars[j]==searchChar){
//SETTING THE POSITION FOR THE CHARACTER
int k = j+1;
//DISPLAYING MESSAGE IF THE EXPECTED CHARACTER FOUND
System.out.println("The character found at position: "+k);
//SETTING FLAG=1 IF THE CHARACTER FOUND
flag = 1;
}
}

//DISPLAYING MESSAGE IF THE EXPECTED CHARACTER DOESN'T EXIST
if(flag==0){
System.out.println("The character "+searchChar+" has not found!!!");
}
}
}