Programming Exercises - April 10, 2026
Quick Jump
| Program | File | Link |
|---|---|---|
| Exercise 1 : Binary Search With Iteration | BinarySearchIteration.cpp | Open on GitHub |
| Exercise 2 : Binary Search With Reccursion | BinarySearchReccursion.cpp | Open on GitHub |
| Exercise 3 : Binary Search With Unsorted Array | BinarySearchSort.cpp | Open on GitHub |
Exercise 1 : Binary Search With Iteration
Algorithm
- Start with
left = 0andright = size - 1 - While
left <= right:- Calculate
mid = left + (right - left) / 2 - If
arr[mid] == target→ Returnmid(Element Found) - If
arr[mid] < target→ Setleft = mid + 1(Search Right Half) - Else → Set
right = mid - 1(Search Left Half)
- Calculate
- Return
-1(Element Not Found)
Code
cpp
#include <stdio.h>
int binarySearch(int arr[], int size, int target) {
int left = 0;
int right = size - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
int main() {
// Array Implementation ~ Array Must Be Sorted
int arr[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 7;
// Calling Binary Search With Array, Size, And Target
int result = binarySearch(arr, size, target);
if (result != -1) {
printf("Element Found At Index %d\n", result);
} else {
printf("Element Not Found\n");
}
return 0;
}Compile command
bash
g++ BinarySearchIteration.cpp -o BinarySearchIteration && ./BinarySearchIterationOutput
bash
Element Found At Index 3Exercise 2 : Binary Search With Reccursion
Algorithm
- Base Case : If
size == 0, return-1(Element Not Found) - Calculate
mid = size / 2 - If
arr[mid] == target→ Returnmid(Element Found) - If
arr[mid] > target→ Recursively search left half:binarySearch(arr, mid, target) - Else → Recursively search right half:
binarySearch(arr + mid + 1, size - mid - 1, target)
Code
cpp
#include <stdio.h>
int binarySearch(int arr[], int size, int target) {
if (size == 0) {
return -1;
}
int mid = size / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] > target) {
return binarySearch(arr, mid, target);
} else {
return binarySearch(arr + mid + 1, size - mid - 1, target);
}
}
int main() {
// Array Implementation ~ Array Must Be Sorted
int arr[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
int size = sizeof(arr) / sizeof(arr[0]);
int target = 7;
// Calling Binary Search With Array, Size, And Target
int result = binarySearch(arr, size, target);
if (result != -1) {
printf("Element Found At Index %d\n", result);
} else {
printf("Element Not Found\n");
}
return 0;
}Compile command
bash
g++ BinarySearchReccursion.cpp -o BinarySearchReccursion && ./BinarySearchReccursionOutput
bash
Element Found At Index 0Exercise 3 : Binary Search With Unsorted Array
Algorithm
- Take user input for the array size and elements
- Print the unsorted array
- Sort the array using Bubble Sort :
- Outer loop runs
size - 1passes - Inner loop compares adjacent elements and swaps if out of order
- Repeat until the array is fully sorted
- Outer loop runs
- Print the sorted array
- Take user input for the target element
- Apply recursive Binary Search on the sorted array
- Print the result (Element Found at Index / Element Not Found)
Code
cpp
#include <stdio.h>
int bubbleSort(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
return 0;
}
int binarySearch(int arr[], int size, int target) {
if (size == 0) {
return -1;
}
int mid = size / 2;
if (arr[mid] == target) {
return mid;
} else if (arr[mid] > target) {
return binarySearch(arr, mid, target);
} else {
return binarySearch(arr + mid + 1, size - mid - 1, target);
}
}
int main() {
int size;
// Array Size Input
printf("Enter The Number Of Elements : ");
scanf("%d", &size);
int arr[size];
// Taking User Input In Array
for (int i = 0; i < size; i++) {
printf("Enter Element %d: ", i + 1);
scanf("%d", &arr[i]);
}
// Printing Unsorted Array
printf("Unsorted Array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// Sorting Array
bubbleSort(arr, size);
// Printing Sorted Array
printf("Sorted Array: ");
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// Binary Search
int target;
printf("Enter The Element To Search: ");
scanf("%d", &target);
int result = binarySearch(arr, size, target);
if (result == -1) {
printf("Element Not Found\n");
} else {
printf("Element Found At Index %d\n", result);
}
return 0;
}Compile command
bash
g++ BinarySearchSort.cpp -o BinarySearchSort && ./BinarySearchSortOutput
bash
Enter The Number Of Elements : 5
Enter Element 1: 9
Enter Element 2: 3
Enter Element 3: 7
Enter Element 4: 1
Enter Element 5: 5
Unsorted Array: 9 3 7 1 5
Sorted Array: 1 3 5 7 9
Enter The Element To Search: 7
Element Found At Index 3Compile All Files
bash
g++ BinarySearchIteration.cpp -o BinarySearchIteration && ./BinarySearchIteration
g++ BinarySearchReccursion.cpp -o BinarySearchReccursion && ./BinarySearchReccursion
g++ BinarySearchSort.cpp -o BinarySearchSort && ./BinarySearchSortTurbo C++ compatibility notes
c
// Change function signature
int main() -> void main()
// Change the final line
return 0; -> getch();