Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

iterative and recursive implementation #115

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
76 changes: 49 additions & 27 deletions cpp/Binary Search.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,50 @@

"""binary search in c++"""

#include<iostream>
using namespace std;
int binarySearch(int arr[], int p, int r, int num) {
if (p <= r) {
int mid = (p + r)/2;
if (arr[mid] == num)
return mid ;
if (arr[mid] > num)
return binarySearch(arr, p, mid-1, num);
if (arr[mid] > num)
return binarySearch(arr, mid+1, r, num);
}
return -1;
}
int main(void) {
int arr[] = {1, 3, 7, 15, 18, 20, 25, 33, 36, 40};
int n = sizeof(arr)/ sizeof(arr[0]);
int num = 33;
int index = binarySearch (arr, 0, n-1, num);
if(index == -1)
cout<< num <<" is not present in the array";
else
cout<< num <<" is present at index "<< index <<" in the array";
return 0;
}

# iterative implementation of binary search in Python


def binary_search(a_list, item):
"""Performs iterative binary search to find the position of an integer in a given, sorted, list.
a_list -- sorted list of integers
item -- integer you are searching for the position of
"""

first = 0
last = len(a_list) - 1

while first <= last:
i = (first + last) / 2

if a_list[i] == item:
return '{item} found at position {i}'.format(item=item, i=i)
elif a_list[i] > item:
last = i - 1
elif a_list[i] < item:
first = i + 1
else:
return '{item} not found in the list'.format(item=item)


# recursive implementation of binary search in Python


def binary_search_recursive(a_list, item):
"""Performs recursive binary search of an integer in a given, sorted, list.
a_list -- sorted list of integers
item -- integer you are searching for the position of
"""

first = 0
last = len(a_list) - 1

if len(a_list) == 0:
return '{item} was not found in the list'.format(item=item)
else:
i = (first + last) // 2
if item == a_list[i]:
return '{item} found'.format(item=item)
else:
if a_list[i] < item:
return binary_search_recursive(a_list[i+1:], item)
else:
return binary_search_recursive(a_list[:i], item)
274 changes: 94 additions & 180 deletions cpp/CircularLinkedList.cpp
Original file line number Diff line number Diff line change
@@ -1,199 +1,113 @@
#include <iostream>
#include <cstdlib>

using namespace std;
using std::cout;
using std::cin;

