diff --git a/public/consolidated/c.json b/public/consolidated/c.json index 2343c989..ae37ef55 100644 --- a/public/consolidated/c.json +++ b/public/consolidated/c.json @@ -41,5 +41,106 @@ "code": "#include\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" } ] + }, + { + "name": "Search", + "snippets": [ + { + "title": "Binary Search ", + "description": "Searches for an element in a sorted array using the Binary Search algorithm.", + "author": "utkarshkonwar", + "tags": [ + "search", + "binarysearch", + "array", + "algorithm" + ], + "contributors": [], + "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" + }, + { + "title": "Linear Search ", + "description": "Searches for an element in an array using the Linear Search algorithm.", + "author": "utkarshkonwar", + "tags": [ + "search", + "linearsearch", + "array", + "algorithm" + ], + "contributors": [], + "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" + } + ] + }, + { + "name": "Sorting", + "snippets": [ + { + "title": "Bubble Sort ", + "description": "Sorts an array of integers using the Bubble Sort algorithm.", + "author": "utkarshkonwar", + "tags": [ + "sorting", + "bubblesort", + "array", + "algorithm" + ], + "contributors": [], + "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" + }, + { + "title": "Insertion Sort ", + "description": "Sorts an array of integers using the Insertion Sort algorithm.", + "author": "utkarshkonwar", + "tags": [ + "sorting", + "insertionsort", + "array", + "algorithm" + ], + "contributors": [], + "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" + }, + { + "title": "Merge Sort ", + "description": "Sorts an array of integers using the Merge Sort algorithm.", + "author": "utkarshkonwar", + "tags": [ + "sorting", + "mergesort", + "array", + "algorithm" + ], + "contributors": [], + "code": "#include \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" + }, + { + "title": "Quick Sort ", + "description": "Sorts an array of integers using the Quick Sort algorithm.", + "author": "utkarshkonwar", + "tags": [ + "sorting", + "quicksort", + "array", + "algorithm" + ], + "contributors": [], + "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" + }, + { + "title": "Selection Sort ", + "description": "Sorts an array of integers using the Selection Sort algorithm.", + "author": "utkarshkonwar", + "tags": [ + "sorting", + "selectionsort", + "array", + "algorithm" + ], + "contributors": [], + "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" + } + ] } ] \ No newline at end of file diff --git a/public/consolidated/cpp.json b/public/consolidated/cpp.json index fd38de1b..c121a725 100644 --- a/public/consolidated/cpp.json +++ b/public/consolidated/cpp.json @@ -33,7 +33,6 @@ ] }, { - "categoryName": "Debuging", "name": "Debugging", "snippets": [ { @@ -50,6 +49,23 @@ } ] }, + { + "name": "Debuging", + "snippets": [ + { + "title": "Vector Print", + "description": "Overloads the << operator to print the contents of a vector just like in python.", + "author": "Mohamed-faaris", + "tags": [ + "printing", + "debuging", + "vector" + ], + "contributors": [], + "code": "#include \n#include \n\ntemplate \nstd::ostream& operator<<(std::ostream& os, const std::vector& vec) {\n os << \"[\"; \n for (size_t i = 0; i < vec.size(); ++i) {\n os << vec[i]; // Print each vector element\n if (i != vec.size() - 1) {\n os << \", \"; // Add separator\n }\n }\n os << \"]\"; \n return os; // Return the stream\n}\n\n// Usage:\nstd::vector numbers = {1, 2, 3, 4, 5};\nstd::cout << numbers << std::endl; // Outputs: [1, 2, 3, 4, 5]\n\n" + } + ] + }, { "name": "Math And Numbers", "snippets": [ diff --git a/public/consolidated/css.json b/public/consolidated/css.json index 359088c9..0f19249e 100644 --- a/public/consolidated/css.json +++ b/public/consolidated/css.json @@ -1,4 +1,67 @@ [ + { + "name": "Animations", + "snippets": [ + { + "title": "Blink Animation", + "description": "Adds an infinite blinking animation to an element", + "author": "AlsoKnownAs-Ax", + "tags": [ + "animation", + "blink", + "infinite" + ], + "contributors": [], + "code": ".blink {\n animation: blink 1s linear infinite;\n}\n\n@keyframes blink{\n 0%{\n opacity: 0;\n }\n 50%{\n opacity: 1;\n }\n 100%{\n opacity: 0;\n }\n}\n" + }, + { + "title": "Pulse Animation", + "description": "Adds a smooth pulsing animation with opacity and scale effects", + "author": "AlsoKnownAs-Ax", + "tags": [ + "animation", + "pulse", + "pulse-scale" + ], + "contributors": [], + "code": ".pulse {\n animation: pulse 2s ease-in-out infinite;\n}\n\n@keyframes pulse {\n 0% {\n opacity: 0.5;\n transform: scale(1);\n }\n 50% {\n opacity: 1;\n transform: scale(1.05);\n }\n 100% {\n opacity: 0.5;\n transform: scale(1);\n }\n}\n" + }, + { + "title": "Shake Animation", + "description": "Adds a shake animation ( commonly used to mark invalid fields )", + "author": "AlsoKnownAs-Ax", + "tags": [ + "shake", + "shake-horizontal" + ], + "contributors": [], + "code": ".shake {\n animation: shake .5s ease-in-out;\n}\n\n@keyframes shake {\n 0%, 100% {\n transform: translateX(0);\n }\n 25% {\n transform: translateX(-10px);\n }\n 50% {\n transform: translateX(10px);\n }\n 75% {\n transform: translateX(-10px);\n }\n}\n" + }, + { + "title": "Slide-in Animation", + "description": "Adds a slide-in from the right side of the screen", + "author": "AlsoKnownAs-Ax", + "tags": [ + "animation", + "slide-in", + "slide-right" + ], + "contributors": [], + "code": ".slide-in {\n animation: slide-in 1s ease-in-out;\n}\n\n@keyframes slide-in {\n from {\n scale: 300% 1;\n translate: 150vw 0;\n }\n\n to {\n scale: 100% 1;\n translate: 0 0;\n }\n}\n" + }, + { + "title": "Typewriter Animation", + "description": "Adds a typewriter animation + blinking cursor", + "author": "AlsoKnownAs-Ax", + "tags": [ + "blinking", + "typewriter" + ], + "contributors": [], + "code": "
\n
\n

Typerwriter Animation

\n
\n
\n```\n\n```css\n .typewriter{\n display: flex;\n justify-content: center;\n }\n\n .typewriter p {\n overflow: hidden;\n font-size: 1.5rem;\n font-family: monospace;\n border-right: 1px solid;\n margin-inline: auto;\n white-space: nowrap;\n /* The cursor will inherit the text's color by default */\n /* border-color: red */ \n /* Steps: number of chars (better to set directly in js)*/\n animation: typing 3s steps(21) forwards,\n blink 1s step-end infinite;\n }\n\n @keyframes typing{\n from{\n width: 0%\n }\n to{\n width: 100%\n }\n }\n\n @keyframes blink{\n 50%{\n border-color: transparent;\n }\n }\n" + } + ] + }, { "name": "Buttons", "snippets": [ diff --git a/public/consolidated/java.json b/public/consolidated/java.json index 3a36e17d..f972f5e0 100644 --- a/public/consolidated/java.json +++ b/public/consolidated/java.json @@ -1,4 +1,22 @@ [ + { + "name": "Array Manipulation", + "snippets": [ + { + "title": "Zip Two Lists", + "description": "Zips two lists into a list of paired elements, combining corresponding elements from both lists.", + "author": "davidanukam", + "tags": [ + "lists", + "zip", + "stream-api", + "collections" + ], + "contributors": [], + "code": "import java.util.*; // Importing utility classes for List and Arrays\nimport java.util.stream.IntStream; // Importing IntStream for range and mapping\nimport java.util.stream.Collectors; // Importing Collectors for collecting stream results\n\npublic List zip(List list1, List list2) {\n // Create pairs by iterating through the indices of both lists\n return IntStream.range(0, Math.min(list1.size(), list2.size())) // Limit the range to the smaller list\n .mapToObj(i -> Arrays.asList(list1.get(i), list2.get(i))) // Pair elements from both lists at index i\n .collect(Collectors.toList()); // Collect the pairs into a List\n}\n\n// Usage:\nList arr1 = Arrays.asList(\"a\", \"b\", \"c\");\nList arr2 = Arrays.asList(1, 2, 3);\nList zipped = zip(arr1, arr2);\n\nSystem.out.println(zipped); // Output: [[a, 1], [b, 2], [c, 3]]\n" + } + ] + }, { "name": "Basics", "snippets": [ diff --git a/public/consolidated/python.json b/public/consolidated/python.json index 8acadcfc..77474474 100644 --- a/public/consolidated/python.json +++ b/public/consolidated/python.json @@ -434,6 +434,107 @@ } ] }, + { + "name": "Search", + "snippets": [ + { + "title": "Binary Search ", + "description": "Searches for an element in a sorted list using the Binary Search algorithm.", + "author": "utkarshkonwar", + "tags": [ + "search", + "binarysearch", + "array", + "algorithm" + ], + "contributors": [], + "code": "def binarySearch(arr, low, high, x):\n while low <= high:\n mid = (low + high) // 2\n\n # Check if x is present at mid\n if arr[mid] == x:\n return mid\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 return -1 # Element not found\n\n# Usage:\narr = [2, 3, 4, 10, 40]\nx = 10\nresult = binarySearch(arr, 0, len(arr) - 1, x)\n# result = 3 (index of the element 10)\n\n" + }, + { + "title": "Linear Search ", + "description": "Searches for an element in a list using the Linear Search algorithm.", + "author": "utkarshkonwar", + "tags": [ + "search", + "linearsearch", + "array", + "algorithm" + ], + "contributors": [], + "code": "def linearSearch(arr, x):\n for i in range(len(arr)):\n if arr[i] == x:\n return i # Element found at index i\n return -1 # Element not found\n\n# Usage:\narr = [10, 20, 30, 40, 50]\nx = 30\nresult = linearSearch(arr, x)\n# result = 2 (index of the element 30)\n\n" + } + ] + }, + { + "name": "Sorting", + "snippets": [ + { + "title": "Bubble Sort", + "description": "Sorts a list of integers using the Bubble Sort algorithm.", + "author": "utkarshkonwar", + "tags": [ + "sorting", + "bubblesort", + "array", + "algorithm" + ], + "contributors": [], + "code": "def bubbleSort(arr):\n n = len(arr)\n for i in range(n - 1):\n for j in range(n - i - 1):\n if arr[j]> arr[j + 1]:\n # Swap arr[j] and arr[j + 1]\n arr[j], arr[j + 1] = arr[j + 1], arr[j]\n\n# Usage:\narr = [64, 34, 25, 12, 22, 11, 90]\nbubbleSort(arr)\n# Now arr is sorted: [11, 12, 22, 25, 34, 64, 90]\n" + }, + { + "title": "Insertion Sort", + "description": "Sorts a list of integers using the Insertion Sort algorithm.", + "author": "utkarshkonwar", + "tags": [ + "sorting", + "insertionsort", + "array", + "algorithm" + ], + "contributors": [], + "code": "def insertionSort(arr):\n for i in range(1, len(arr)):\n key = arr[i]\n 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 and arr[j]> key:\n arr[j + 1] = arr[j]\n j -= 1\n arr[j + 1] = key\n\n# Usage:\narr = [12, 11, 13, 5, 6]\ninsertionSort(arr)\n# Now arr is sorted: [5, 6, 11, 12, 13]\n\n" + }, + { + "title": "Merge Sort ", + "description": "Sorts a list of integers using the Merge Sort algorithm.", + "author": "utkarshkonwar", + "tags": [ + "sorting", + "mergesort", + "array", + "algorithm" + ], + "contributors": [], + "code": "def merge(arr, l, m, r):\n n1 = m - l + 1\n n2 = r - m\n\n # Temporary arrays\n L = arr[l:l + n1]\n R = arr[m + 1:m + 1 + n2]\n\n i = j = k = 0\n\n # Merge the temporary arrays back into arr[l..r]\n while i < n1 and j < n2:\n if L[i] <= R[j]:\n arr[k + l] = L[i]\n i += 1\n else:\n arr[k + l] = R[j]\n j += 1\n k += 1\n\n # Copy remaining elements of L[], if any\n while i < n1:\n arr[k + l] = L[i]\n i += 1\n k += 1\n\n # Copy remaining elements of R[], if any\n while j < n2:\n arr[k + l] = R[j]\n j += 1\n k += 1\n\ndef mergeSort(arr, l, r):\n if l < r:\n m = (l + r) // 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# Usage:\narr = [38, 27, 43, 3, 9, 82, 10]\nmergeSort(arr, 0, len(arr) - 1)\n# Now arr is sorted: [3, 9, 10, 27, 38, 43, 82]\n\n" + }, + { + "title": "Quick Sort ", + "description": "Sorts a list of integers using the Quick Sort algorithm.", + "author": "utkarshkonwar", + "tags": [ + "sorting", + "quicksort", + "array", + "algorithm" + ], + "contributors": [], + "code": "def partition(arr, low, high):\n pivot = arr[high] # Pivot element\n i = low - 1\n\n for j in range(low, high):\n if arr[j] < pivot:\n i += 1\n # Swap arr[i] and arr[j]\n arr[i], arr[j] = arr[j], arr[i]\n\n # Swap arr[i + 1] and arr[high] (pivot)\n arr[i + 1], arr[high] = arr[high], arr[i + 1]\n return i + 1\n\ndef quickSort(arr, low, high):\n if low < high:\n 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# Usage:\narr = [10, 7, 8, 9, 1, 5]\nquickSort(arr, 0, len(arr) - 1)\n# Now arr is sorted: [1, 5, 7, 8, 9, 10]\n\n" + }, + { + "title": "Selection Sort ", + "description": "Sorts a list of integers using the Selection Sort algorithm.", + "author": "utkarshkonwar", + "tags": [ + "sorting", + "selectionsort", + "array", + "algorithm" + ], + "contributors": [], + "code": "def selectionSort(arr):\n for i in range(len(arr) - 1):\n minIdx = i\n\n # Find the minimum element in the unsorted part of the list\n for j in range(i + 1, len(arr)):\n if arr[j] < arr[minIdx]:\n minIdx = j\n\n # Swap the found minimum element with the first element of the unsorted part\n arr[i], arr[minIdx] = arr[minIdx], arr[i]\n\n# Usage:\narr = [64, 25, 12, 22, 11]\nselectionSort(arr)\n# Now arr is sorted: [11, 12, 22, 25, 64]\n\n" + } + ] + }, { "name": "Sqlite Database", "snippets": [ diff --git a/snippets/c/search/binary-search.md b/snippets/c/search/binary-search.md new file mode 100644 index 00000000..0505326c --- /dev/null +++ b/snippets/c/search/binary-search.md @@ -0,0 +1,36 @@ +--- +title: Binary Search +description: Searches for an element in a sorted array using the Binary Search algorithm. +author: utkarshkonwar +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) + + +``` \ No newline at end of file diff --git a/snippets/c/search/linear-search.md b/snippets/c/search/linear-search.md new file mode 100644 index 00000000..1bf6adff --- /dev/null +++ b/snippets/c/search/linear-search.md @@ -0,0 +1,25 @@ +--- +title: Linear Search +description: Searches for an element in an array using the Linear Search algorithm. +author: utkarshkonwar +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) + +``` \ No newline at end of file diff --git a/snippets/c/sorting/bubble-sort.md b/snippets/c/sorting/bubble-sort.md new file mode 100644 index 00000000..6297d650 --- /dev/null +++ b/snippets/c/sorting/bubble-sort.md @@ -0,0 +1,27 @@ +--- +title: Bubble Sort +description: Sorts an array of integers using the Bubble Sort algorithm. +author: utkarshkonwar +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} +``` \ No newline at end of file diff --git a/snippets/c/sorting/insertion-sort.md b/snippets/c/sorting/insertion-sort.md new file mode 100644 index 00000000..5ed63a44 --- /dev/null +++ b/snippets/c/sorting/insertion-sort.md @@ -0,0 +1,30 @@ +--- +title: Insertion Sort +description: Sorts an array of integers using the Insertion Sort algorithm. +author: utkarshkonwar +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} + +``` \ No newline at end of file diff --git a/snippets/c/sorting/merge-sort.md b/snippets/c/sorting/merge-sort.md new file mode 100644 index 00000000..d1796210 --- /dev/null +++ b/snippets/c/sorting/merge-sort.md @@ -0,0 +1,71 @@ +--- +title: Merge Sort +description: Sorts an array of integers using the Merge Sort algorithm. +author: utkarshkonwar +tags: sorting,mergesort,array,algorithm +--- + +```c +#include + +void merge(int arr[], int l, int m, int r) { + int n1 = m - l + 1; + int n2 = r - m; + + // Temporary arrays + int L[n1], R[n2]; + + // Copy data to temporary arrays L[] and R[] + for (int i = 0; i < n1; i++) + L[i] = arr[l + i]; + for (int j = 0; j < n2; j++) + R[j] = arr[m + 1 + j]; + + int i = 0, j = 0, k = l; + + // Merge the temporary arrays back into arr[l..r] + while (i < n1 && j < n2) { + if (L[i] <= R[j]) { + arr[k] = L[i]; + i++; + } else { + arr[k] = R[j]; + j++; + } + k++; + } + + // Copy remaining elements of L[], if any + while (i < n1) { + arr[k] = L[i]; + i++; + k++; + } + + // Copy remaining elements of R[], if any + while (j < n2) { + arr[k] = R[j]; + j++; + k++; + } +} + +void mergeSort(int arr[], int l, int r) { + if (l < r) { + int m = l + (r - l) / 2; + + // Sort first and second halves + mergeSort(arr, l, m); + mergeSort(arr, m + 1, r); + + merge(arr, l, m, r); + } +} + +// Usage: +int arr[] = {38, 27, 43, 3, 9, 82, 10}; +int n = sizeof(arr) / sizeof(arr[0]); +mergeSort(arr, 0, n - 1); +// Now arr[] is sorted: {3, 9, 10, 27, 38, 43, 82} + +``` \ No newline at end of file diff --git a/snippets/c/sorting/quick-sort.md b/snippets/c/sorting/quick-sort.md new file mode 100644 index 00000000..fb0ea9ab --- /dev/null +++ b/snippets/c/sorting/quick-sort.md @@ -0,0 +1,47 @@ +--- +title: Quick Sort +description: Sorts an array of integers using the Quick Sort algorithm. +author: utkarshkonwar +tags: sorting,quicksort,array,algorithm +--- + +```c +int partition(int arr[], int low, int high) { + int pivot = arr[high]; // Pivot element + int i = low - 1; + + for (int j = low; j < high; j++) { + if (arr[j] < pivot) { + i++; + // Swap arr[i] and arr[j] + int temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } + } + + // Swap arr[i + 1] and arr[high] (pivot) + int temp = arr[i + 1]; + arr[i + 1] = arr[high]; + arr[high] = temp; + + return i + 1; +} + +void quickSort(int arr[], int low, int high) { + if (low < high) { + int pi = partition(arr, low, high); + + // Recursively sort elements before and after partition + quickSort(arr, low, pi - 1); + quickSort(arr, pi + 1, high); + } +} + +// Usage: +int arr[] = {10, 7, 8, 9, 1, 5}; +int n = sizeof(arr) / sizeof(arr[0]); +quickSort(arr, 0, n - 1); +// Now arr[] is sorted: {1, 5, 7, 8, 9, 10} + +``` \ No newline at end of file diff --git a/snippets/c/sorting/selection-sort.md b/snippets/c/sorting/selection-sort.md new file mode 100644 index 00000000..57804569 --- /dev/null +++ b/snippets/c/sorting/selection-sort.md @@ -0,0 +1,33 @@ +--- +title: Selection Sort +description: Sorts an array of integers using the Selection Sort algorithm. +author: utkarshkonwar +tags: sorting,selectionsort,array,algorithm +--- + +```c +void selectionSort(int arr[], int n) { + for (int i = 0; i < n - 1; i++) { + int minIdx = i; + + // Find the minimum element in the unsorted part of the array + for (int j = i + 1; j < n; j++) { + if (arr[j] < arr[minIdx]) { + minIdx = j; + } + } + + // Swap the found minimum element with the first element of the unsorted part + int temp = arr[minIdx]; + arr[minIdx] = arr[i]; + arr[i] = temp; + } +} + +// Usage: +int arr[] = {64, 25, 12, 22, 11}; +int n = sizeof(arr) / sizeof(arr[0]); +selectionSort(arr, n); +// Now arr[] is sorted: {11, 12, 22, 25, 64} + +``` \ No newline at end of file diff --git a/snippets/python/search/binary-search.md b/snippets/python/search/binary-search.md new file mode 100644 index 00000000..ef4a5fca --- /dev/null +++ b/snippets/python/search/binary-search.md @@ -0,0 +1,31 @@ +--- +title: Binary Search +description: Searches for an element in a sorted list using the Binary Search algorithm. +author: utkarshkonwar +tags: search,binarysearch,array,algorithm +--- + + +```py +def binarySearch(arr, low, high, x): + while low <= high: + mid = (low + high) // 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: +arr = [2, 3, 4, 10, 40] +x = 10 +result = binarySearch(arr, 0, len(arr) - 1, x) +# result = 3 (index of the element 10) + +``` \ No newline at end of file diff --git a/snippets/python/search/linear-search.md b/snippets/python/search/linear-search.md new file mode 100644 index 00000000..414f06af --- /dev/null +++ b/snippets/python/search/linear-search.md @@ -0,0 +1,22 @@ +--- +title: Linear Search +description: Searches for an element in a list using the Linear Search algorithm. +author: utkarshkonwar +tags: search,linearsearch,array,algorithm +--- + + +```py +def linearSearch(arr, x): + for i in range(len(arr)): + if arr[i] == x: + return i # Element found at index i + return -1 # Element not found + +# Usage: +arr = [10, 20, 30, 40, 50] +x = 30 +result = linearSearch(arr, x) +# result = 2 (index of the element 30) + +``` \ No newline at end of file diff --git a/snippets/python/sorting/bubble-sort.md b/snippets/python/sorting/bubble-sort.md new file mode 100644 index 00000000..d0cf2776 --- /dev/null +++ b/snippets/python/sorting/bubble-sort.md @@ -0,0 +1,22 @@ +--- +title: Bubble Sort +description: Sorts a list of integers using the Bubble Sort algorithm. +author: utkarshkonwar +tags: sorting,bubblesort,array,algorithm +--- + + +```py +def bubbleSort(arr): + n = len(arr) + for i in range(n - 1): + for j in range(n - i - 1): + if arr[j]> arr[j + 1]: + # Swap arr[j] and arr[j + 1] + arr[j], arr[j + 1] = arr[j + 1], arr[j] + +# Usage: +arr = [64, 34, 25, 12, 22, 11, 90] +bubbleSort(arr) +# Now arr is sorted: [11, 12, 22, 25, 34, 64, 90] +``` \ No newline at end of file diff --git a/snippets/python/sorting/insertion-sort.md b/snippets/python/sorting/insertion-sort.md new file mode 100644 index 00000000..16d7fa02 --- /dev/null +++ b/snippets/python/sorting/insertion-sort.md @@ -0,0 +1,27 @@ +--- +title: Insertion Sort +description: Sorts a list of integers using the Insertion Sort algorithm. +author: utkarshkonwar +tags: sorting,insertionsort,array,algorithm +--- + + +```py +def insertionSort(arr): + for i in range(1, len(arr)): + key = arr[i] + 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 and arr[j]> key: + arr[j + 1] = arr[j] + j -= 1 + arr[j + 1] = key + +# Usage: +arr = [12, 11, 13, 5, 6] +insertionSort(arr) +# Now arr is sorted: [5, 6, 11, 12, 13] + +``` \ No newline at end of file diff --git a/snippets/python/sorting/merge-sort.md b/snippets/python/sorting/merge-sort.md new file mode 100644 index 00000000..fd02d4f2 --- /dev/null +++ b/snippets/python/sorting/merge-sort.md @@ -0,0 +1,57 @@ +--- +title: Merge Sort +description: Sorts a list of integers using the Merge Sort algorithm. +author: utkarshkonwar +tags: sorting,mergesort,array,algorithm +--- + + +```py +def merge(arr, l, m, r): + n1 = m - l + 1 + n2 = r - m + + # Temporary arrays + L = arr[l:l + n1] + R = arr[m + 1:m + 1 + n2] + + i = j = k = 0 + + # Merge the temporary arrays back into arr[l..r] + while i < n1 and j < n2: + if L[i] <= R[j]: + arr[k + l] = L[i] + i += 1 + else: + arr[k + l] = R[j] + j += 1 + k += 1 + + # Copy remaining elements of L[], if any + while i < n1: + arr[k + l] = L[i] + i += 1 + k += 1 + + # Copy remaining elements of R[], if any + while j < n2: + arr[k + l] = R[j] + j += 1 + k += 1 + +def mergeSort(arr, l, r): + if l < r: + m = (l + r) // 2 + + # Sort first and second halves + mergeSort(arr, l, m) + mergeSort(arr, m + 1, r) + + merge(arr, l, m, r) + +# Usage: +arr = [38, 27, 43, 3, 9, 82, 10] +mergeSort(arr, 0, len(arr) - 1) +# Now arr is sorted: [3, 9, 10, 27, 38, 43, 82] + +``` \ No newline at end of file diff --git a/snippets/python/sorting/quick-sort.md b/snippets/python/sorting/quick-sort.md new file mode 100644 index 00000000..6b490df5 --- /dev/null +++ b/snippets/python/sorting/quick-sort.md @@ -0,0 +1,38 @@ +--- +title: Quick Sort +description: Sorts a list of integers using the Quick Sort algorithm. +author: utkarshkonwar +tags: sorting,quicksort,array,algorithm + +--- + + +```py +def partition(arr, low, high): + pivot = arr[high] # Pivot element + i = low - 1 + + for j in range(low, high): + if arr[j] < pivot: + i += 1 + # Swap arr[i] and arr[j] + arr[i], arr[j] = arr[j], arr[i] + + # Swap arr[i + 1] and arr[high] (pivot) + arr[i + 1], arr[high] = arr[high], arr[i + 1] + return i + 1 + +def quickSort(arr, low, high): + if low < high: + pi = partition(arr, low, high) + + # Recursively sort elements before and after partition + quickSort(arr, low, pi - 1) + quickSort(arr, pi + 1, high) + +# Usage: +arr = [10, 7, 8, 9, 1, 5] +quickSort(arr, 0, len(arr) - 1) +# Now arr is sorted: [1, 5, 7, 8, 9, 10] + +``` \ No newline at end of file diff --git a/snippets/python/sorting/selection-sort.md b/snippets/python/sorting/selection-sort.md new file mode 100644 index 00000000..4c0b27b9 --- /dev/null +++ b/snippets/python/sorting/selection-sort.md @@ -0,0 +1,27 @@ +--- +title: Selection Sort +description: Sorts a list of integers using the Selection Sort algorithm. +author: utkarshkonwar +tags: sorting,selectionsort,array,algorithm +--- + + +```py +def selectionSort(arr): + for i in range(len(arr) - 1): + minIdx = i + + # Find the minimum element in the unsorted part of the list + for j in range(i + 1, len(arr)): + if arr[j] < arr[minIdx]: + minIdx = j + + # Swap the found minimum element with the first element of the unsorted part + arr[i], arr[minIdx] = arr[minIdx], arr[i] + +# Usage: +arr = [64, 25, 12, 22, 11] +selectionSort(arr) +# Now arr is sorted: [11, 12, 22, 25, 64] + +``` \ No newline at end of file

AltStyle によって変換されたページ (->オリジナル) /