Skip to main content

Featured Post

How to use Tabs in ASP.NET CORE

I want to show Components in a tabs , so first of all create few components. In this project we have three components, First View Component  public class AllViewComponent : ViewComponent     {         private readonly UserManager<ApplicationUser> _userManager;         public AllViewComponent(UserManager<ApplicationUser> userManager)         {             _userManager = userManager;         }         public async Task<IViewComponentResult> InvokeAsync()         {             List<StudentViewModel> allUsers = new List<StudentViewModel>();             var items = await _userManager.Users.ToListAsync();             foreach (var item in items)             {                 allUsers.Add(new StudentViewModel {Id=item.Id, EnrollmentNo = item.EnrollmentNo, FatherName = item.FatherName, Name = item.Name, Age = item.Age, Birthdate = item.Birthdate, Address = item.Address, Gender = item.Gender, Email = item.Email });             }            

Linear Search in C language

Searching

Before writing the algorithm or program for searching , let us see, “ What is searching? What are the searching techniques?”
Definition : More often we will be working with the large amount of data. It may be necessary to determine whether a particular item is present in the large amount of data or not. This process of finding a particular item in the large amount of data is called searching. The two important and simple searching techniques are linear search and binary search.

SQL Video Channel : Download all SQL Video


Linear search (Sequential search)
“What is the linear search?”
Definition: Linear search is a simple searching techniques in which we search for a given key item in the list in linear order(Sequential order) i.e. one after the other. The item to be searched is often called key item. The linear search is also called sequential search.

For example , if key=10 and the list is 20,10,40,25 after searching we say that key is present . If key =100 after searching we say that key is not present.
“How to search for an item in a list of elements?” The procedure is as follows.

Linear/Sequencial Search in C


Procedure: Assume 10 is the item to be searched in the list of items 50,40,30,60,10. Observe from above figure that, 10 has to be compared with a[0], a[1], a[2], a[3], a[4].
But, once the value of I is greater than or equal to 5, it is an indication that item is not present and display the message “Unsuccessful Search”.

Note: In general the terminal condition in the for loop i<5 can be replaced by i<n.
You are already familiar with the algorithm and flowchart of linear search . Now, the C program for linear search is shown below:

#include<stdio.h>
#include<conio.h>
main()
{
int i,n,key,a[20];
printf("Enter the value of n.");
scanf("%d",&n);
printf("Enter n values:\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter item to search:");
scanf("%d",&key);
/* Search the key in array */
for(i=o;i<n;i++)
{
if(a[i]==key)
{
printf("item found");
exit(0);
}
}
printf("Not found");
}

Output
Linear Search in C

Linear Search in C

Advantage of linear search

  • Very simple approach
  • Works well for small array
  • Used to search when the elements are not sorted (not in any order)
Disadvantage of linear search

  • Less efficient if the array size is large
  • If the elements are already sorted, linear search is not efficient.

Note : When the elements are not sorted and size is very less, linear search is used . if the elements are sorted , a better method or technique binary search can be used.

Binary Search in C
Selection Search in C

Comments

Popular Post

Polynomial representation using Linked List for Data Structure in 'C'

Polynomial representation using Linked List The linked list can be used to represent a polynomial of any degree. Simply the information field is changed according to the number of variables used in the polynomial. If a single variable is used in the polynomial the information field of the node contains two parts: one for coefficient of variable and the other for degree of variable. Let us consider an example to represent a polynomial using linked list as follows: Polynomial:      3x 3 -4x 2 +2x-9 Linked List: In the above linked list, the external pointer ‘ROOT’ point to the first node of the linked list. The first node of the linked list contains the information about the variable with the highest degree. The first node points to the next node with next lowest degree of the variable. Representation of a polynomial using the linked list is beneficial when the operations on the polynomial like addition and subtractions are performed. The resulting polynomial can also

How to use Tabs in ASP.NET CORE

I want to show Components in a tabs , so first of all create few components. In this project we have three components, First View Component  public class AllViewComponent : ViewComponent     {         private readonly UserManager<ApplicationUser> _userManager;         public AllViewComponent(UserManager<ApplicationUser> userManager)         {             _userManager = userManager;         }         public async Task<IViewComponentResult> InvokeAsync()         {             List<StudentViewModel> allUsers = new List<StudentViewModel>();             var items = await _userManager.Users.ToListAsync();             foreach (var item in items)             {                 allUsers.Add(new StudentViewModel {Id=item.Id, EnrollmentNo = item.EnrollmentNo, FatherName = item.FatherName, Name = item.Name, Age = item.Age, Birthdate = item.Birthdate, Address = item.Address, Gender = item.Gender, Email = item.Email });             }            

Memory representation of Linked List Data Structures in C Language

                                 Memory representation of Linked List              In memory the linked list is stored in scattered cells (locations).The memory for each node is allocated dynamically means as and when required. So the Linked List can increase as per the user wish and the size is not fixed, it can vary.                Suppose first node of linked list is allocated with an address 1008. Its graphical representation looks like the figure shown below:       Suppose next node is allocated at an address 506, so the list becomes,   Suppose next node is allocated with an address with an address 10,s the list become, The other way to represent the linked list is as shown below:  In the above representation the data stored in the linked list is “INDIA”, the information part of each node contains one character. The external pointer root points to first node’s address 1005. The link part of the node containing information I contains 1007, the address of