Sorting and Searching
Sorting and Searching
•Searching is the process of determining whether or not a given value exists in a data
structure or a storage media.
1. Sequentially scan the array, comparing each array item with the searched value.
2. If a match is found; return the index of the matched element; otherwise return –1.
Sorting:
Internal Sorting :
When all data is placed in the main memory or internal memory then sorting is
called internal sorting.
In internal sorting, the problem cannot take input beyond its size.
Example: heap sort, bubble sort, selection sort, quick sort, shell sort, insertion sort.
External Sorting :
We can use an external sort when the collection of data cannot fit in the computer’s main
memory all at once but must reside in secondary storage such as on a disk
External Sorting is used for the massive amount of data.
Merge Sort and its variations are typically used for external sorting.
Some external storage like hard disks and CDs are used for external sorting.
Example: Merge sort, Tag sort, Polyphase sort, Four tape sort, External radix sort,
Internal merge sort, etc.
Sort Order :
• The order in which the data is organized, either ascending order or descending order, is
called sort order
Sort Stability
• A sorting method is said to be stable if at the end of the method, identical elements occur in
the same relative order as in the original unsorted set
•Sort Efficiency
• In addition, the characteristic of a sort pass is the placement of one or more elements in a
sorted list
Application of sorting
1. The sorting is useful in database applications for arranging the data in desired ORDER.
3. For searching the element from the list of elements the sorting is required
5. For finding the closest pair from the list of elements the sorting is required.
Bubble sort:
In bubble sorting, consecutive adjacent pairs of elements in the array are compared
with each other. If the element at the lower index is greater than the element at the
higher index, the two elements are interchanged so that the element is placed before
the bigger one. This process will continue till the list of unsorted elements exhausts.
time complexity:
1. Time complexity: O(n^2) in the worst and average cases, O(n) in the best case (when
the input array is already sorted)
Space complexity: O(1)
2. Basic idea: Iterate through the array repeatedly, comparing adjacent pairs of
elements and swapping them if they are in the wrong order. Repeat until the array is
fully sorted.
Advantages:
Simple implementation
works well for small datasets
requires only constant space, stable sorting algorithm
Disadvantages:
selection sort:
Selection sorting is conceptually the simplest sorting algorithm. This algorithm first
finds the smallest element in the array and exchanges it with the element in the first
position, then finds the second smallest element and exchange it with the element in
the second position, and continues in this way until the entire array is sorted
Auxiliary Space: O(1) as the only extra memory used is for temporary variables while
swapping two values in Array. The selection sort never makes more than O(N) swaps and
can be useful when memory writing is costly.
Example Consider an array of integers given below. We will sort the values in the array
using insertion sort
23 15 29 11 1
Advantages:
It can be efficiently implemented on data sets that are already substantially sorted.
The insertion sort is an in-place sorting algorithm so the space requirement is minimal.
Disadvantages: o
MERGE SORT
The merge sort recursively follows the steps
. 4) Combine the sorted left and right partitions into a single sorted array.
Advantages :
Merge sort algorithm is best case for sorting slow-access data e.g) tape drive
. Disadvantages:
Merge sort algorithm requires additional memory space of 0(n) for the temporary
array .
The running time of merge sort in the average case and the worst case can be given as
O(n logn).
Although merge sort has an optimal Tim complexity, it needs an additional space of
O(n) for the temporary array TEMP.
Applications
Merge Sort is useful for sorting linked lists in O(nLogn) time.
Shell sort
linked list:
Linked List is a linear data structure, in which elements are not stored at a contiguous
location, rather they are linked using pointers. Linked List forms a series of connected
nodes, where each node stores the data and the address of the next node.Linked List is
a linear data structure, in which elements are not stored at a contiguous location,
rather they are linked using pointers. Linked List forms a series of connected nodes,
where each node stores the data and the address of the next node.
delete(insert(I,L)) ::= L
13. Can have multiple head Only one head and tail
and tail pointers pointer is required
It is the simplest type of linked list in which every node contains some data
and a pointer to the next node of the same data type.
This would enable us to traverse the list in the backward direction as well.
3. Circular Linked List
A circular linked list is that in which the last node contains the pointer to
the first node of the list.
The difference between the doubly linked and circular doubly list is the same as
that between a singly linked list and a circular linked list.
The circular doubly linked list does not contain null in the previous field of the
first node
1. Queues are generally utilized as hanging tight records for a solitary shared asset like
printer, plate, CPU.
2. Queues are utilized in offbeat exchange of information (where information isn't being
moved at similar rate between two cycles) for eg. pipes, document IO, attachments.
3. Queues are utilized as cradles in the greater part of the applications like MP3 media
player, CD player, and so on
4. Queue are utilized to keep up the play list in media major parts to add and eliminate
the tunes from the play-list.
Advantages:
Orderly Processing: Queues follow a first-in, first-out (FIFO) order, ensuring that tasks
or elements are processed in the order they arrive.
Task Scheduling: Queues are useful for managing and scheduling tasks in a systematic
manner, making them suitable for various applications like print spooling, job scheduling, etc.
Buffering: Queues act as buffers in systems, allowing for temporary storage of data or tasks
when the rate of production and consumption varies.
Data Structures: Queues are fundamental data structures used in algorithms and program
design, making them essential for organizing and manipulating data.
Disadvantages of Queues:
Limited Access: Queues typically offer limited access to elements, with operations like
enqueue (adding to the end) and dequeue (removing from the front). Access to elements in
the middle is usually restricted.
Overhead: Implementing and managing queues can introduce overhead in terms of memory
and processing, especially in scenarios with frequent enqueue and dequeue operations.
Complexity: In some scenarios, managing queues can add complexity to the system design,
especially when dealing with priority queues, circular queues, or other variations.
Waiting Time: In systems where tasks are queued up, there can be an inherent waiting time for
tasks to be processed, leading to potential delays in overall system responsiveness.
circular queue:
A Circular Queue is an extended version of a normal queue where the last element of
the queue is connected to the first element of the queue forming a circle.
The operations are performed based on FIFO (First In First Out) principle. It is also
called ‘Ring Buffer’.
7.3.Uses of Circular Queue
roundabout Queue can be utilized in the accompanying situations: Memory the board:
The roundabout queue gives memory the executives. As we have just seen that in linear
queue, the memory isn't overseen proficiently. Yet, if there should arise an occurrence
of a roundabout queue, the memory is overseen effectively by putting the components
in an area which is unused.
CPU Scheduling: The working framework likewise utilizes the circular queue to embed
the cycles and afterward execute them.
Traffic framework: In a PC control traffic framework, traffic signal is probably the best
illustration of the circular queue. Each light of traffic signal gets ON individually after
each jinterval of time. Like red light gets ON briefly then yellow light briefly and
afterward green light. After green light, the red light gets ON