struct node
{
int info;
struct node *next;
class IntNode{
public:
/* Constructors */
IntNode() {}
IntNode(int theData): data(theData), link(NULL) {}
IntNode(int theData, IntNode* theLink): data(theData), link(theLink) {}
~IntNode() {} //Destructor

/* Accessors */
int getData() const { return data; }
IntNode* getLink() const { return link; }

/* Mutators */
void setData(int theData) { data = theData; }
void setLink(IntNode* pointer) { link = pointer; }

private:
int data;
IntNode *link;
};

node *last = nullptr;
typedef IntNode* IntNodePtr;

void Display()
{
struct node *s;
if (last == nullptr)
{
cout << "List is empty, nothing to display" << endl;
return;
}
s = last->next;
cout << "Circular Link List: " << endl;
while (s != last)
{
cout << s->info << "->";
s = s->next;
}
cout << s->info << endl;
}
/* Creates a circular linked list containing "suitors" number of nodes */
IntNodePtr lineUpSuitors(int suitors);

void createNode(int value)
{
struct node *temp;
temp = new (struct node);
temp->info = value;
if (last == nullptr)
{
last = temp;
temp->next = last;
}
else
{
temp->next = last->next;
last->next = temp;
last = temp;
}
Display();
}
/* Output linked list. Because this demonstration concerns circular lists
this function halts output when the pointer of the final element
points back to the first. This function also works for single nodes
that refer to themselves. */
void outputList(IntNodePtr head);

void addBegin(int value)
{
if (last == nullptr)
{
cout << "First Create the list." << endl;
return;
}
struct node *temp;
temp = new (struct node);
temp->info = value;
temp->next = last->next;
last->next = temp;
/* Remove and delete every third node in the circular linked list until
one remains. */
IntNodePtr eliminateSuitors(IntNodePtr);

Display();
int main(){
int numSuitors = 0;

/* Prompt for suitors */
cout << "\nHow many suitors (in integers) does Eve have: ";
cin >> numSuitors;

/* Create circular linked list */
IntNodePtr head = lineUpSuitors(numSuitors);

/* Confirm accuracy of linked list */
cout << "\nThe suitors are lined up by number: ";
outputList(head);

/* Eliminate every third suitor until one remains and output result */
head = eliminateSuitors(head);
cout << "\nEve eliminates every third suitor (starting at 1)"
<< " and selects suitor: ";
outputList(head);

delete head; //Discard final node

cout << "\n"; //Extra space for readability
return 1;
}

void addAfter(int value, int pos)
{
if (last == nullptr)
{
cout << "First Create the list." << endl;
return;
/* Creates a circular linked list containing "suitors" number of nodes */
IntNodePtr lineUpSuitors(int suitors){
IntNodePtr head = new IntNode(1), current = head;

for(int i = 2; i <= suitors; i++){
current->setLink(new IntNode(i));
current = current->getLink();
}
struct node *temp, *s;
s = last->next;
for (int i = 0; i < pos - 1; i++)
{
s = s->next;
if (s == last->next)
{
cout << "There are less than ";
cout << pos << " in the list" << endl;
return;
}
}
temp = new (struct node);
temp->next = s->next;
temp->info = value;
s->next = temp;

if (s == last)
{
last = temp;
}
Display();

current->setLink(head);

return head;
}

void Delete(int value)
{
struct node *temp, *s;
s = last->next;

if (last->next == last && last->info == value)
{
temp = last;
last = nullptr;
free(temp);
Display();
return;
}
if (s->info == value)
{
temp = s;
last->next = s->next;
free(temp);
Display();
return;
}
while (s->next != last)
{

if (s->next->info == value)
{
temp = s->next;
s->next = temp->next;
free(temp);
cout << "Element " << value;
cout << " deleted from the list" << endl;
Display();
return;
}
s = s->next;
}

if (s->next->info == value)
{
temp = s->next;
s->next = last->next;
free(temp);
last = s;
Display();
return;
/* Output linked list. Because this demonstration concerns circular lists
this function halts output when the pointer of the final element
points back to the first. This function also works for single nodes
that refer to themselves. */
void outputList(IntNodePtr head){
IntNodePtr current = head;

cout << current->getData();

while(current->getLink() != head){
current = current->getLink();
cout << current->getData();
}
cout << "Element " << value << " not found in the list" << endl;

cout << "\n"; //Extra space for readability
}

int main()
{
int choice, element, position;

while (1)
{
cout << "1.Create Node" << endl;
cout << "2.Add at beginning" << endl;
cout << "3.Add after a position" << endl;
cout << "4.Delete" << endl;
cout << "5.Quit" << endl;
cout << "Enter your choice : ";
cin >> choice;
switch (choice)
{
case 1:
cout << "Enter the element: ";
cin >> element;
createNode(element);
cout << endl;
break;
case 2:
cout << "Enter the element: ";
cin >> element;
addBegin(element);
cout << endl;
break;
case 3:
cout << "Enter the element: ";
cin >> element;
cout << "Insert element after position: ";
cin >> position;
addAfter(element, position);
cout << endl;
break;
case 4:
if (last == nullptr)
{
cout << "List is empty, nothing to delete" << endl;
break;
}
cout << "Enter the element for deletion: ";
cin >> element;
Delete(element);
cout << endl;
break;
case 5:
exit(0);
default:
cout << "Wrong choice" << endl;
}
/* Remove and delete every third node in the circular linked list until
one remains. */
IntNodePtr eliminateSuitors(IntNodePtr head){
IntNodePtr current = head->getLink(), next = current->getLink();

while(current->getLink() != current){
current->setLink(next->getLink());
delete next;
head = current->getLink();
current = head->getLink();
next = current->getLink();
}

return current;
}
Loading