PST Unit4
PST Unit4
Sorting is a process of ordering or placing a list of elements from a collection in ascending or descending order. The
different types of sorting methods are (a) Bubble sort (b) Shell sort (c) Quick sort (d) Insertion sort (e ) Selection sort (f)
Heap sort (g) Merge sort etc.
Example:
Consider the same array of integers: {5, 2, 8, 12, 3}.
o In the first iteration, the algorithm finds the minimum element as 2 and swaps it with the first element. The
array becomes: {2, 5, 8, 12, 3}.
o In the second iteration, the algorithm finds the minimum element in the unsorted portion (starting from the
second element) as 3 and swaps it with the second element. The array becomes: {2,5, 8, 12, 3}.
o In the third iteration, the algorithm determines that the third element is the minimum element in the unsorted
segment, beginning from that element, and swaps it with that element. The array becomes: {2, 3, 8, 12, 5}.
o In the fourth iteration, the algorithm finds the minimum element in the unsorted portion (starting from the
fourth element) as 5 and swaps it with the fourth element. The array becomes: {2, 3, 5, 12, 8}.
o In the fifth iteration, there is only one element left in the unsorted portion, so no swaps occur.
OUTPUT
Array before Sorting: 12 19 55 2 16
Array after Sorting: 2 12 16 19 55
Bubble sort/ Sort by exchange
Bubble sort is a simple sorting algorithm. This sorting algorithm is comparison-based algorithm in which each pair of
adjacent elements is compared and the elements are swapped if they are not in order. This algorithm is not suitable for
large data sets as its average and worst case complexity are of O(n2) where n is the number of items.
Algorithm
Step 1 − Check if the first element in the input array is greater than the next element in the array.
Step 2 − If it is greater, swap the two elements; otherwise move the pointer forward in the array.
Step 3 − Repeat Step 2 until we reach the end of the array.
Step 4 − Check if the elements are sorted; if not, repeat the same process (Step 1 to Step 3) from the last element of the
array to the first.
Step 5 − The final output achieved is the sorted array.
Example:
Let's consider an array of integers: {5, 2, 8, 12, 3}.
o In the first pass, the algorithm compares 5 and 2, swaps them, and then compares 5 and 8 (no swap). Next, it
compares 8 and 12 (no swap), and finally, 12 and 3, resulting in a swap. The array becomes: {2, 5, 8, 3, 12}.
o In the second pass, the algorithm compares 2 and 5 (no swap), 5 and 8 (no swap), and 8 and 3, leading to a
swap. The array becomes: {2, 5, 3, 8, 12}.
o In the third pass, the algorithm compares 2 and 5 (no swap), 5 and 3, resulting in a swap. The array becomes: {2,
3, 5, 8, 12}.
o In the fourth pass, all elements are already sorted, so no swaps occur.
OUTPUT
Array before Sorting: 67 44 82 17 20
Array after Sorting: 17 20 44 67 82
Insertion sort/ Sort by insertion
It is a very simple method to sort numbers in an ascending or descending order. This method follows the incremental
method. It can be compared with the technique how cards are sorted at the time of playing a game.
Insertion is a sorting algorithm that arranges numbers in either ascending or descending order. It follows an incremental
approach, similar to how elements are sorted one by one.
Algorithm
Step 1 − If it is the first element, it is already sorted. return 1;
Step 2 − Pick next element
Step 3 − Compare with all elements in the sorted sub-list
Step 4 − Shift all the elements in the sorted sub-list that is greater than the value to be sorted
Step 5 − Insert the value
Step 6 − Repeat until list is sorted
Example:
Let's use the same array of integers: {5, 2, 8, 12, 3}.
o In the first iteration, the algorithm considers 5 as the first element, which is already sorted, so no changes occur.
o In the second iteration, the algorithm considers 2 and inserts it before 5, resulting in {2, 5, 8, 12, 3}.
o In the third iteration, the algorithm takes into account 8, which is already in the right place.
o In the fourth iteration, the algorithm takes into account 12, which is already in the right position.
o In the fifth iteration, the algorithm considers 3 and inserts it between 2 and 5, resulting in {2, 3, 5, 8, 12}.
OUTPUT
Array before Sorting: 67 44 82 17 20
Array after Sorting: 17 20 44 67 82
Shell sort/ Sort by diminishing increment
It is a highly efficient sorting algorithm and is based on insertion sort algorithm. This algorithm avoids large shifts as in
case of insertion sort, if the smaller value is to the far right and has to be moved to the far left.
Diminishing increment sort algorithms, like Shell sort, improve on insertion sort by comparing and swapping elements
that are separated by a certain gap. The gap decreases until it becomes 1, making the final pass an insertion sort.
Algorithm
Step1: Initialize the value of h(gap/Interval).
Step2: Divide the list into smaller sub-list of equal interval h.
Step3: Sort these sub-lists using insertion sort.
Step4: Repeat until complete list is sorted
Working
• The shell Sort function starts with a large gap (initially half of the array size) and gradually reduces the gap by dividing
it by 2 in each pass.
• For each gap value, it performs an insertion sort on the elements that are gap positions apart.
• The inner insertion sort loop compares and swaps elements that are separated by the current gap.
• This process continues with smaller gaps until the gap becomes 1, making the final pass an insertion sort pass over the
entire array.
OUTPUT
Array before Sorting: 33 45 62 12 98
Array after Sorting: 12 33 45 62 98
Quick sort/ Sort by partition
It is a highly efficient sorting algorithm and is based on partitioning of array of data into smaller arrays. A large array is
partitioned into two arrays one of which holds values smaller than the specified value, say pivot, based on which the
partition is made and another array holds values greater than the pivot value.
Example:
Using the array of integers: {5, 2, 8, 12, 3}.
The algorithm selects a pivot element, which can be chosen in various ways (e.g., the last element in the array).
o In the first iteration, the pivot is selected as 3.
o The algorithm rearranges the elements such that all elements smaller than 3 come before it, and all elements
greater than 3 come after it. The array becomes: {2, 3, 8, 12, 5}.
o The subarray before the pivot (2 and 3) is already sorted.
o The subarray after the pivot (8, 12, and 5) is sorted recursively using the same process.
o The pivot is chosen as 5 in the second iteration.
o After rearrangement, the array becomes: {2, 3, 5, 12, 8}.
o The algorithm recursively sorts the subarrays until the entire array is sorted.
Algorithm:
Step1: Choose the highest index value has pivot
Step2: Take two variables to point left and right of the list excluding pivot
Step3: Left points to the low index
Step4: Right points to the high
Step5: While value at left is less than pivot move right
Step6: While value at right is greater than pivot move left
Step7: If both step 5 and step 6 does not match swap left and right
Step8: If left ≥ right, the point where they met is new pivot
Program
#include <stdio.h>
void swap(int *a, int *b) { OUTPUT
int temp = *a;
Unsorted array: 64 25 12 22 11
*a = *b;
Sorted array: 11 12 22 25 64
*b = temp;
}
int partition(int arr[], int low, int high) {
int pivot = arr[high]; // Choose the last element as pivot
int i = low - 1; // Index of the smaller element
SEARCHING
Searching is the process of finding some particular element in the list. If the element is present in the list, then the
process is called successful, and the process returns the location of that element; otherwise, the search is called
unsuccessful.
There are different searching techniques.
Linear Search /Sequential Search
Binary Search
Hash search
Linear Search
Linear search is also called as sequential search algorithm. It is the simplest searching algorithm. In Linear search, we
simply traverse the list completely and match each element of the list with the item whose location is to be found. If the
match is found, then the location of the item is returned; otherwise, the algorithm returns NULL.
It is widely used to search an element from the unordered list, i.e., the list in which items are not sorted. The worst-case
time complexity of linear search is O(n).
Algorithm
Step 1 − Start from the 0th index of the input array, compare the key value with the value present in the 0th index.
Step 2 − If the value matches with the key, return the position at which the value was found.
Step 3 − If the value does not match with the key, compare the next element in the array.
Step 4 − Repeat Step 3 until there is a match found. Return the position at which the match was found.
Step 5 − If it is an unsuccessful search, print that the element is not present in the array and exit the program
#include <stdio.h>
int main() {
int arr[] = {10, 20, 30, 40, 50}; // Sample array
int target = 30; // Element to search for
int i;
// Linear search
for (i = 0; i < 5; i++) {
if (arr[i] == target) {
printf("Element %d found at index %d.\n", target, i); OUTPUT
return 0; // Exit once found
Element 30 found at index 2.
}
}
Binary Search
Binary search is the search technique that works efficiently on sorted lists. Hence, to search an element into some list
using the binary search technique, we must ensure that the list is sorted.
Binary search follows the divide and conquer approach in which the list is divided into two halves, and the item is
compared with the middle element of the list. If the match is found then, the location of the middle element is returned.
Otherwise, we search into either of the halves depending upon the result produced through the match.
Working
1: Start
2: Find the position of middle element of array
3: Compare the element of middle position with the search element
Repeat the following steps based on condition:
If search element is same as the middle element, then search is successful and return its position.
Else if the search element is less than the element in middle position, then continue the search to the left
portion leaving the right portion
Else if the search element is greater than the element in middle position, then continue the search to the right
portion leaving the left portion
4: Stop
Algorithm
Step 1: Set low=0, high=n-1
Step 2: while(low<=high)
mid=(low+high)/2;
if(key==A[mid])
return (mid); //element found
else if(key<A[mid])
high=mid-1;
else
low=mid+1;
end while
Step 3: return -1 //element not found
Program
OUTPUT
Binary search is an efficient algorithm for finding a target value within a sorted array. It works by repeatedly
dividing the search interval in half.
Binary search can be used as a building block for more complex algorithms used in machine learning, such as
algorithms for training neural networks or finding the optimal hyper parameters for a model.
It can be used for searching in computer graphics such as algorithms for ray tracing or texture mapping.
It can be used for searching a database.
Finds the key present at first position in constant time Finds the key present at center position in constant
time
Sequence of elements in the container does not The elements must be sorted in the container
affect.
Algorithm is easy to implement, and requires less Algorithm is slightly complex. It takes more amount of
amount of code. code to implement.
Hash Search
Working
Collision
Since the hashing procedure yields a tiny number for a large key, it is possible for two keys to provide the same result.
When a freshly inserted key corresponds to an existing occupied slot, a collision handling technique must be used to
address the problem.
How should collisions be handled?
There are primarily ways to deal with collision:
o Separate Chaining
Separate Chaining
The goal is to create a linked list of records with the same hash function value that each cell of the hash table may point
to. Although chaining is straightforward, extra memory outside the table is needed.
Example:
Hash function = key % 5,
Elements = 12, 15, 22, 25 and 37.
PATTERN SEARCHING AND TEXT PROCESSING
The capability to change the text alignment of a block of text either Left justified or Right justified is known as text line
adjustment.
Working
1. Iterate through the text completely
2. Check for line width
3. If there is no span
Then move to next line
Else
Backtrack the text until “ “
Then move to next line
4. Print the resultant text
Keyword searching in text
Counting the number of times a particular word occurs in a given text is called keyword searching.
Ex: GALS BANGALORE IS BEAUTIFUL CITY
Key : “GAL” Occurrence = 2
Pattern Searching
Pattern searching is an algorithm that involves searching for patterns such as strings, words, etc. It is used to find a
pattern/ substring from another complex string. The main goal is to reduce the time complexity, since traditional
approach takes lot of time for pattern searching.
Pattern searching algorithms should recognize familiar patterns quickly and accurately.
Recognize and classify unfamiliar patterns.
Identify patterns even when partly hidden.
Recognize patterns quickly with ease, and with automaticity.
It is a technique where it searches for a particular pattern / substring in a line Text : I love programming
of text and replaces by another pattern if the search is found. Pattern: love
All the occurrences of given pattern should be searched in a given text and Replace string : like
replaced by another pattern. The two phases involved are:
New string: I like programming
Searching for the pattern to be replaced in the text
Replace pattern with new string.