Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions contributors.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
# List of all contributors


1. [Bhrigu Kansra](https://github.com/kinetickansra)
2. [Tathya Thaker](https://github.com/thetathya)
3. [Aslan](https://github.com/impeccableaslan)
4. [Naman Manchanda](https://github.com/namanmanchanda09)
5. [Harsh Arora](https://github.com/aroraharsh010)
6. [Ujjval Patel](https://github.com/Ujjval-Patel)
7. [Shaguna Awasthi](https://github.com/Shagunaawasthi)
8. [Joy Banerjee](https://github.com/joybanerjee08)
9. [Ambika](https://github.com/Ambika55)
10. [Shivam Yadav](https://github.com/shivamyadav2512)
11. [Nikhil Arora](https://github.com/nikhilarora06

12. [Your Name](https://github.com/yourprofile)


1. [Bhrigu Kansra](https://github.com/kinetickansra)
2. [Tathya Thaker](https://github.com/thetathya)
3. [Aslan](https://github.com/impeccableaslan)
4. [Naman Manchanda](https://github.com/namanmanchanda09)
5. [Harsh Arora](https://github.com/aroraharsh010)
6. [Ujjval Patel](https://github.com/Ujjval-Patel)
7. [Shaguna Awasthi](https://github.com/Shagunaawasthi)
8. [Joy Banerjee](https://github.com/joybanerjee08)
9. [Kartikey Tripathi](https://github.com/kartikeytripathi)
62 changes: 62 additions & 0 deletions searching/binary_search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Binary Search
-----------
A searching algorithm that finds the position of a target value
within a sorted array.

Time complexity
---------------
O(log(N)), where N is the number of elements in the array.

Space complexity
----------------
O(1).
*/

#include <iostream>
#include <vector>

using namespace std;

int binarySearch(const int value, const vector<int>& sortedVect, const int low, const int high) {
int mid = (low + high) / 2;

if (value == sortedVect[mid])
return mid;
else if (low <= high) {
if (value < sortedVect[mid]) {
// value must be between indices low and mid-1, if exists
return binarySearch(value, sortedVect, low, mid-1);
}
else if (value > sortedVect[mid]) {
// value must be between indices mid-1 and high, if exists
return binarySearch(value, sortedVect, mid+1, high);
}
}

return -1;
}

int main() {
int value;
cout << "Enter the value to search for : ";
cin >> value;

int size;
cout << "Enter the input size : ";
cin >> size;

vector<int> inputVect(size); // supposedly sorted values
cout << "Enter " << size << " integers in ascending order :\n";
for (int& val: inputVect)
cin >> val;

int index = binarySearch(value, inputVect, 0, size-1);
cout << "\n";
if (index != -1)
cout << "Found " << value << " at position " << (index + 1) << "\n";
else
cout << "Either " << value << " is not present among the input values, or the values aren\'t sorted\n";

return 0;
}
104 changes: 104 additions & 0 deletions searching/ternary_search.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
Ternary search
--------------
A searching algorithm that finds the position of a maximum(minimum) value
within an unimodal array.

Time complexity
---------------
O(log(N)), where N is the number of elements in the array.

Space complexity
----------------
O(1).
*/

#include <iostream>
#include <vector>

using namespace std;

enum Pattern {
ASCEND_THEN_DESCEND,
DESCEND_THEN_ASCEND
};

/*
ternary_search
-------------
If the values first ascend and then descend, this function finds the position
of the maximum value. Otherwise, if they first descend and then ascend, it
finds the position of the minimum value.
*/
template <typename T>
size_t ternary_search(const vector<T>& values, const Pattern& pattern) {
// left and right are the edges of the interval of search
size_t left = 0;
size_t right = values.size() - 1;

bool changed = true;
size_t mid1, mid2;
while (right - left > 1 and changed) { // if the interval is not shrinking,
// its size already equals O(1)
changed = false;
mid1 = left + (right - left) / 3;
mid2 = right - (right - left) / 3;
if ((pattern and values[mid1] < values[mid2])
or (!pattern and values[mid1] > values[mid2])) {
changed |= (right != mid2);
right = mid2;
}
else {
changed |= (left != mid1);
left = mid1;
}
}

T min_value = values[left];
T max_value = values[left];
size_t min_index = left;
size_t max_index = left;
for (size_t index = left + 1; index <= right; index++) {
if (min_value > values[index]) {
min_value = values[index];
min_index = index;
}
if (max_value < values[index]) {
max_value = values[index];
max_index = index;
}
}

return pattern == ASCEND_THEN_DESCEND ? max_index : min_index;
}

#ifndef TERNARY_SEARCH_TEST
int main() {
int size;
cout << "Enter the input size : ";
cin >> size;

vector <int> input_values(size); //supposedly unimodal array
cout << "Enter " << size << " values:\n";
for (int& value: input_values)
cin >> value;

string answer;
cout << "\nDo the values first ascend then descend?"
<< "If otherwise, enter \"no\".\n"
<< "[Y]es / [n]o : ";
getline(cin, answer);

Pattern pattern = ASCEND_THEN_DESCEND; // answer is "yes" by default
if (answer[0] == 'n' or answer[0] == 'N') // user answers "no"
pattern = Pattern::DESCEND_THEN_ASCEND;

int index = ternary_search(input_values, pattern);
if (pattern == ASCEND_THEN_DESCEND)
cout << "Maximum value has position " << index << "\n";
else
cout << "Minimum value has position " << index << "\n";

return 0;
}
#endif