E-Notes_558_Content_Document_20240909104743AM (1)
E-Notes_558_Content_Document_20240909104743AM (1)
E-Notes_558_Content_Document_20240909104743AM (1)
LinearSearch(array, key)
for each item in the array
if item == value
return its index
#include <stdio.h>
int main() {
int array[] = {2, 4, 0, 1, 9};
int x = 1;
int n = sizeof(array) / sizeof(array[0]);
The steps used in the sorting of radix sort are listed as follows –
First, we have to find the largest element (suppose max) from the given array.
Suppose 'x' be the number of digits in max. The 'x' is calculated because we need to
go through the significant places of all elements.
After that, go through one by one each significant place. Here, we have to use any
stable sorting algorithm to sort the digits of each significant place.
#include <stdio.h>
int getMax(int a[], int n) { // Place the elements in sorted order
int max = a[0]; for (int i = n - 1; i >= 0; i--) {
for(int i = 1; i<n; i++) { output[count[(a[i] / place) % 10] -
if(a[i] > max) 1] = a[i];
max = a[i]; count[(a[i] / place) % 10]--;
} }
return max; //maximum element from the arr
ay for (int i = 0; i < n; i++)
} a[i] = output[i];
void countingSort(int a[], int n, int place) // fu }
nction to implement counting sort
{ // function to implement radix sort
int output[n + 1]; void radixsort(int a[], int n) {
int count[10] = {0};
// Calculate count of elements // get maximum element from array
for (int i = 0; i < n; i++) int max = getMax(a, n);
count[(a[i] / place) % 10]++;
// Apply counting sort to sort elements
// Calculate cumulative frequency based on place value
for (int i = 1; i < 10; i++) for (int place = 1; max / place > 0; place
count[i] += count[i - 1]; *= 10)
countingSort(a, n, place);
}
// function to print array elements
void printArray(int a[], int n) {
for (int i = 0; i < n; ++i) {
printf("%d ", a[i]);
}
printf("\n");
}
int main() {
int a[] = {181, 289, 390, 121, 145, 736, 514, 888, 122};
int n = sizeof(a) / sizeof(a[0]);
printf("Before sorting array elements are - \n");
printArray(a,n);
radixsort(a, n);
printf("After applying Radix sort, the array elements are -
\n");
printArray(a, n);
}
MERGE SORT
Merge sort is the sorting technique that follows the divide and conquer
approach. This article will be very helpful and interesting to students as they might
face merge sort as a question in their examinations. In coding or technical
interviews for software engineers, sorting algorithms are widely asked. So, it is
important to discuss the topic.
Merge sort is similar to the quick sort algorithm as it uses the divide and
conquer approach to sort the elements. It is one of the most popular and efficient
sorting algorithm. It divides the given list into two equal halves, calls itself for the
two halves and then merges the two sorted halves. We have to define
the merge() function to perform the merging.
The sub-lists are divided again and again into halves until the list cannot
be divided further. Then we combine the pair of one element lists into two-
element lists, sorting them in the process. The sorted two-element pairs is merged
into the four-element lists, and so on until we get the sorted list.
#include <stdio.h>
while (i < n1 && j < n2)
{
/* Function to merge the subarrays of a[] */
if(LeftArray[i] <= RightArray[j])
void merge(int a[], int beg, int mid, int end)
{
{
a[k] = LeftArray[i];
int i, j, k;
i++;
int n1 = mid - beg + 1;
}
int n2 = end - mid;
else
{
int LeftArray[n1], RightArray[n2]; //tempor
a[k] = RightArray[j];
ary arrays
j++;
}
/* copy data to temp arrays */
k++;
for (int i = 0; i < n1; i++)
}
LeftArray[i] = a[beg + i];
while (i<n1)
for (int j = 0; j < n2; j++)
{
RightArray[j] = a[mid + 1 + j];
a[k] = LeftArray[i];
i++;
i = 0; /* initial index of first sub-array */
k++;
j = 0; /* initial index of second sub-
}
array */
k = beg; /* initial index of merged sub-
array */
while (j<n2) /* Function to print the array */
{ void printArray(int a[], int n)
a[k] = RightArray[j]; {
j++; int i;
k++; for (i = 0; i < n; i++)
} printf("%d ", a[i]);
} printf("\n");
}
void mergeSort(int a[], int beg, int end
) int main()
{ {
if (beg < end) int a[] = { 12, 31, 25, 8, 32, 17, 40, 42 };
{ int n = sizeof(a) / sizeof(a[0]);
int mid = (beg + end) / 2; printf("Before sorting array elements are -
mergeSort(a, beg, mid); \n");
mergeSort(a, mid + 1, end); printArray(a, n);
merge(a, beg, mid, end); mergeSort(a, 0, n - 1);
} printf("After sorting array elements are - \n");
} printArray(a, n);
return 0;
}
QUICK SORT
Quick sort is a commonly used sorting algorithm that is often preferred over
other sorting algorithms due to its efficiency and effectiveness. It proceeds by
splitting an array into two parts, one with elements smaller than a selected pivot
element and the other with elements bigger than the pivot. The algorithm then
applies this procedure recursively to each partition until the complete array is
sorted.
Quick sort can be used in any scenario where sorting is required, such as in
database applications, scientific computing, and web applications. It is frequently
utilized when a large dataset must be rapidly and effectively sorted. Some specific
use cases where quick sort is commonly used include:
• Sorting arrays in programming languages such as C, Java, and Python.
• Sorting database records in database management systems.
• Sorting large datasets in scientific computing, such as in numerical simulations
and data analysis.
• Sorting search results in web applications and e-commerce platforms.
Overall, quick sort is a versatile and widely used algorithm that can be applied in a
variety of domains where sorting is required. Its low average-case time
complexity and simplicity of execution make it an appealing choice for efficiently
sorting large datasets.
#include <stdio.h> void quickSort(int arr[], int low, int high) {
if (low < high) {
// Function to swap two elements int pi = partition(arr, low, high);
void swap(int* a, int* b) { quickSort(arr, low, pi - 1);
int t = *a; quickSort(arr, pi + 1, high);
*a = *b; }
*b = t; }
} // Function to print the array
int partition(int arr[], int low, int high) { void printArray(int arr[], int size) {
int pivot = arr[high]; int i;
int i = (low - 1); for (i = 0; i < size; i++)
printf("%d ", arr[i]);
for (int j = low; j <= high - 1; j++) { printf("\n");
if (arr[j] < pivot) { }
i++;
swap(&arr[i], &arr[j]); int main() {
} int arr[] = { 12, 17, 6, 25, 1, 5 };
} int n = sizeof(arr) / sizeof(arr[0]);
swap(&arr[i + 1], &arr[high]); quickSort(arr, 0, n - 1);
return (i + 1); printf("Sorted array: \n");
} printArray(arr, n);
return 0;
}