From 08cba51fb38e276062b0c9791d7e0ee494ffa903 Mon Sep 17 00:00:00 2001 From: Yash9276 <123647814+Yash9276@users.noreply.github.com> Date: Fri, 27 Oct 2023 14:47:30 +0530 Subject: [PATCH] Added Insertion sort, palindrome code as well as Subset generator code (#88) * added InsertionSort sort * added Palindrome.sol which checks whether steing is palindrome or not * added Subset.sol which finds total number of subsets in array * Update src/Maths/Palindrome.sol --------- Co-authored-by: @Yash9276 Co-authored-by: Maxime Kubik <50140834+mkubdev@users.noreply.github.com> --- src/Maths/Palindrome.sol | 24 ++++++++++++++++++++++ src/Maths/Subset.sol | 33 ++++++++++++++++++++++++++++++ src/Sorts/InsertionSort.sol | 40 +++++++++++++++++++++++++++++++++++++ 3 files changed, 97 insertions(+) create mode 100644 src/Maths/Palindrome.sol create mode 100644 src/Maths/Subset.sol create mode 100644 src/Sorts/InsertionSort.sol diff --git a/src/Maths/Palindrome.sol b/src/Maths/Palindrome.sol new file mode 100644 index 0000000..fc656fb --- /dev/null +++ b/src/Maths/Palindrome.sol @@ -0,0 +1,24 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +contract PalindromeChecker { + // Function to check if a given string is a palindrome + function isPalindrome(string memory str) public pure returns (bool) { + // Convert the input string to a bytes array + bytes memory bytesStr = bytes(str); + // Get the length of the string + uint256 len = bytesStr.length; + + // Loop through the first half of the string + for (uint256 i = 0; i < len / 2; i++) { + // Compare characters from the start and end of the string + if (bytesStr[i] != bytesStr[len - 1 - i]) { + // If characters don't match, return false (not a palindrome) + return false; + } + } + + // If the loop completes without finding mismatches, return true (a palindrome) + return true; + } +} diff --git a/src/Maths/Subset.sol b/src/Maths/Subset.sol new file mode 100644 index 0000000..6f51f84 --- /dev/null +++ b/src/Maths/Subset.sol @@ -0,0 +1,33 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +contract SubsetGenerator { + // Function to generate all possible subsets of an input array + function generateSubsets(uint[] memory arr) public pure returns (uint[][] memory) { + uint n = arr.length; // Get the length of the input array + uint[][] memory subsets = new uint[][](2**n); // Initialize an array to store subsets + + // Loop through all possible binary combinations + for (uint i = 0; i < 2**n; i++) { + uint[] memory subset = new uint[](n); // Create an array for the current subset + uint count = 0; // Counter for the number of elements in the subset + + // Loop through the bits of the current binary combination + for (uint j = 0; j < n; j++) { + // Check if the j-th bit of i is set to 1 + if ((i & (1 << j)) != 0) { + subset[count] = arr[j]; // Include the element in the subset + count++; // Increment the count of elements in the subset + } + } + + // Create a new array to store the final subset (with only the necessary elements) + subsets[i] = new uint[](count); + for (uint k = 0; k < count; k++) { + subsets[i][k] = subset[k]; // Copy the elements to the final subset + } + } + + return subsets; // Return the array of subsets + } +} diff --git a/src/Sorts/InsertionSort.sol b/src/Sorts/InsertionSort.sol new file mode 100644 index 0000000..fb73388 --- /dev/null +++ b/src/Sorts/InsertionSort.sol @@ -0,0 +1,40 @@ +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.0; + +/** + * @title InsertionSOrt + * @author [Yash9276](https://github.com/Yash9276) + * @notice A contract that implements the insertion sort algorithm. + * @dev It repeatedly takes an element from the unsorted part and inserts + * it into its correct position in the sorted part of the array, +*shifting the elements if necessary. It has an average and worst-case time complexity of O(n^2) but +* is efficient for small datasets or partially sorted data. + + */ + +contract InsertionSort { + /* @notice Sorts the given array in increasing order. + @param input The array to sort. + @return output The resulting sorted array in increasing order + */ + + function sort(uint256[] memory myArray) + public + pure + returns (uint256[] memory) + { + uint256 n = myArray.length; + for (uint256 i = 1; i < n; i++) { + uint256 key = myArray[i]; + int256 j = int256(i - 1); + + while (j >= 0 && int256(myArray[uint256(j)]) > int256(key)) { + myArray[uint256(j + 1)] = myArray[uint256(j)]; + j--; + } + + myArray[uint256(j + 1)] = key; + } + return myArray; + } +}