0% found this document useful (0 votes)
6 views20 pages

DS Ass1

The document provides an overview of three fundamental algorithms: Linear Search, Binary Search, and Bubble Sort. It explains how each algorithm works, their characteristics, advantages, disadvantages, and applications, along with example C++ code for implementation. The document highlights the simplicity of Linear Search and Bubble Sort, while emphasizing the efficiency of Binary Search for large datasets.

Uploaded by

nakulanr307
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)
6 views20 pages

DS Ass1

The document provides an overview of three fundamental algorithms: Linear Search, Binary Search, and Bubble Sort. It explains how each algorithm works, their characteristics, advantages, disadvantages, and applications, along with example C++ code for implementation. The document highlights the simplicity of Linear Search and Bubble Sort, while emphasizing the efficiency of Binary Search for large datasets.

Uploaded by

nakulanr307
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
You are on page 1/ 20

1

Linear Search :
Linear Search is one of the simplest
searching algorithms used in computer
science to find a particular element
(referred to as the "target") in a list or array.
It is a straightforward approach that
involves checking each element in the list
sequentially until the desired element is
found or the end of the list is reached.

How It Works
1. Initialization:
 Start from the first element of the
list.
2. Comparison:
 Compare the current element with
the target element
3. Decision:
 If the current element matches the
target, the search is successful, and
its position (index) is returned.

2
 If it does not match, move to the
next element in the list.
4. Termination: Repeat steps 2 and 3
until:
 The target element is found, or
 The end of the list is reached (in
which case the target is not in the
list).

Characteristics:
Complexity:
 Best Case: O(1), when the target is
found at the first position.
 Average Case: O(n), where n is the
number of elements in the list.
 Worst Case: O(n), when the target is
at the end of the list or not present.
Iterative or Recursive:
 Can be implemented either iteratively or
recursively.

3
Search Space:
 Works on both sorted and unsorted lists,
as it does not assume any particular
order of elements.
Ease of Implementation:
 Simple and requires no additional
memory or preprocessing of data.

Advantages:
 Simplicity:
Easy to understand and implement.
 No Extra Space:
Does not require any additional data
structures.
 Versatility:
Works on any data structure that allows
sequential access, such as arrays, linked
lists, or files.

4
Disadvantages:
 Inefficiency:
Can be slow for large datasets as it
examines each element.
 Not Optimized:
Does not take advantage of sorted data
or other potential efficiencies.

Applications:
 Small Data Sets: Suitable when the list
is small and the overhead of more
complex algorithms is not justified.
 Unsorted Data: Useful when data is
not sorted and cannot be sorted easily.
 Preliminary Search: Often used as a
basic algorithm for teaching or as a
fallback in hybrid approaches.

Example program using C++:


5
#include <iostream>

using namespace std;

