Skip to content

Commit b98bef1

Browse files
utkarshkonwarACR1209
authored andcommitted
Added Sorting and Search Algorithms to C (quicksnip-dev#199)
1 parent 1e00637 commit b98bef1

File tree

8 files changed

+370
-0
lines changed

8 files changed

+370
-0
lines changed

public/consolidated/c.json

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,106 @@
4141
"code": "#include<stdio.h>\nvoid swap(int* num1,int* num2){\n *num1 = *num1 + *num2;\n *num2 = *num1 - *num2;\n *num1 = *num1 - *num2;\n}\n\n// Usage:\nint a = 3,b = 4;\nswap(&a,&b); // swaps the values of the a and b variables\n"
4242
}
4343
]
44+
},
45+
{
46+
"name": "Search",
47+
"snippets": [
48+
{
49+
"title": "Binary Search ",
50+
"description": "Searches for an element in a sorted array using the Binary Search algorithm.",
51+
"author": "0xHouss",
52+
"tags": [
53+
"search",
54+
"binarysearch",
55+
"array",
56+
"algorithm"
57+
],
58+
"contributors": [],
59+
"code": "int binarySearch(int arr[], int low, int high, int x) {\n while (low <= high) {\n int mid = low + (high - low) / 2;\n\n // Check if x is present at mid\n if (arr[mid] == x) {\n return mid;\n }\n\n // If x is smaller, search the left half\n if (arr[mid] > x) {\n high = mid - 1;\n } else { // If x is larger, search the right half\n low = mid + 1;\n }\n }\n return -1; // Element not found\n}\n\n// Usage:\nint arr[] = {2, 3, 4, 10, 40};\nint n = sizeof(arr) / sizeof(arr[0]);\nint x = 10;\nint result = binarySearch(arr, 0, n - 1, x);\n// result = 3 (index of the element 10)\n\n\n"
60+
},
61+
{
62+
"title": "Linear Search ",
63+
"description": "Searches for an element in an array using the Linear Search algorithm.",
64+
"author": "0xHouss",
65+
"tags": [
66+
"search",
67+
"linearsearch",
68+
"array",
69+
"algorithm"
70+
],
71+
"contributors": [],
72+
"code": "int linearSearch(int arr[], int n, int x) {\n for (int i = 0; i < n; i++) {\n if (arr[i] == x) {\n return i; // Element found at index i\n }\n }\n return -1; // Element not found\n}\n\n// Usage:\nint arr[] = {10, 20, 30, 40, 50};\nint n = sizeof(arr) / sizeof(arr[0]);\nint x = 30;\nint result = linearSearch(arr, n, x);\n// result = 2 (index of the element 30)\n\n"
73+
}
74+
]
75+
},
76+
{
77+
"name": "Sorting",
78+
"snippets": [
79+
{
80+
"title": "Bubble Sort ",
81+
"description": "Sorts an array of integers using the Bubble Sort algorithm.",
82+
"author": "0xHouss",
83+
"tags": [
84+
"sorting",
85+
"bubblesort",
86+
"array",
87+
"algorithm"
88+
],
89+
"contributors": [],
90+
"code": "void bubbleSort(int arr[], int n) {\n for (int i = 0; i < n - 1; i++) {\n for (int j = 0; j < n - i - 1; j++) {\n if (arr[j] > arr[j + 1]) {\n // Swap arr[j] and arr[j + 1]\n int temp = arr[j];\n arr[j] = arr[j + 1];\n arr[j + 1] = temp;\n }\n }\n }\n}\n\n// Usage:\nint arr[] = {64, 34, 25, 12, 22, 11, 90};\nint n = sizeof(arr) / sizeof(arr[0]);\nbubbleSort(arr, n);\n// Now arr[] is sorted: {11, 12, 22, 25, 34, 64, 90}\n"
91+
},
92+
{
93+
"title": "Insertion Sort ",
94+
"description": "Sorts an array of integers using the Insertion Sort algorithm.",
95+
"author": "0xHouss",
96+
"tags": [
97+
"sorting",
98+
"insertionsort",
99+
"array",
100+
"algorithm"
101+
],
102+
"contributors": [],
103+
"code": "void insertionSort(int arr[], int n) {\n for (int i = 1; i < n; i++) {\n int key = arr[i];\n int j = i - 1;\n\n // Move elements of arr[0..i-1] that are greater than key\n // to one position ahead of their current position\n while (j >= 0 && arr[j] > key) {\n arr[j + 1] = arr[j];\n j--;\n }\n arr[j + 1] = key;\n }\n}\n\n// Usage:\nint arr[] = {12, 11, 13, 5, 6};\nint n = sizeof(arr) / sizeof(arr[0]);\ninsertionSort(arr, n);\n// Now arr[] is sorted: {5, 6, 11, 12, 13}\n\n"
104+
},
105+
{
106+
"title": "Merge Sort ",
107+
"description": "Sorts an array of integers using the Merge Sort algorithm.",
108+
"author": "0xHouss",
109+
"tags": [
110+
"sorting",
111+
"mergesort",
112+
"array",
113+
"algorithm"
114+
],
115+
"contributors": [],
116+
"code": "#include <stdio.h>\n\nvoid merge(int arr[], int l, int m, int r) {\n int n1 = m - l + 1;\n int n2 = r - m;\n\n // Temporary arrays\n int L[n1], R[n2];\n\n // Copy data to temporary arrays L[] and R[]\n for (int i = 0; i < n1; i++)\n L[i] = arr[l + i];\n for (int j = 0; j < n2; j++)\n R[j] = arr[m + 1 + j];\n\n int i = 0, j = 0, k = l;\n\n // Merge the temporary arrays back into arr[l..r]\n while (i < n1 && j < n2) {\n if (L[i] <= R[j]) {\n arr[k] = L[i];\n i++;\n } else {\n arr[k] = R[j];\n j++;\n }\n k++;\n }\n\n // Copy remaining elements of L[], if any\n while (i < n1) {\n arr[k] = L[i];\n i++;\n k++;\n }\n\n // Copy remaining elements of R[], if any\n while (j < n2) {\n arr[k] = R[j];\n j++;\n k++;\n }\n}\n\nvoid mergeSort(int arr[], int l, int r) {\n if (l < r) {\n int m = l + (r - l) / 2;\n\n // Sort first and second halves\n mergeSort(arr, l, m);\n mergeSort(arr, m + 1, r);\n\n merge(arr, l, m, r);\n }\n}\n\n// Usage:\nint arr[] = {38, 27, 43, 3, 9, 82, 10};\nint n = sizeof(arr) / sizeof(arr[0]);\nmergeSort(arr, 0, n - 1);\n// Now arr[] is sorted: {3, 9, 10, 27, 38, 43, 82}\n\n"
117+
},
118+
{
119+
"title": "Quick Sort ",
120+
"description": "Sorts an array of integers using the Quick Sort algorithm.",
121+
"author": "0xHouss",
122+
"tags": [
123+
"sorting",
124+
"quicksort",
125+
"array",
126+
"algorithm"
127+
],
128+
"contributors": [],
129+
"code": "int partition(int arr[], int low, int high) {\n int pivot = arr[high]; // Pivot element\n int i = low - 1;\n\n for (int j = low; j < high; j++) {\n if (arr[j] < pivot) {\n i++;\n // Swap arr[i] and arr[j]\n int temp = arr[i];\n arr[i] = arr[j];\n arr[j] = temp;\n }\n }\n\n // Swap arr[i + 1] and arr[high] (pivot)\n int temp = arr[i + 1];\n arr[i + 1] = arr[high];\n arr[high] = temp;\n\n return i + 1;\n}\n\nvoid quickSort(int arr[], int low, int high) {\n if (low < high) {\n int pi = partition(arr, low, high);\n\n // Recursively sort elements before and after partition\n quickSort(arr, low, pi - 1);\n quickSort(arr, pi + 1, high);\n }\n}\n\n// Usage:\nint arr[] = {10, 7, 8, 9, 1, 5};\nint n = sizeof(arr) / sizeof(arr[0]);\nquickSort(arr, 0, n - 1);\n// Now arr[] is sorted: {1, 5, 7, 8, 9, 10}\n\n"
130+
},
131+
{
132+
"title": "Selection Sort ",
133+
"description": "Sorts an array of integers using the Selection Sort algorithm.",
134+
"author": "0xHouss",
135+
"tags": [
136+
"sorting",
137+
"selectionsort",
138+
"array",
139+
"algorithm"
140+
],
141+
"contributors": [],
142+
"code": "void selectionSort(int arr[], int n) {\n for (int i = 0; i < n - 1; i++) {\n int minIdx = i;\n\n // Find the minimum element in the unsorted part of the array\n for (int j = i + 1; j < n; j++) {\n if (arr[j] < arr[minIdx]) {\n minIdx = j;\n }\n }\n\n // Swap the found minimum element with the first element of the unsorted part\n int temp = arr[minIdx];\n arr[minIdx] = arr[i];\n arr[i] = temp;\n }\n}\n\n// Usage:\nint arr[] = {64, 25, 12, 22, 11};\nint n = sizeof(arr) / sizeof(arr[0]);\nselectionSort(arr, n);\n// Now arr[] is sorted: {11, 12, 22, 25, 64}\n\n"
143+
}
144+
]
44145
}
45146
]

