0% found this document useful (0 votes)
1K views5 pages

Practical No.7

The document outlines a practical assignment for a Data Structures and Algorithms Lab, focusing on implementing searching techniques in a contact manager app using both binary and linear search methods. It includes detailed algorithms, C code implementations, and common errors with their fixes. Additionally, it compares the two search methods in terms of requirements, complexity, and suitability for different data sets.

Uploaded by

imhacker181006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views5 pages

Practical No.7

The document outlines a practical assignment for a Data Structures and Algorithms Lab, focusing on implementing searching techniques in a contact manager app using both binary and linear search methods. It includes detailed algorithms, C code implementations, and common errors with their fixes. Additionally, it compares the two search methods in terms of requirements, complexity, and suitability for different data sets.

Uploaded by

imhacker181006
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Name: Vivek Gajanan Sarode

Roll No.:SE-2=28
Batch: S6
Subject: Data Structures and Algorithms Lab

Title: Practical No.7

(Searching Techniques)
Problem Statement:
You are building a contact manager app. A user wants to search for a contact either by scanning
one by one or by using a fast lookup if the list is sorted. Write a program that locates a specific
name using both sequential and binary search
techniques.
Date of Performance:14/7/2025
Date of Submission:
1)Binary Search:
Algorithm:
Step 1: Start
Step 2: Define Constants and Structure
Define a constant SIZE for array size.
Define a structure Contact_Manage with fields:
name (character array)
mobile_no (integer)
email_id (character array)
Step 3: Declare Global Contact Array
Declare an array contact[SIZE] of structure type.
Step 4: Read Number of Contacts
Prompt the user to enter the number of contacts (n).
Show a message: “Please enter contact names in sorted (A-Z) order”.
Step 5: Input Contact Details
Repeat for i = 0 to n-1:
Input contact[i].name
Input contact[i].mobile_no
Input contact[i].email_id
Step 6: Search a Contact by Name Using Binary Search
Step 6.1: Input the Search Name
Ask the user to enter a search_name
Step 6.2: Call binarySearch() Function
Pass search_name and n to binarySearch()
Store the result in variable result
Step 7: Binary Search Logic (inside binarySearch() function)Step 8: Display the
Result
If result != -1:
Print contact details at contact[result]
Else:
Print “Contact not found”
Step 8: Display the Result
If result != -1:
Print contact details at contact[result]
Else:
Print “Contact not found”
Step 9: End
C Program:
#include <stdio.h>
#include <string.h>
#define SIZE 100
struct Contact_Manage {
char name[SIZE];
int mobile_no;
char email_id[SIZE];
}contact[SIZE]; /* Declare global array*/
/* Binary search function (requires sorted data)*/
int binarySearch(char target[], int n) {
int beg = 0, end = n - 1;
while (beg <= end) {
int mid = beg + (end - beg) / 2;
int cmp = strcmp(contact[mid].name, target);
if (cmp == 0) {
return mid; /* Found*/
} else if (cmp < 0) {
beg = mid + 1;
} else {
end = mid - 1;
}
}
return -1; /* Not found*/
}
int main() {
int n, i, result;
char search_name[SIZE];
printf("Enter the number of contacts: ");
scanf("%d", &n);
printf("\nNote: Please enter contact names in sorted (A-Z) order!\n");
for (i = 0; i < n; i++)
{ printf("\nEnter details for contact %d:\n", i + 1);
printf("Enter the name: ");
scanf("%s", contact[i].name);
printf("Enter the mobile number: ");
scanf("%d", &contact[i].mobile_no);
printf("Enter the email id: ");
scanf("%s", contact[i].email_id);
}
printf("\nEnter the name to search: ");
scanf("%s", search_name);
result = binarySearch(search_name, n);
if (result != -1) {
printf("\nContact Found at index %d\n", result);
printf("Name : %s\n", contact[result].name);
printf("Mobile No : %d\n", contact[result].mobile_no);
printf("Email ID : %s\n", contact[result].email_id);
} else {
printf("\nContact not found.\n");
}
return 0;
}

/*Output
Enter the number of contacts: 3

Note: Please enter contact names in sorted (A-Z) order!

Enter details for contact 1:


Enter the name: Anjali
Enter the mobile number: 9876543210
Enter the email id: anjali@[Link]

Enter details for contact 2:


Enter the name: Deepak
Enter the mobile number: 9123456789
Enter the email id: deepak@[Link]

Enter details for contact 3:


Enter the name: Kavita
Enter the mobile number: 9988776655
Enter the email id: kavita@[Link]

Enter the name to search: Anjali

Contact Found at index 0


Name : Anjali
Mobile No : 1286608618
Email ID : anjali@[Link]

Press Enter to return to Quincy...*/

