0% found this document useful (0 votes)
10 views14 pages

PST Unit4

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

PST Unit4

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

UNIT – 4: SORTING AND SEARCHING

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.

Selection sort /Sort by selection


Selection sort is a simple comparison-based sorting algorithm that repeatedly selects the minimum element from the
unsorted portion of an array and moves it to the sorted portion until n-1 position. Here's a step-by-step explanation of
how selection sort works, along with an example:
Algorithm:
1. Find the minimum element in the unsorted portion of the array.
2. Swap the minimum element with the first element of the unsorted portion, effectively moving it to the sorted portion.
3. Repeat steps 1 and 2 for the remaining unsorted portion until the entire array is sorted.

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

for (int j = low; j < high; j++) {


if (arr[j] < pivot) {
i++;
swap(&arr[i], &arr[j]); // Swap if element is smaller than pivot
}
}
swap(&arr[i + 1], &arr[high]); // Swap the pivot element with the element at i+1
return (i + 1); // Return the partition index
}
void quickSort(int arr[], int low, int high) {
if (low < high) {
int pi = partition(arr, low, high); // Find the partition index
quickSort(arr, low, pi - 1); // Recursively sort the left part
quickSort(arr, pi + 1, high); // Recursively sort the right part
}
}
void printArray(int arr[], int n) {
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
printf("Unsorted array: ");
printArray(arr, n);
quickSort(arr, 0, n - 1);
printf("Sorted array: ");
printArray(arr, n);
return 0;
}

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.
}
}

printf("Element %d not found.\n", target);


return 0;
}

Advantages of Linear Search Algorithm:


 Linear search can be used irrespective of whether the array is sorted or not. It can be used on arrays of any data
type.
 Does not require any additional memory.
 It is a well-suited algorithm for small datasets.

Disadvantages of Linear Search Algorithm:


 Linear search has a time complexity of O(N), which in turn makes it slow for large datasets.
 Not suitable for large arrays.

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

#include<stdio.h> void main()


#include<conio.h> {
int bsearch(int n, int A[ ], int key) int A[30],key,i,n,flag;
{ clrscr();
int low,mid,high; printf(“Enter the limit:\n”);
low=0; scanf(“%d”,&n);
high=n-1; printf(“Enter the array elements:\n");
mid=(low+high)/2; for(i=0;i<n;i++)
while(low<=high) {
{ printf(“\nEnter the element[%d]:”,i+1);
if(key==A[mid]) scanf("%d",&A[i]);
return (mid); //element found }
else if(key<A[mid]) printf(“\nEnter element to search:");
high=mid-1; scanf("%d",&key);
else flag=bsearch(n,A,key);
low=mid+1; if(flag==-1)
mid=(low+high)/2; printf(“\n%d not found in the array”,key);
} else
return -1; //element not found printf(“\n %d found at position :%d ”,key,flag+1);
} getch();
}

OUTPUT

Element is present at index 3


Applications of binary search

 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.

Advantages of Binary Search


 Binary search is faster than linear search, especially for large arrays.
 More efficient than other searching algorithms with a similar time complexity, such as interpolation search or
exponential search.
 Binary search is well-suited for searching large datasets that are stored in external memory, such as on a hard
drive or in the cloud.

Disadvantages of Binary Search


 The array should be sorted.
 Binary search requires that the data structure being searched be stored in contiguous memory locations.
 Binary search requires that the elements of the array be comparable, meaning that they must be able to be
ordered.

Sequential Search /Linear search Binary Search

Time complexity is O(n) Time complexity is O(log n)

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 iterative in nature Algorithm technique is Divide and Conquer.

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

Hashing is the process of transforming any given key or


a string of characters into another value.this is usually
represented by a shorter, fixed-length value or key that
represents and makes it easier to find or employ the
original string. The most popular use for hashing is the
implementation of hash tables.

Working

1. Create an array of structure data


2. Take a key to be stored in hash table as input
3. Corresponding to the key , an index will be generated.
4. In case of absence of data in that index of array , create one and insert the data item
5. In case the data already exists, the new data cannot be inserted if the already present data does not match given key.
6. To display all the elements of hash table, data at each index is extracted and elements are read and printed.
7. To remove a key from hash table, we will first calculate its index and extract its data, delete the key is case it matches
the given key.

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

Text Line Adjustment

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.

Features of Pattern Searching Algorithm:

 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.

Linear pattern searching / Naïve pattern searching


Naive pattern searching is the simplest method among other
pattern-searching algorithms. It checks for all characters of the
main string to the pattern. This algorithm is helpful for smaller
texts. It does not need any pre-processing phases. We can find the
substring by checking once for the string. It also does not occupy
extra space to perform the operation
Program
#include<stdio.h>
#include<string.h> OUTPUT
int main (){
char txt[]="AABAACAADAABAABA"; Pattern matches at 0, 9, 12
char pat[]="AABA";
int M =strlen(pat);
int N =strlen(txt);
for(inti=0;i<= N - M;i++){
int j;
for(j =0; j < M;j++)
if(txt[i+ j]!= pat[j])
break;
if(j == M)
printf("Pattern matches at index %d ",i);
}
return0;
}

Text Line editing Example:

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.

You might also like