snippets/c/search/binary-search.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
title: Binary Search
3+
description: Searches for an element in a sorted array using the Binary Search algorithm.
4+
author: 0xHouss
5+
tags: search,binarysearch,array,algorithm
6+
---
7+
8+
```c
9+
int binarySearch(int arr[], int low, int high, int x) {
10+
while (low <= high) {
11+
int mid = low + (high - low) / 2;
12+
13+
// Check if x is present at mid
14+
if (arr[mid] == x) {
15+
return mid;
16+
}
17+
18+
// If x is smaller, search the left half
19+
if (arr[mid] > x) {
20+
high = mid - 1;
21+
} else { // If x is larger, search the right half
22+
low = mid + 1;
23+
}
24+
}
25+
return -1; // Element not found
26+
}
27+
28+
// Usage:
29+
int arr[] = {2, 3, 4, 10, 40};
30+
int n = sizeof(arr) / sizeof(arr[0]);
31+
int x = 10;
32+
int result = binarySearch(arr, 0, n - 1, x);
33+
// result = 3 (index of the element 10)
34+
35+
36+
```

snippets/c/search/linear-search.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
---
2+
title: Linear Search
3+
description: Searches for an element in an array using the Linear Search algorithm.
4+
author: 0xHouss
5+
tags: search,linearsearch,array,algorithm
6+
---
7+
8+
```c
9+
int linearSearch(int arr[], int n, int x) {
10+
for (int i = 0; i < n; i++) {
11+
if (arr[i] == x) {
12+
return i; // Element found at index i
13+
}
14+
}
15+
return -1; // Element not found
16+
}
17+
18+
// Usage:
19+
int arr[] = {10, 20, 30, 40, 50};
20+
int n = sizeof(arr) / sizeof(arr[0]);
21+
int x = 30;
22+
int result = linearSearch(arr, n, x);
23+
// result = 2 (index of the element 30)
24+
25+
```

