0% found this document useful (0 votes)
14 views3 pages

Sort

The document defines functions for linear search, binary search, insertion sort, bubble sort, and selection sort of arrays. It tests each sorting and search algorithm on an input array and prints the results. Main prompts the user to enter array size and elements, performs linear and binary search for a key, and sorts the array using insertion, bubble, and selection sort while printing the sorted arrays.

Uploaded by

Nitish Debbarma
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)
14 views3 pages

Sort

The document defines functions for linear search, binary search, insertion sort, bubble sort, and selection sort of arrays. It tests each sorting and search algorithm on an input array and prints the results. Main prompts the user to enter array size and elements, performs linear and binary search for a key, and sorts the array using insertion, bubble, and selection sort while printing the sorted arrays.

Uploaded by

Nitish Debbarma
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/ 3

//NITISH KR.

DEBBARMA 22UCS054
#include <stdio.h>

int linearSearch(int arr[], int N, int key) {


for (int i = 0; i < N; i++) {
if (arr[i] == key) {
return i;
}
}
return -1;
}
int binarySearch(int arr[], int N, int key) {
int left = 0, right = N - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == key) {
return mid;
} else if (arr[mid] < key) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}

void insertionSort(int arr[], int N) {


for (int i = 1; i < N; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}

void bubbleSort(int arr[], int N) {


for (int i = 0; i < N - 1; i++) {
for (int j = 0; j < N - i - 1; j++) {
if (arr[j] > arr[j + 1])
{

int temp = arr[j];


arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

void selectionSort(int arr[], int N) {


for (int i = 0; i < N - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < N; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}

int temp = arr[i];


arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}

int main()
{
int N,ch;
printf("Enter the number of elements in the array: ");
scanf("%d", &N);

int arr[N];
printf("Enter %d integers for the array:\n", N);
for (int i = 0; i < N; i++) {
scanf("%d", &arr[i]);
}

int key;
printf("Enter the element to search: ");
scanf("%d", &key);

int linearResult = linearSearch(arr, N, key);


if (linearResult != -1) {
printf("Linear Search: Element found at index %d\n", linearResult);
} else {
printf("Linear Search: Element not found\n");
}

insertionSort(arr, N);

int binaryResult = binarySearch(arr, N, key);


if (binaryResult != -1) {
printf("Binary Search: Element found at index %d\n", binaryResult);
} else {
printf("Binary Search: Element not found\n");
}

int sortedArr[N];
for (int i = 0; i < N; i++) {
sortedArr[i] = arr[i];
}

// Insertion Sort
insertionSort(sortedArr, N);
printf("Insertion Sort: Sorted array:\n");
for (int i = 0; i < N; i++) {
printf("%d ", sortedArr[i]);
}
printf("\n");

// Bubble Sort
for (int i = 0; i < N; i++) {
sortedArr[i] = arr[i];
}
bubbleSort(sortedArr, N);
printf("Bubble Sort: Sorted array:\n");
for (int i = 0; i < N; i++) {
printf("%d ", sortedArr[i]);
}
printf("\n");

// Selection Sort
for (int i = 0; i < N; i++) {
sortedArr[i] = arr[i];
}
selectionSort(sortedArr, N);
printf("Selection Sort: Sorted array:\n");
for (int i = 0; i < N; i++) {
printf("%d ", sortedArr[i]);
}
printf("\n");

return 0;
}

You might also like