0% found this document useful (0 votes)
2 views

Program 14+

The document contains multiple C programs that implement various sorting algorithms including Insertion Sort, Selection Sort, Merge Sort, Quick Sort, and Bubble Sort, as well as Linear and Binary Search algorithms. Each program prompts the user for input, performs the respective sorting or searching operation, and outputs the result. The programs are structured with main functions and respective sorting or searching functions, demonstrating basic algorithm implementations.

Uploaded by

roboticsir07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Program 14+

The document contains multiple C programs that implement various sorting algorithms including Insertion Sort, Selection Sort, Merge Sort, Quick Sort, and Bubble Sort, as well as Linear and Binary Search algorithms. Each program prompts the user for input, performs the respective sorting or searching operation, and outputs the result. The programs are structured with main functions and respective sorting or searching functions, demonstrating basic algorithm implementations.

Uploaded by

roboticsir07
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Aim: Write a Program to perform sorting on a

given array using Insertion Sort


Solution
#include <stdio.h>

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;
}
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d",&n);
int arr[n];
printf("Enter the elements:\n");
for(int i=0;i<n;i++) {
scanf("%d",&arr[i]);
}
insertionSort(arr, n);
printf("Sorted array: ");
for(int i=0;i<n;i++) {
printf("%d\t",arr[i]);
}
return 0;
}

Output
Aim: Write a Program to perform sorting on a
given array using Selection Sort
Solution
#include <stdio.h>

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;
}
}
if (minIndex != i) {
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d",&n);
int arr[n];
printf("Enter the elements:\n");
for(int i=0;i<n;i++) {
scanf("%d",&arr[i]);
}
selectionSort(arr, n);
printf("Sorted array: ");
for(int i=0;i<n;i++) {
printf("%d\t",arr[i]);
}
return 0;
}

Output
Aim: Write a Program to perform sorting on a
given array using Merge Sort
Solution
#include <stdio.h>

void merge(int arr[], int left, int mid, int right) {


int n1 = mid - left + 1;
int n2 = right - mid;
int L[n1], R[n2];

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


L[i] = arr[left + i];
for (int i = 0; i < n2; i++)
R[i] = arr[mid + 1 + i];

int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}

while (i < n1) {


arr[k] = L[i];
i++;
k++;
}

while (j < n2) {


arr[k] = R[j];
j++;
k++;
}
}

void mergeSort(int arr[], int left, int right) {


if (left < right) {
int mid = left + (right - left) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d",&n);
int arr[n];
printf("Enter the elements:\n");
for(int i=0;i<n;i++) {
scanf("%d",&arr[i]);
}
mergeSort(arr,0,n-1);
printf("Sorted array: ");
for(int i=0;i<n;i++) {
printf("%d\t",arr[i]);
}
return 0;
}

Output
Aim: Write a Program to perform sorting on a
given array using Quick Sort
Solution
#include <stdio.h>

int partition(int arr[], int low, int high) {


int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[high];
arr[high] = temp;
return i + 1;
}

void quickSort(int arr[], int low, int high) {


if (low < high) {
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}
int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d",&n);
int arr[n];
printf("Enter the elements:\n");
for(int i=0;i<n;i++) {
scanf("%d",&arr[i]);
}
quickSort(arr,0,n-1);
printf("Sorted array: ");
for(int i=0;i<n;i++) {
printf("%d\t",arr[i]);
}
return 0;
}
Output
Aim: Write a Program to perform Linear Search on
a Array
Solution
#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 main() {
int n, key;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the element to search: ");
scanf("%d", &key);
int result = linearSearch(arr, n, key);
if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found\n");
}
return 0;
}

Output
Aim: Write a Program to perform Binary Search
on a Array
Solution
#include <stdio.h>

int binarySearch(int arr[], int n, int key) {


int low = 0, high = n - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (arr[mid] == key) {
return mid;
} else if (arr[mid] < key) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return -1;
}

int main() {
int n, key;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
printf("Enter the element to search: ");
scanf("%d", &key);
int result = binarySearch(arr, n, key);
if (result != -1) {
printf("Element found at index %d\n", result);
} else {
printf("Element not found\n");
}
return 0;
}

Output
Aim: Write a Program to perform sorting on a
given array using Bubble Sort
Solution
#include <stdio.h>

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

int main() {
int n;
printf("Enter the number of elements: ");
scanf("%d", &n);
int arr[n];
printf("Enter the elements:\n");
for (int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
}
bubbleSort(arr, n);
printf("Sorted array: ");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}

Output

You might also like