Skip to content

Commit

Permalink
Merge pull request #70 from Somil-Shukla/Somil_Shukla-dsa-worksheet
Browse files Browse the repository at this point in the history
Search in Sorted Rotated Array in C
  • Loading branch information
lilmistake authored Oct 21, 2023
2 parents d6edb58 + d1f8e4c commit 5f57aae
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
58 changes: 58 additions & 0 deletions DSA_Codesheet/C/Search_Rotated_SortedArray.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include <stdio.h> // 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;
}
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,13 @@ Distributed under the MIT License. See `LICENSE` for more information.
<br />
<sub><b>Uday Sagar</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/Somil-Shukla">
<img src="https://avatars.githubusercontent.com/u/116343874?v=4" width="100;" alt="Somil-Shukla"/>
<br />
<sub><b>Somil Shukla</b></sub>
</a>
</td></tr>
</table>
<!-- readme: collaborators,contributors -end -->

0 comments on commit 5f57aae

Please sign in to comment.