diff --git a/DSA_Codesheet/C/Search_Rotated_SortedArray.cpp b/DSA_Codesheet/C/Search_Rotated_SortedArray.cpp new file mode 100644 index 0000000..770b809 --- /dev/null +++ b/DSA_Codesheet/C/Search_Rotated_SortedArray.cpp @@ -0,0 +1,58 @@ +#include // Include the standard input/output library. + +// Function to search for a target element in a rotated and sorted array. +int searchInRotatedSortedArray(int rotatedSortedArray[], int n, int target) { + int low = 0, high = n - 1; // Initialize low and high pointers. + + while (low <= high) { // Perform a binary search in the array. + int mid = (low + high) / 2; // Calculate the middle index of the current search range. + + if (rotatedSortedArray[mid] == target) { // If the middle element is the target, return its index. + return mid; + } + + if (rotatedSortedArray[low] <= rotatedSortedArray[mid]) { + // If the left half is sorted (no rotation in this part): + if (rotatedSortedArray[low] <= target && target < rotatedSortedArray[mid]) { + high = mid - 1; // Adjust the high pointer to search in the left half. + } else { + low = mid + 1; // Adjust the low pointer to search in the right half. + } + } else { + // If the right half is sorted (no rotation in this part): + if (rotatedSortedArray[mid] < target && target <= rotatedSortedArray[high]) { + low = mid + 1; // Adjust the low pointer to search in the right half. + } else { + high = mid - 1; // Adjust the high pointer to search in the left half. + } + } + } + + return -1; // If the target element is not found, return -1. +} + +int main() { + printf("Enter the size of the array: "); + int n; + scanf("%d", &n); // Read the size of the array from the user. + + int rotatedSortedArray[n]; + printf("Enter the elements of the rotated sorted array: "); + for (int i = 0; i < n; ++i) { + scanf("%d", &rotatedSortedArray[i]); // Read each element from the user and store it in the array. + } + + int target; + printf("Enter the target element to search: "); + scanf("%d", &target); // Read the target element from the user. + + int result = searchInRotatedSortedArray(rotatedSortedArray, n, target); // Call the search function. + + if (result != -1) { + printf("Element %d found at index %d\n", target, result); // Output the result if the target is found. + } else { + printf("Element %d not found in the array\n", target); // Output a message if the target is not found. + } + + return 0; +} diff --git a/DSA_Codesheet/Python/merge_sort.py b/DSA_Codesheet/Python/merge_sort.py deleted file mode 100644 index 050fbc7..0000000 --- a/DSA_Codesheet/Python/merge_sort.py +++ /dev/null @@ -1,45 +0,0 @@ -def merge_sort(arr): - # Base case: If the list has 0 or 1 elements, it is already sorted - if len(arr) <= 1: - return arr - - # Divide the input list into two halves - middle = len(arr) // 2 - left_half = arr[:middle] - right_half = arr[middle:] - - # Recursively sort the two halves - left_half = merge_sort(left_half) - right_half = merge_sort(right_half) - - # Merge the sorted halves - return merge(left_half, right_half) - -def merge(left, right): - result = [] - left_idx, right_idx = 0, 0 - - # Compare and merge elements from the left and right halves - while left_idx < len(left) and right_idx < len(right): - if left[left_idx] < right[right_idx]: - result.append(left[left_idx]) - left_idx += 1 - else: - result.append(right[right_idx]) - right_idx += 1 - - # Append any remaining elements from the left and right halves - result.extend(left[left_idx:]) - result.extend(right[right_idx:]) - return result - -# Input: Size of the list -n = int(input()) -# Input: List of integers -arr = list(map(int, input().split())) - -# Perform Merge Sort -sorted_arr = merge_sort(arr) - -# Output: Sorted list of integers -print(" ".join(map(str, sorted_arr)))