pps module 5 assignment
pps module 5 assignment
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>
if (arr[mid] == target) {
return mid;
}
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;
if (result == -1) {
printf("Element not found in the array");
} else {
printf("Element found at index %d", result);
}
return 0;
}
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>
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;
}
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>
arr[j + 1] = key;
}
}
int main() {
int arr[] = {12, 11, 13, 5, 6};
int n = sizeof(arr) / sizeof(arr[0]);
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>
int i = 0, j = 0, k = left;
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
mergeSort(arr, 0, n - 1);
return 0;
}
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>
if (minIndex != i) {
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
}
int main() {
int arr[] = {64, 34, 25, 12, 22, 11, 90};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
return 0;
}