Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 41 additions & 27 deletions C/algorithms/searching/binary_search.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,46 @@
*/

#include <stdio.h>
#define SIZE 10
int BinarySearch(int [],int);
int main()
{
int a[SIZE]={3,5,9,11,15,17,22,25,37,68},key,pos;
printf("Enter the Search Key\n");
scanf("%d",&key);
pos=BinarySearch(a,key);
if(pos==-1)
printf("The search key is not in the array\n");
else
printf("The search key %d is at location %d\n",key,pos);
return 0;
//function to perform binary search
int binarySearch(int arr[], int size, int key) {
int low = 0, high = size - 1, mid;

while (low <= high) {
mid = (low + high) / 2;

if (arr[mid] == key)
return mid;
else if (key < arr[mid])
high = mid - 1;
else
low = mid + 1;
}

int BinarySearch (int A[],int skey)
{
int low=0,high=SIZE-1,middle;
while (low <=high){
middle=(low+high)/2;
if(skey==A[middle])
return middle;
else if(skey <A[middle])
high=middle-1;
else
low=middle+1;
}
return -1;
}
return -1;
}
int main() {
int size, key, position;

printf("Enter the number of elements: ");
scanf("%d", &size);

int arr[size];

printf("Enter %d elements in ascending order:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}

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

position = binarySearch(arr, size, key);

if (position == -1)
printf("The number %d was not found in the array.\n", key);
else
printf("The number %d was found at index %d (position %d).\n", key, position, position + 1);

return 0;
}

160 changes: 92 additions & 68 deletions C/algorithms/sorting/bubble_sort.c
Original file line number Diff line number Diff line change
@@ -1,79 +1,103 @@
/*
* Algorithm: [Bubble Sort]
* Description: [It starts from the beginning of the list and compares each
* pair of adjacent elements, and swap them if they are in the
* wrong order]
* Time Complexity :
* Best Case : O(n) // when given array is already sorted.
* Average Case : O(n^2) //when given array is in random order
* Worst Case : O(n^2) // when given array is in reverse order
* Space Complexity:
* Worst : 0(1)
* Author: [tanshen-kun]
*/
Bubble Sort Algorithm

Description:
Bubble Sort is a simple comparison-based sorting algorithm.
It repeatedly steps through the array, compares adjacent elements,
and swaps them if they are in the wrong order. This process is repeated
until the array is completely sorted. The largest element "bubbles up"
to the end of the array with each pass.

Approach:
- Compare adjacent elements and swap if needed.
- After each iteration, the largest unsorted element is moved to its correct position.
- Repeat the process until no swaps are needed (optimized version).

Use Cases:
- Small datasets
- Educational purposes for learning sorting concepts

Time Complexity:
- Best Case: O(n) when the array is already sorted
- Average Case: O(n²)
- Worst Case: O(n²)
Reason: Two nested loops — outer loop runs n times, inner loop runs (n - i - 1) times.

Space Complexity:
- O(1): Uses only a few extra variables for swapping and flags.
*/

#include <stdio.h>

/*
* bubble_sort : it takse the array and its size then it starts to travel array
* while comparing successive pairs of element until the travel reach the last
* element
* @param arr: Array to process
* @param size: Size of the array
* @return: None
*/

void bubble_sort(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;
}
}
}
// Function prototypes
void inputArray(int arr[], int size);
void displayArray(int arr[], int size);
void bubbleSort(int arr[], int size);

int main() {
int size;

printf("Enter the number of elements: ");
scanf("%d", &size);

int arr[size];

// Input elements
inputArray(arr, size);

printf("\nOriginal array:\n");
displayArray(arr, size);

// Sort array
bubbleSort(arr, size);

printf("\nSorted array in ascending order:\n");
displayArray(arr, size);

// Example test cases (demonstration)
// Input: [5, 2, 9, 1, 5, 6]
// Output: [1, 2, 5, 5, 6, 9]
// Edge Case: Single element or already sorted array

return 0;
}

/*
* printArray: Prints a given array
*
*@param arr: Array to print
*@param size: Size of the array
*/
void printArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
// Reads 'size' number of elements into the array
void inputArray(int arr[], int size) {
printf("Enter %d elements:\n", size);
for (int i = 0; i < size; i++) {
scanf("%d", &arr[i]);
}
}

void test_algorithm() {
printf("Testing Algorithm...\n");

// Test Case 1 : Already Sorted
int test_arr1[] = {1, 2, 3, 4, 5};
int size1 = sizeof(test_arr1) / sizeof(test_arr1[0]);
printf("Test 1 : ");
bubble_sort(test_arr1, size1);
printArray(test_arr1, size1);

// Test Case 2 : Random order
int test_arr2[] = {3, 1, 5, 2, 4};
int size2 = sizeof(test_arr2) / sizeof(test_arr2[0]);
printf("Test 2 : ");
bubble_sort(test_arr2, size2);
printArray(test_arr2, size2);

// Test Case 3 : Reverse Order
int test_arr3[] = {5, 4, 3, 2, 1};
int size3 = sizeof(test_arr3) / sizeof(test_arr3[0]);
printf("Test 3 : ");
bubble_sort(test_arr3, size3);
printArray(test_arr3, size3);
// Displays the contents of the array
void displayArray(int arr[], int size) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}

int main() {
test_algorithm();
return 0;
// Performs Bubble Sort on the array
void bubbleSort(int arr[], int size) {
int temp, swapped;

for (int i = 0; i < size - 1; i++) {
swapped = 0;

// Compare adjacent elements
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// Swap elements
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
swapped = 1;
}
}

// Optimization: Stop if no swaps occurred
if (!swapped)
break;
}
}