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

pps module 5 assignment

Uploaded by

hemanshausali8
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)
18 views

pps module 5 assignment

Uploaded by

hemanshausali8
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/ 7

1.

Binary Search Algorithm


Binary search is an efficient algorithm for finding an item from a sorted list of items. It
works by repeatedly dividing in half the portion of the list that could contain the item, until
you've narrowed the possible locations to just one.

Working Procedure:
1. Start with a sorted list of items.
2. Find the middle point of the list.
3. Compare the middle element with the target element.
4. If the target element is equal to the middle element, return the index of the middle
element.
5. If the target element is less than the middle element, repeat the process with the left half
of the list.
6. If the target element is greater than the middle element, repeat the process with the right
half of the list.

Implementation in C:

#include <stdio.h>

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


int left = 0;
int right = n - 1;

while (left <= right) {


int mid = left + (right - left) / 2;

if (arr[mid] == target) {
return mid;
}

if (arr[mid] < target) {


left = mid + 1;
} else {
right = mid - 1;
}
}

return -1;
}
int main() {
int arr[] = {2, 5, 8, 12, 16, 23, 38, 56, 72, 91};
int n = sizeof(arr) / sizeof(arr[0]);
int target = 23;

int result = binarySearch(arr, n, target);

if (result == -1) {
printf("Element not found in the array");
} else {
printf("Element found at index %d", result);
}

return 0;
}

2. Bubble Sort Algorithm


Bubble sort is a simple sorting algorithm that repeatedly steps through the list, compares
adjacent elements, and swaps them if they are in the wrong order.

Working Procedure:
1. Start at the beginning of the list.
2. Compare the first two elements.
3. If the first element is greater than the second element, swap them.
4. Move to the next pair of elements and repeat steps 2-3.
5. Continue this process until the end of the list is reached.
6. Repeat the process until no more swaps are needed.

Implementation in C:

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

void printArray(int arr[], int n) {


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);

bubbleSort(arr, n);
printf("Sorted array: \n");
printArray(arr, n);

return 0;
}

3. Insertion Sort Algorithm


Insertion sort is a simple sorting algorithm that works by dividing the input into a sorted
and an unsorted region.

Working Procedure:
1. Start with the first element as the sorted region.
2. Take the next element from the unsorted region and insert it into the sorted region.
3. Shift the elements in the sorted region to make space for the new element.
4. Repeat steps 2-3 until the entire list is sorted.

Implementation in C:

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

void printArray(int arr[], int n) {


for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);

4. Merge Sort Algorithm


Merge sort is a divide-and-conquer algorithm that splits the input into two halves,
recursively sorts them, and then merges them.

Working Procedure:
1. Divide the input into two halves.
2. Recursively sort the two halves.
3. Merge the two sorted halves into a single sorted list.

Implementation in C:

#include <stdio.h>

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


int n1 = mid - left + 1;
int n2 = right - mid;

int leftArr[n1], rightArr[n2];

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


leftArr[i] = arr[left + i];
for (int j = 0; j < n2; j++)
rightArr[j] = arr[mid + 1 + j];

int i = 0, j = 0, k = left;

while (i < n1 && j < n2) {


if (leftArr[i] <= rightArr[j]) {
arr[k] = leftArr[i];
i++;
} else {
arr[k] = rightArr[j];
j++;
}
k++;
}

while (i < n1) {


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

while (j < n2) {


arr[k] = rightArr[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);


}
}

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


for (int i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}

int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);

mergeSort(arr, 0, n - 1);

printf("Sorted array: \n");


printArray(arr, n);

return 0;
}

5. Selection Sort Algorithm


Selection sort is a simple sorting algorithm that works by repeatedly finding the minimum
element from the unsorted part and putting it at the beginning.

Working Procedure:
1. Start with the first element as the minimum.
2. Compare the minimum with the next element.
3. If the next element is smaller, update the minimum.
4. Repeat steps 2-3 until the end of the list is reached.
5. Swap the minimum with the first element.
6. Repeat the process for the remaining elements.

Implementation in C:

#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[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
}

void printArray(int arr[], int n) {


for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
printf("\n");
}

int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);

selectionSort(arr, n);

printf("Sorted array: \n");


printArray(arr, n);

return 0;
}

You might also like