0% found this document useful (0 votes)
32 views4 pages

Pattern Matching, Insertion Sort, Radix Sort

The document contains code snippets demonstrating three sorting algorithms: pattern matching, insertion sort, and radix sort. The pattern matching code searches for a pattern string within a text string. The insertion sort code iterates through an integer array and inserts each element into its sorted position. The radix sort code uses counting sort to sort an integer array based on the place value of its digits.

Uploaded by

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

Pattern Matching, Insertion Sort, Radix Sort

The document contains code snippets demonstrating three sorting algorithms: pattern matching, insertion sort, and radix sort. The pattern matching code searches for a pattern string within a text string. The insertion sort code iterates through an integer array and inserts each element into its sorted position. The radix sort code uses counting sort to sort an integer array based on the place value of its digits.

Uploaded by

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

Pattern Matching

#include <stdio.h>

#include <string.h>

int main()

char text[20],pat[20];

int a,b;

printf("Enter the string : ");

scanf("%s",&text);

printf("Enter the pattern to find : ");

scanf("%s",&pat);

a = strlen(pat);

b = strlen(text);

for (int i = 0; i <= b - a; i++) {

int j;

for (j = 0; j < a; j++)

if (text[i + j] != pat[j])

break;

if (j == a)

printf("Pattern found at position %d \n", i+1);

return 0;

Insertion Sort:
#include<stdio.h>

int main(){

/* Here i & j for loop counters, temp for swapping,

* count for total number of elements, number[] to


* store the input numbers in array. You can increase

* or decrease the size of number array as per requirement

*/

int i, j, count, temp, number[25];

printf("How many numbers u are going to enter?: ");

scanf("%d",&count);

printf("Enter %d elements: ", count);

// This loop would store the input numbers in array

for(i=0;i<count;i++)

scanf("%d",&number[i]);

// Implementation of insertion sort algorithm

for(i=1;i<count;i++){

temp=number[i];

j=i-1;

while((temp<number[j])&&(j>=0)){

number[j+1]=number[j];

j=j-1;

number[j+1]=temp;

printf("Order of Sorted elements: ");

for(i=0;i<count;i++)

printf(" %d",number[i]);

return 0;

}
Radix sort:
#include <stdio.h>

// Function to get the largest element from an array

int getMax(int array[], int n) {

int max = array[0];

for (int i = 1; i < n; i++)

if (array[i] > max)

max = array[i];

return max;

// Using counting sort to sort the elements in the basis of significant places

void countingSort(int array[], int size, int place) {

int output[size + 1];

int max = (array[0] / place) % 10;

for (int i = 1; i < size; i++) {

if (((array[i] / place) % 10) > max)

max = array[i];

int count[max + 1];

for (int i = 0; i < max; ++i)

count[i] = 0;

// Calculate count of elements

for (int i = 0; i < size; i++)

count[(array[i] / place) % 10]++;

// Calculate cumulative count

for (int i = 1; i < 10; i++)

count[i] += count[i - 1];

// Place the elements in sorted order

for (int i = size - 1; i >= 0; i--) {

output[count[(array[i] / place) % 10] - 1] = array[i];


count[(array[i] / place) % 10]--;

for (int i = 0; i < size; i++)

array[i] = output[i];

// Main function to implement radix sort

void radixsort(int array[], int size) {

// Get maximum element

int max = getMax(array, size);

// Apply counting sort to sort elements based on place value.

for (int place = 1; max / place > 0; place *= 10)

countingSort(array, size, place);

// Print an array

void printArray(int array[], int size) {

for (int i = 0; i < size; ++i) {

printf("%d ", array[i]);

printf("\n");

// Driver code

int main() {

int array[] = {121, 432, 564, 23, 1, 45, 788};

int n = sizeof(array) / sizeof(array[0]);

radixsort(array, n);

printArray(array, n);

You might also like