0% found this document useful (0 votes)
13 views28 pages

Searching and Sorting

Uploaded by

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

Searching and Sorting

Uploaded by

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

Searching and Sorting

C.SAGANA
AP(SrG/CSE)
Difference Between Linear and Binary Search: Linear vs Binary Search
Linear Search
• Also known as Sequential Search used for finding a particular
element within a list
• It sequentially checks each element in the list until a match is found
• It is used for smaller datasets and the data set is unsorted.
linear search algorithm
• Given list: [2, 4, 6, 8, 10]. To find the number 6
1.Start from the first element (2).
2.Compare the current element with the target element (6).
3.If they match, the search ends.
4.If they do not match, move to the next element.
5.Repeat steps 2-4 until the target element is found or the list ends.
#include<stdio.h>
int linearSearch(int arr[], int size, int element){
for (int i = 0; i < size; i++)
{
if(arr[i]==element){
return i;
}
}
return -1;
}
int main(){
// Unsorted array for linear search
int arr[] = {1,3,5,56,4,3,23,5,4,54634,56,34};
int size = sizeof(arr)/sizeof(int);
printf("enter the element to be search");
scanf("%d",&element);
int linear = linearSearch(arr, size, element);
printf("The element %d was found at index %d \n", element, linear);
return 0;
}
Advantage and Disadvantage of Linear
Search

Simplicity: Linear Search is straightforward and easy to implement, making it a good


Advantage
choice for small datasets and simple search tasks.

