-
-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added Sorting and Search Algorithms to C
- Loading branch information
1 parent
b0c00df
commit 2bf6879
Showing
11 changed files
with
468 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
--- | ||
title: Binary Search | ||
description: Searches for an element in a sorted array using the Binary Search algorithm. | ||
author: 0xHouss | ||
tags: search,binarysearch,array,algorithm | ||
--- | ||
|
||
```c | ||
int binarySearch(int arr[], int low, int high, int x) { | ||
while (low <= high) { | ||
int mid = low + (high - low) / 2; | ||
|
||
// Check if x is present at mid | ||
if (arr[mid] == x) { | ||
return mid; | ||
} | ||
|
||
// If x is smaller, search the left half | ||
if (arr[mid] > x) { | ||
high = mid - 1; | ||
} else { // If x is larger, search the right half | ||
low = mid + 1; | ||
} | ||
} | ||
return -1; // Element not found | ||
} | ||
|
||
// Usage: | ||
int arr[] = {2, 3, 4, 10, 40}; | ||
int n = sizeof(arr) / sizeof(arr[0]); | ||
int x = 10; | ||
int result = binarySearch(arr, 0, n - 1, x); | ||
// result = 3 (index of the element 10) | ||
|
||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
--- | ||
title: Linear Search | ||
description: Searches for an element in an array using the Linear Search algorithm. | ||
author: 0xHouss | ||
tags: search,linearsearch,array,algorithm | ||
--- | ||
|
||
```c | ||
int linearSearch(int arr[], int n, int x) { | ||
for (int i = 0; i < n; i++) { | ||
if (arr[i] == x) { | ||
return i; // Element found at index i | ||
} | ||
} | ||
return -1; // Element not found | ||
} | ||
|
||
// Usage: | ||
int arr[] = {10, 20, 30, 40, 50}; | ||
int n = sizeof(arr) / sizeof(arr[0]); | ||
int x = 30; | ||
int result = linearSearch(arr, n, x); | ||
// result = 2 (index of the element 30) | ||
|
||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
--- | ||
title: Bubble Sort | ||
description: Sorts an array of integers using the Bubble Sort algorithm. | ||
author: 0xHouss | ||
tags: sorting,bubblesort,array,algorithm | ||
--- | ||
|
||
```c | ||
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]) { | ||
// Swap arr[j] and arr[j + 1] | ||
int temp = arr[j]; | ||
arr[j] = arr[j + 1]; | ||
arr[j + 1] = temp; | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Usage: | ||
int arr[] = {64, 34, 25, 12, 22, 11, 90}; | ||
int n = sizeof(arr) / sizeof(arr[0]); | ||
bubbleSort(arr, n); | ||
// Now arr[] is sorted: {11, 12, 22, 25, 34, 64, 90} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
--- | ||
title: Insertion Sort | ||
description: Sorts an array of integers using the Insertion Sort algorithm. | ||
author: 0xHouss | ||
tags: sorting,insertionsort,array,algorithm | ||
--- | ||
|
||
```c | ||
void insertionSort(int arr[], int n) { | ||
for (int i = 1; i < n; i++) { | ||
int key = arr[i]; | ||
int j = i - 1; | ||
|
||
// Move elements of arr[0..i-1] that are greater than key | ||
// to one position ahead of their current position | ||
while (j >= 0 && arr[j] > key) { | ||
arr[j + 1] = arr[j]; | ||
j--; | ||
} | ||
arr[j + 1] = key; | ||
} | ||
} | ||
|
||
// Usage: | ||
int arr[] = {12, 11, 13, 5, 6}; | ||
int n = sizeof(arr) / sizeof(arr[0]); | ||
insertionSort(arr, n); | ||
// Now arr[] is sorted: {5, 6, 11, 12, 13} | ||
|
||
``` |
Oops, something went wrong.