snippets/c/sorting/bubble-sort.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
---
2+
title: Bubble Sort
3+
description: Sorts an array of integers using the Bubble Sort algorithm.
4+
author: 0xHouss
5+
tags: sorting,bubblesort,array,algorithm
6+
---
7+
8+
```c
9+
void bubbleSort(int arr[], int n) {
10+
for (int i = 0; i < n - 1; i++) {
11+
for (int j = 0; j < n - i - 1; j++) {
12+
if (arr[j] > arr[j + 1]) {
13+
// Swap arr[j] and arr[j + 1]
14+
int temp = arr[j];
15+
arr[j] = arr[j + 1];
16+
arr[j + 1] = temp;
17+
}
18+
}
19+
}
20+
}
21+
22+
// Usage:
23+
int arr[] = {64, 34, 25, 12, 22, 11, 90};
24+
int n = sizeof(arr) / sizeof(arr[0]);
25+
bubbleSort(arr, n);
26+
// Now arr[] is sorted: {11, 12, 22, 25, 34, 64, 90}
27+
```

snippets/c/sorting/insertion-sort.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
title: Insertion Sort
3+
description: Sorts an array of integers using the Insertion Sort algorithm.
4+
author: 0xHouss
5+
tags: sorting,insertionsort,array,algorithm
6+
---
7+
8+
```c
9+
void insertionSort(int arr[], int n) {
10+
for (int i = 1; i < n; i++) {
11+
int key = arr[i];
12+
int j = i - 1;
13+
14+
// Move elements of arr[0..i-1] that are greater than key
15+
// to one position ahead of their current position
16+
while (j >= 0 && arr[j] > key) {
17+
arr[j + 1] = arr[j];
18+
j--;
19+
}
20+
arr[j + 1] = key;
21+
}
22+
}
23+
24+
// Usage:
25+
int arr[] = {12, 11, 13, 5, 6};
26+
int n = sizeof(arr) / sizeof(arr[0]);
27+
insertionSort(arr, n);
28+
// Now arr[] is sorted: {5, 6, 11, 12, 13}
29+
30+
```