int linearSearch(int arr[], int size, int target) {

for (int i = 0; i < size; i++) {

if (arr[i] == target) {

return i;

return -1;

int main() {

int size, target;

cout << "Enter the number of elements in the array: ";


cin >> size;

int arr[size];

cout << "Enter the elements of the array:\n";

for (int i = 0; i < size; i++) {

cin >> arr[i];

cout << "Enter the element to search: ";

cin >> target;

int result = linearSearch(arr, size, target);

if (result != -1) {

6
cout << "Element found at index " << result << endl;

} else {

cout << "Element not found in the array.\n";

return 0;

Output:
Enter the number of elements in the array:
5
Enter the elements of the array:4 5 6 2 1
Enter the element to search: 6
Element found at index 2

Binary Search:

7
Binary Search is an efficient algorithm
used to find the position of a target element
in a sorted array or list. Unlike Linear
Search, Binary Search divides the search
space into halves, making it significantly
faster for large datasets

How It Works:
Binary Search works on the principle of
divide and conquer. It repeatedly divides
the array into two halves and eliminates the
half that cannot contain the target element.
Initialization:
 Set two pointers: low at the start of the
array and high at the end of the array.
Finding the Middle:
 mid=low+(high−low)/2

Comparison:

8
 If the middle element matches the
target, return its index.
 If the middle element is greater than the
target, focus on the left half by setting
high = mid - 1.
 If the middle element is less than the
target, focus on the right half by setting
low = mid + 1.
Repeat:
 Continue this process until the low
pointer exceeds the high pointer,
indicating that the target is not in the
array.

Characteristics:
Precondition:
 The array must be sorted
Complexity:
 Best Case: O(1), when the target is at
the middle.
 Average Case: )O(log n), where n is the
number of elements.
9
 Worst Case: O(logn), when the search
space is halved until one element
remains.
Space Complexity:
 O(1) for the iterative approach and
O(logn) for the recursive approach due
to the stack space.

Advantages:
Efficiency:
 Faster than Linear Search for large
datasets
Predictable:
 Guarantees O(logn) complexity.
Versatile:

 Can be adapted for searching in


databases, strings, and more.

10
Disadvantages:
1. Pre-Sorting:
 Requires the array to be sorted
beforehand, which can add
overhead.
2. Limited Use:
 Not suitable for dynamic or unsorted
datasets without sorting them first.
3. Non-Trivial Implementation:
 Slightly more complex to implement
compared to Linear Search.

Applications:
1. Searching in Large Data Sets:
 Used in databases and file systems.
2. Algorithm Foundations:
 Forms the basis for more advanced
search algorithms.

11
3. Range Queries: Often used in
competitive programming for range-
based problems.

Example program using C++:


#include <iostream>

using namespace std;

int binarySearch(int arr[], int size, int target) {

int low = 0, high = size - 1;

while (low <= high) {

int mid = low + (high - low) / 2;

if (arr[mid] == target)

return mid;

else if (arr[mid] < target)

low = mid + 1;

else

high = mid - 1;

return -1;

int main() {

int size, target;

cout << "Enter the number of elements in the array: ";

cin >> size;

12
int arr[size];

cout << "Enter the sorted elements of the array:\n";

for (int i = 0; i < size; i++) {

cin >> arr[i];

cout << "Enter the element to search: ";

cin >> target;

int result = binarySearch(arr, size, target);

if (result != -1) {

cout << "Element found at index " << result << endl;

} else {

cout << "Element not found in the array.\n";

return 0;

Output:
Enter the number of elements in the array:
5
Enter the sorted elements of the array:10
20 30 40 50
Enter the element to search: 40

13
Element found at index 3

Bubble Sort:
Bubble Sort is one of the simplest sorting
algorithms in computer science. It
repeatedly compares adjacent elements in a
list and swaps them if they are in the wrong
order. The process continues until the list is
sorted. It is called "bubble sort" because
smaller elements "bubble" to the top (front)
of the list, and larger elements "sink" to the
bottom (back).

How It Works:
1. Initialization:
 Start with the first element of the
list.
2. Comparison:
 Compare each pair of adjacent
elements.
3. Swap:

14
 If the first element is greater than
the second, swap them.

4. Repeat:
 Move to the next pair and repeat the
process for the entire list.
5. Iterations:
 After the first pass, the largest
element will be at the end of the list.
 Exclude the last sorted element and
repeat the process for the remaining
unsorted part of the list.
6. Termination:
 Continue until no swaps are needed,
indicating that the list is sorted.

Characteristics
1. Stability:

15
 Bubble Sort is stable, meaning that
equal elements retain their relative
order.

2. Complexity:
o Best Case: O(n), when the list is
already sorted (with an optimized
version).
o Average Case: O(n^2).
o Worst Case: O(n^2), when the list is
sorted in reverse order.
3. Space Complexity:
 O(1) as it is an in-place sorting
algorithm.
4. Ease of Implementation:
 Easy to understand and implement

Advantages:
1. Simplicity:

16
 Straightforward and easy to code.
2. Stable Sorting:
 Maintains the order of equal
elements.
3. No Additional Space:
 Does not require extra memory for
sorting

Disadvantages
1. Inefficiency:
 Performs poorly on large datasets.
2. Redundant Comparisons:
 Makes unnecessary passes even if
the list is already sorted (in its naive
implementation).
3. Not Practical for Large Inputs:
 Better algorithms like Merge Sort or
Quick Sort are preferred for larger
datasets.

Applications:
17
1. Small Data Sets:
 Works well for small or nearly sorted
datasets.
2. Educational Purposes:
 Used as an introductory sorting
algorithm to teach basic concepts of
sorting.

3. Easy Debugging:
 Due to its simplicity, it is easier to
debug and visualize.

Example program using C++:


#include <iostream>

using namespace std;

void bubbleSort(int arr[], int size) {

for (int i = 0; i < size - 1; i++) {

bool swapped = false;

for (int j = 0; j < size - i - 1; j++) {

if (arr[j] > arr[j + 1]) {

// Swap the elements

int temp = arr[j];

18
arr[j] = arr[j + 1];

arr[j + 1] = temp;

swapped = true;

if (!swapped)

break;

int main() {

int size;

cout << "Enter the number of elements: ";

cin >> size;

int arr[size];

cout << "Enter the elements:\n";

for (int i = 0; i < size; i++) {

cin >> arr[i];

bubbleSort(arr, size);

cout << "Sorted array: ";

for (int i = 0; i < size; i++) {

cout << arr[i] << " ";

return 0;

19
}

Output:
Enter the number of elements: 5
Enter the elements:0 52 63 99 100
Sorted array: 0 52 63 99 100

20

You might also like