Efficiency: Linear Search can be inefficient for large datasets as it checks each element
Disadvantage
sequentially, leading to a higher time complexity (O(n)) in worst-case scenarios.
Binary Search
• Efficient searching algorithm that finds the position of a target value
within a sorted array.
• It compares the target value to the middle element of the array and
eliminates half of the search space successively.
• It is efficient for large datasets and requires sorted data.
• uses the divide-and-conquer approach
#include<stdio.h> int main(){
int binarySearch(int arr[], int size, int element){
int low, mid, high;
int arr[] =
low = 0; {1,3,5,56,64,73,123,225,444};
high = size-1; int size = sizeof(arr)/sizeof(int);
// Keep searching until low <= high
while(low<=high){ int element;
mid = (low + high)/2; printf("enter the element to be
if(arr[mid] == element){ search");
return mid;
} scanf("%d",&element);
if(arr[mid]<element){ int searchIndex = binarySearch(arr,
low = mid+1;
size, element);
}
else{ printf("The element %d was found at
high = mid -1; index %d \n", element, searchIndex);
}
}
return 0;
return -1; }
}
Advantages and Disadvantages of Binary Search

Efficiency: Binary Search is highly efficient for large datasets. It significantly


Advantage reduces the search space by half with each comparison, leading to a time
complexity of O(log n).

Sorted Data Requirement: Binary Search requires the dataset to be sorted


Disadvantage beforehand. This prerequisite can be a limitation if dealing with unsorted or
dynamically changing datasets.
#include<stdio.h> int main(){
int linearSearch(int arr[], int size, int element){ // Unsorted array for linear search
for (int i = 0; i < size; i++) // int arr[] = {1,3,5,56,4,3,23,5,4,54634,56,34};
{ // int size = sizeof(arr)/sizeof(int);
if(arr[i]==element){ // Sorted array for binary search
return i; int arr[] = {1,3,5,56,64,73,123,225,444};
} int size = sizeof(arr)/sizeof(int);
} int ele,element;
return -1; printf("enter the element to be search");
} scanf("%d",&ele);
int binarySearch(int arr[], int size, int element){ int linear = linearSearch(arr, size, ele);
int low, mid, high; printf("The element %d was found at index %d \n", ele,
linear);
low = 0;
printf("enter the element to be search");
high = size-1;
scanf("%d",&element);
// Keep searching until low <= high
int searchIndex = binarySearch(arr, size, element);
while(low<=high){
printf("The element %d was found at index %d \n",
mid = (low + high)/2; element, searchIndex);
if(arr[mid] == element){ return 0;
return mid; }
Bubble sort
• compares two adjacent elements and swaps them until they are in the
intended order
• each element of the array move to the end in each iteration
1. First Iteration (Compare and Swap)
Remaining Iteration
#include<stdio.h> a[j] = a[j+1];
void print(int a[], int n) a[j+1] = temp;
{ }
int i; }
for(i = 0; i < n; i++) }
{ }
printf("%d ",a[i]); void main ()
} {
} int i, j,temp;
void bubble(int a[], int n) int a[5] = { 10, 35, 32, 13, 26};
{ int n = sizeof(a)/sizeof(a[0]);
int i, j, temp; printf("Before sorting array elements are - \n");
for(i = 0; i < n; i++) print(a, n);
{ bubble(a, n);
for(j = 0; j < n; j++) printf("\nAfter sorting array elements are - \n");
{ print(a, n);
if(a[j] < a[j+1]) }
{
temp = a[j];
Shell sort

• extended version of insertion sort


• efficient for medium-sized data sets
• It is a comparison-based and in-place sorting algorithm.
• In insertion sort, elements can be moved ahead by one position only.
• To move an element to a far-away position, many movements are
required that increase the algorithm's execution time.
• Shell sort overcomes this drawback of insertion sort.
• It allows the movement and swapping of far-away elements as well.
• sorts the elements that are far away from each other, then it
subsequently reduces the gap between them.
• shell's original sequence (N/2, N/4, ...1) as intervals
• original sequence of shell sort, i.e., N/2, N/4,....,1 as the intervals.
• In the first loop, n is equal to 8 (size of the array), so the elements are
lying at the interval of 4 (n/2 = 4). Elements will be compared and
swapped if they are not in order.
• Here, in the first loop, the element at the 0 th position will be compared
with the element at 4th position. If the 0th element is greater, it will be
swapped with the element at 4th position. Otherwise, it remains the
same. This process will continue for the remaining elements.
• At the interval of 4, the sublists are {33, 12}, {31, 17}, {40, 25}, {8,
42}.

• After comparing and swapping, the updated array


• In the second loop, elements are lying at the interval of 2 (n/4 = 2),
where n = 8.

• After comparing and swapping, the updated array will be


• In the third loop, elements are lying at the interval of 1 (n/8 = 1),
where n = 8
• First Pass:
• Current element is 23

Insertion Sort • The first element in the array is assumed to be sorted.


• The sorted part until 0th index is : [23]

Algorithm: • Second Pass:


• Compare 1 with 23 (current element with the sorted part).
• Since 1 is smaller, insert 1 before 23.
• The sorted part until 1st index is: [1, 23]
• Third Pass:
• Compare 10 with 1 and 23 (current element with the sorted part).
• Since 10 is greater than 1 and smaller than 23, insert 10 between 1 and 23.
• The sorted part until 2nd index is: [1, 10, 23]
• Fourth Pass:
• Compare 5 with 1, 10, and 23 (current element with the sorted part).
• Since 5 is greater than 1 and smaller than 10, insert 5 between 1 and 10.
• The sorted part until 3rd index is: [1, 5, 10, 23]
• Fifth Pass:
• Compare 2 with 1, 5, 10, and 23 (current element with the sorted part).
• Since 2 is greater than 1 and smaller than 5 insert 2 between 1 and 5.
• The sorted part until 4th index is: [1, 2, 5, 10, 23]
• Final Array:
#include <stdio.h> }
void shellSort(int arr[], int n){ int main(){
int gap, j, k; int n;
for(gap = n/2; gap > 0; gap = gap / 2) { n = 5;
for(j = gap; j<n; j++) { int arr[5] = {33, 45, 62, 12, 98}; // initialize the
array
for(k = j-gap; k>=0; k -= gap) {
printf("Array before Sorting: ");
if(arr[k+gap] >= arr[k])
for(int i = 0; i<n; i++)
break;
printf("%d ",arr[i]);
else {
printf("\n");
int temp;
shellSort(arr, n);
temp = arr[k+gap];
printf("Array after Sorting: ");
arr[k+gap] = arr[k];
for(int i = 0; i<n; i++)
arr[k] = temp;
printf("%d ", arr[i]);
}
printf("\n");
}
Bucket sort
• sorting technique that involves dividing elements into various groups,
or buckets.
• Let the elements of array are

• create buckets with a range from 0 to 25.


• The buckets range are 0-5, 5-10, 10-15, 15-20, 20-25.
• Elements are inserted in the buckets according to the bucket range
• The value of an item is 16, so it will be inserted in the bucket with the
range 15-20
• sort each bucket individually.
• The elements of each bucket can be sorted by using any of the stable
sorting algorithms.

• gather the sorted elements from each bucket in order


• Input array

• Make a one-dimensional array of size ten and fill it with zeros at first.
• Each array slot serves as a bucket for storing elements.

• Insert array elements into the buckets.


• input element .34 is chosen
• It is multiplied by size = 10 (.34*10=3.4).
• Converted into an integer.
• Finally,.34 is inserted into bucket 3.
• Each bucket's elements are sorted using one of the stable sorting
algorithms.

• Each bucket's elements are gathered.


#include <stdio.h> {
int getMax(int a[], int n) // function to get maximum element fr while (bucket[i] > 0)
om the given array {
{ a[j++] = i;
int max = a[0]; bucket[i]--;
for (int i = 1; i < n; i++) }
if (a[i] > max) }
max = a[i]; }
return max; void printArr(int a[], int n) // function to print array elements
} {
void bucket(int a[], int n) // function to implement bucket sort for (int i = 0; i < n; ++i)
{ printf("%d ", a[i]);
int max = getMax(a, n); // }
max is the maximum element of array
int main()
int bucket[max], i;
{
for (int i = 0; i <= max; i++)
int a[] = {54, 12, 84, 57, 69, 41, 9, 5};
{
int n = sizeof(a) / sizeof(a[0]); // n is the size of array
bucket[i] = 0;
printf("Before sorting array elements are - \n");
}
printArr(a, n);
for (int i = 0; i < n; i++)
bucket(a, n);
{
printf("\nAfter sorting array elements are - \n");
bucket[a[i]]++;
printArr(a, n);
}
}
for (int i = 0, j = 0; i <= max; i++)

You might also like