Searching and Sorting Algorithm
Searching and Sorting Algorithm
1
1. Finding a Contact in a Phonebook
If phone numbers are stored in an unsorted contact list, the phone system
checks each entry one by one to find the desired contact.
6. Debugging in Programming
When searching for a bug or error in a small piece of code, a developer
manually checks each line in a sequential manner.
2
8. Checking for Duplicates in a Small Datasets
In small data collections, checking for duplicate values can be done using
linear search before inserting a new value.
3
1. Searching in a Phonebook or Contact List
When contacts are sorted alphabetically, binary search helps find a name
quickly instead of checking each contact one by one.
4
8. Searching for a Product in an Online Store
E-commerce platforms use binary search to find products in sorted lists of
prices, ratings, or categories to improve search speed.
5
Binary Search is generally better for large datasets because it is much faster.
However, if the data is unsorted and cannot be easily sorted, Linear Search is the
only option.
Algorithm Steps:
1. Starting at the beginning of the list.
2. Compare the first two elements; if the first is greater than the second, swap
them.
3. Move to the next pair of elements and repeat step 2.
4. Continue this process for the entire list.
5. Repeat the entire process for n-1 passes until no swaps are needed.
6
Example of Bubble sort (Sorting [ 5, 3, 8, 4, 6])
1. First Pass: [3, 5, 4, 6, 8]
2. Second Pass: [3, 4, 5, 6, 8] (Sorted)
7
concern, or for educational purposes to demonstrate sorting concepts. Here are a
few potential real-world applications:
1. Teaching and Learning Algorithms
Educational Tool: Bubble Sort is often used in computer science and
algorithm courses to introduce sorting algorithms. Its simplicity helps students
understand basic sorting concepts, such as comparisons, swaps, and iteration.
2. Small Datasets
Small Data Collections: For small data sets, the overhead of more complex
algorithms (like Merge or Quick Sort) may not be necessary, and Bubble Sort can
perform adequately. For instance, sorting a few items in a small database or an
array might use Bubble Sort due to its simplicity.
4. Real-time Systems
Low-memory or Embedded Systems: In environments where memory is limited
and the data size is small, Bubble Sort can be considered because it doesn’t require
additional memory (space complexity is O (1)). Its simplicity means less resource
overhead in certain applications.
5. Stability Requirements
Stable Sorting: Bubble Sort is a stable sorting algorithm, meaning that it
preserves the relative order of elements with equal values. This property can be
useful in scenarios where secondary attributes need to remain unchanged).
8
6. Legacy Systems
Older Software Systems: In legacy systems or applications where the dataset
size has remained small or unchanging, developers may have used Bubble Sort
because it was easy to implement when resources and performance weren’t as big
of concerns.
In general, while there are more efficient algorithms for large datasets,
Bubble Sort still has niche applications in smaller, more controlled environments
or educational contexts.
9
2.3 Reasons of Using Searching and Sorting Algorithm
2. Faster Decision-Making
Sorting helps organize data, making it easier to analyze and process.
Example: In financial markets, sorting algorithms are used to arrange stock prices
for quick trading decisions.
10
A bad algorithm can take minutes or even hours to process data, while an
optimized one can do it in seconds.
Example: Instead of scanning an entire database (O(n) linear search), binary
search (O(log n)) reduces the number of comparisons drastically.
Searching and sorting algorithms make systems faster, more efficient, and
scalable. They are used in search engines, e-commerce, cybersecurity, AI, finance
and more. Choosing the right algorithm can mean the difference between a slow,
inefficient system and a high-performance application.
11
12