snippets/c/sorting/merge-sort.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
---
2+
title: Merge Sort
3+
description: Sorts an array of integers using the Merge Sort algorithm.
4+
author: 0xHouss
5+
tags: sorting,mergesort,array,algorithm
6+
---
7+
8+
```c
9+
#include <stdio.h>
10+
11+
void merge(int arr[], int l, int m, int r) {
12+
int n1 = m - l + 1;
13+
int n2 = r - m;
14+
15+
// Temporary arrays
16+
int L[n1], R[n2];
17+
18+
// Copy data to temporary arrays L[] and R[]
19+
for (int i = 0; i < n1; i++)
20+
L[i] = arr[l + i];
21+
for (int j = 0; j < n2; j++)
22+
R[j] = arr[m + 1 + j];
23+
24+
int i = 0, j = 0, k = l;
25+
26+
// Merge the temporary arrays back into arr[l..r]
27+
while (i < n1 && j < n2) {
28+
if (L[i] <= R[j]) {
29+
arr[k] = L[i];
30+
i++;
31+
} else {
32+
arr[k] = R[j];
33+
j++;
34+
}
35+
k++;
36+
}
37+
38+
// Copy remaining elements of L[], if any
39+
while (i < n1) {
40+
arr[k] = L[i];
41+
i++;
42+
k++;
43+
}
44+
45+
// Copy remaining elements of R[], if any
46+
while (j < n2) {
47+
arr[k] = R[j];
48+
j++;
49+
k++;
50+
}
51+
}
52+
53+
void mergeSort(int arr[], int l, int r) {
54+
if (l < r) {
55+
int m = l + (r - l) / 2;
56+
57+
// Sort first and second halves
58+
mergeSort(arr, l, m);
59+
mergeSort(arr, m + 1, r);
60+
61+
merge(arr, l, m, r);
62+
}
63+
}
64+
65+
// Usage:
66+
int arr[] = {38, 27, 43, 3, 9, 82, 10};
67+
int n = sizeof(arr) / sizeof(arr[0]);
68+
mergeSort(arr, 0, n - 1);
69+
// Now arr[] is sorted: {3, 9, 10, 27, 38, 43, 82}
70+
71+
```

snippets/c/sorting/quick-sort.md

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
---
2+
title: Quick Sort
3+
description: Sorts an array of integers using the Quick Sort algorithm.
4+
author: 0xHouss
5+
tags: sorting,quicksort,array,algorithm
6+
---
7+
8+
```c
9+
int partition(int arr[], int low, int high) {
10+
int pivot = arr[high]; // Pivot element
11+
int i = low - 1;
12+
13+
for (int j = low; j < high; j++) {
14+
if (arr[j] < pivot) {
15+
i++;
16+
// Swap arr[i] and arr[j]
17+
int temp = arr[i];
18+
arr[i] = arr[j];
19+
arr[j] = temp;
20+
}
21+
}
22+
23+
// Swap arr[i + 1] and arr[high] (pivot)
24+
int temp = arr[i + 1];
25+
arr[i + 1] = arr[high];
26+
arr[high] = temp;
27+
28+
return i + 1;
29+
}
30+
31+
void quickSort(int arr[], int low, int high) {
32+
if (low < high) {
33+
int pi = partition(arr, low, high);
34+
35+
// Recursively sort elements before and after partition
36+
quickSort(arr, low, pi - 1);
37+
quickSort(arr, pi + 1, high);
38+
}
39+
}
40+
41+
// Usage:
42+
int arr[] = {10, 7, 8, 9, 1, 5};
43+
int n = sizeof(arr) / sizeof(arr[0]);
44+
quickSort(arr, 0, n - 1);
45+
// Now arr[] is sorted: {1, 5, 7, 8, 9, 10}
46+
47+
```

snippets/c/sorting/selection-sort.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
title: Selection Sort
3+
description: Sorts an array of integers using the Selection Sort algorithm.
4+
author: 0xHouss
5+
tags: sorting,selectionsort,array,algorithm
6+
---
7+
8+
```c
9+
void selectionSort(int arr[], int n) {
10+
for (int i = 0; i < n - 1; i++) {
11+
int minIdx = i;
12+
13+
// Find the minimum element in the unsorted part of the array
14+
for (int j = i + 1; j < n; j++) {
15+
if (arr[j] < arr[minIdx]) {
16+
minIdx = j;
17+
}
18+
}
19+
20+
// Swap the found minimum element with the first element of the unsorted part
21+
int temp = arr[minIdx];
22+
arr[minIdx] = arr[i];
23+
arr[i] = temp;
24+
}
25+
}
26+
27+
// Usage:
28+
int arr[] = {64, 25, 12, 22, 11};
29+
int n = sizeof(arr) / sizeof(arr[0]);
30+
selectionSort(arr, n);
31+
// Now arr[] is sorted: {11, 12, 22, 25, 64}
32+
33+
```

0 commit comments

Comments
 (0)