2)Linear Search:
Algorithm:
Step 1: Start
Step 2: Define Constants and Structure
Define a constant SIZE to specify the maximum size of arrays.
Define a structure Contact_Manage with the following fields:
name (character array)
mobile_no (integer)
email_id (character array)
Step 3: Declare a Global Array
Declare an array contact[SIZE] of type struct Contact_Manage.
Step 4: Input Contact Details
Step 4.1: Ask user to enter number of contacts (n).
Step 4.2: Repeat the following for i = 0 to n - 1:
Ask user to enter:
contact[i].name
contact[i].mobile_no
contact[i].email_id
Step 5: Search for a Contact using Linear Search
Step 5.1: Ask user to enter the name to be searched (search_name).
Step 5.2: Call linearSearch(search_name, n)
Step 6: Linear Search Logic
In linearSearch() function:
Loop i = 0 to n - 1
Compare contact[i].name with target using strcmp()
If match found: return index i
If loop ends without finding match: return -1
Step 7: Display the Result
If returned index ≠ -1:
Display:
Name
Mobile Number
Email ID
Else:
Print "Contact not found"
Step 8: End

C Program:
#include <stdio.h>
#include <string.h>
#define SIZE 100
struct Contact_Manage {
char name[SIZE];
int mobile_no;
char email_id[SIZE];
}contact[SIZE]; /* Global array*/
/* Linear search function*/
int linearSearch(char target[], int n)
{ int i;
for (i = 0; i < n; i++)
{if (strcmp(contact[i].name, target) == 0)
{return i; /*Contact found*/
}
}
return -1; /* Not found*/
}
int main() {
int n, i, result;
char search_name[SIZE];
printf("Enter the number of contacts: ");
scanf("%d", &n);
for (i = 0; i < n; i++) {
printf("\nEnter details for contact %d:\n", i + 1);
printf("Enter the name: ");
scanf("%s", contact[i].name);
printf("Enter the mobile number: ");
scanf("%d", &contact[i].mobile_no);
printf("Enter the email id: ");
scanf("%s", contact[i].email_id);
}
printf("\nEnter the name to search: ");
scanf("%s", search_name);
result = linearSearch(search_name, n);
if (result != -1)
{printf("\nContact Found at index %d\n", result);
printf("Name : %s\n", contact[result].name);
printf("Mobile No : %d\n", contact[result].mobile_no);
printf("Email ID : %s\n", contact[result].email_id);
} else
{ printf("\nContact not found.\n");
}
return 0;
}
/*Output :
Enter the number of contacts: 3

Enter details for contact 1:


Enter the name: Anjali
Enter the mobile number: 9876543210
Enter the email id: anjali@[Link]

Enter details for contact 2:


Enter the name: Deepak
Enter the mobile number: 9123456789
Enter the email id: deepak@[Link]

Enter details for contact 3:


Enter the name: Kavita
Enter the mobile number: 9988776655
Enter the email id: kavita@[Link]

Enter the name to search: Deepak


Contact Found at index 1
Name : Deepak
Mobile No : 533522197
Email ID : deepak@[Link]

[Process completed - press Enter]*/

Errors and how to fix them :


[Link] variable i
error: 'i' undeclared (first use in this function)
Cause: Using variable i in a loop without declaring it.
Fix:
Declare int i; before using the loop:
int i;
for (i = 0; i < n; i++)
{ // your code
}
[Link] == to compare strings
Error: Binary search/linear search condition like:
if (contact[mid].name == target) // WRONG!
Cause:
You can't compare strings using == in C. It compares addresses, not actual
string content.
Fix:
Use strcmp() from <string.h>:
if (strcmp(contact[mid].name, target) == 0)
[Link] correct array indexing or exceeding limits
Error: Using an index out of bounds like contact[n] or using uninitialized
variables.
Fix:
Ensure your loops run from i = 0 to i < n.
Do not use contact[n] if n is the number of contacts — valid indices are 0 to n-
1.
[Link] scanf() usage
Error: scanf("%s", &contact[i].name); // WRONG
Cause:
contact[i].name is already a pointer to the first element (a char array), so &
is not needed.
Fix:
scanf("%s", contact[i].name); // Correct
[Link] input is sorted when using binary search
Logical Error (not compile-time):The binary search will fail or return wrong
result if the input names are not in alphabetical order.
Fix:
Make sure the user enters contacts in sorted order, or add a sorting function
before calling binary search.

Compering Binary Search and Linear search:


Feature Binary Search Linear Search
Requires sorted list: Yes No
Code complexity: Medium Simple
Time complexity: O(log n) O(n)
Suitable for: Large sorted lists Small or unsorted lists

You might also like