Question 1,2,4 :-
-----------------------------------
Please check comments in Programme
#include
/**
* Method for Linear Search
* Loop through given array and search for given key
* IF found return index and stop
* else -1 will be returned at the end
*
*/
int search(int array[],int key,int size){
int index = -1;
for (int c = 0; c < size; c++)
{
if (array[c] == key) /* if required element found */
{
printf(\"%d is present at location %d.\ \", key, c+1);
index = c;
break;
}
}
if(index == -1)
printf(\"%d is not found.\ \", key);
return index;
}
/**
* Function for Binary search
* Binary search is used only for sorted arrays
* Check key with element at middle position
* if key == then return middle positon
* if key > middle position , apply same for sub array range from [middle...last]
* if key < middle position , apply same for sub array range from [0..middle]
*
*/
int binary_search(int array[],int key,int start,int end){
int index = -1;
int first = start;
int last = end;
int middle = (first+last)/2;
while (first <= last) {
//Print array between first nad last for each step
printf(\"Start binary search step.\ \");
for(int i = first ; i < last ; i++){
printf(\"Binary search step element.%d\ \",array[i]);
}
printf(\"Stop binary search step.\ \");
if (array[middle] < key)
first = middle + 1;
else if (array[middle] == key) {
index = middle;
printf(\"%d found at location %d.\ \", key, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
return index;
}
/**
* Recursive linear search
* Instead of for loop call same method with next index
* first pass 0, check if key found at index 0, if not found call same method with index 1 .....
*/
int linear_search_recursive(int array[],int key,int size,int index){
if(array[index] == key){
printf(\"%d found at location %d.\ \",key, index);
return index;
}
else if (index >= size-1){
return -1;
}
else{
return linear_search_recursive(array,key,size,++index);
}
}
/**
* Recursive Binary Search
* Recursively call Binary Search , check key found at element middle
* if key == then return middle positon
* if key > middle position , call recursive method for sub array range from [middle...last]
* if key < middle position , call recursive method for sub array range from [0..middle]
*/
int binary_search_recursive(int array[],int key,int start,int end){
//Key not found
if(start> end){
return -1;
}
int index = -1;
int first = start;
int last = end;
int middle = (first+last)/2;
//Print array between first nad last for each step
printf(\"Start binary search step.\ \");
for(int i = first ; i < last ; i++){
printf(\"Binary search step element.%d\ \",array[i]);
}
printf(\"Stop binary search step.\ \");
if (array[middle] < key){
first = middle + 1;
return binary_search_recursive(array,key,first,last) ;
}else if (array[middle] == key) {
printf(\"%d found at location %d.\ \", key, middle+1);
return middle;
}
else{
last = middle - 1;
return binary_search_recursive(array,key,first,last) ;
}
return -1;
}
/**
*Function t.
1. Question 1,2,4 :-
-----------------------------------
Please check comments in Programme
#include
/**
* Method for Linear Search
* Loop through given array and search for given key
* IF found return index and stop
* else -1 will be returned at the end
*
*/
int search(int array[],int key,int size){
int index = -1;
for (int c = 0; c < size; c++)
{
if (array[c] == key) /* if required element found */
{
printf("%d is present at location %d. ", key, c+1);
index = c;
break;
}
}
if(index == -1)
printf("%d is not found. ", key);
return index;
}
/**
* Function for Binary search
* Binary search is used only for sorted arrays
* Check key with element at middle position
* if key == then return middle positon
* if key > middle position , apply same for sub array range from [middle...last]
* if key < middle position , apply same for sub array range from [0..middle]
*
*/
2. int binary_search(int array[],int key,int start,int end){
int index = -1;
int first = start;
int last = end;
int middle = (first+last)/2;
while (first <= last) {
//Print array between first nad last for each step
printf("Start binary search step. ");
for(int i = first ; i < last ; i++){
printf("Binary search step element.%d ",array[i]);
}
printf("Stop binary search step. ");
if (array[middle] < key)
first = middle + 1;
else if (array[middle] == key) {
index = middle;
printf("%d found at location %d. ", key, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
return index;
}
/**
* Recursive linear search
* Instead of for loop call same method with next index
* first pass 0, check if key found at index 0, if not found call same method with index 1 .....
*/
int linear_search_recursive(int array[],int key,int size,int index){
if(array[index] == key){
printf("%d found at location %d. ",key, index);
return index;
}
else if (index >= size-1){
3. return -1;
}
else{
return linear_search_recursive(array,key,size,++index);
}
}
/**
* Recursive Binary Search
* Recursively call Binary Search , check key found at element middle
* if key == then return middle positon
* if key > middle position , call recursive method for sub array range from [middle...last]
* if key < middle position , call recursive method for sub array range from [0..middle]
*/
int binary_search_recursive(int array[],int key,int start,int end){
//Key not found
if(start> end){
return -1;
}
int index = -1;
int first = start;
int last = end;
int middle = (first+last)/2;
//Print array between first nad last for each step
printf("Start binary search step. ");
for(int i = first ; i < last ; i++){
printf("Binary search step element.%d ",array[i]);
}
printf("Stop binary search step. ");
if (array[middle] < key){
first = middle + 1;
return binary_search_recursive(array,key,first,last) ;
}else if (array[middle] == key) {
printf("%d found at location %d. ", key, middle+1);
return middle;
}
else{
4. last = middle - 1;
return binary_search_recursive(array,key,first,last) ;
}
return -1;
}
/**
*Function to sort the array
* for Binary search we have to sort array first
*/
void sort(int number[], int size){
for (int i = 0; i < size; ++i)
{
for (int j = i + 1; j < size; ++j)
{
if (number[i] > number[j])
{
int a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
}
int main(int argc, char *argv[])
{
int range = 100;
int data[100];
for(int i = 0 ; i < 100 ; i++){
int item = rand() % range + 1;
data[i] = item;
printf("array item %d : %d ",i,item);
}
sort(data,range);
//search(data,120,100);
//binary_search(data,41,0,99);
//linear_search_recursive(data,41,100,0);
5. binary_search_recursive(data,91,0,99);
return 0;
}
Question 3 :-
-------------------------------
To test with 1000 elements
change the variable "range" , assign 1000 to it
worst Time Complexity of Linear search is alwasy O(n), becuase we have to lop throug all
elements in array
For Binary search it is O(n/2) , but array shuld be sorted first
Time complexity for Binaru Searc = Time Complexity ofSorting + Time complexity of Search
Question 5 (Using Linked List):-
--------------------------------
#include
#include
#include
#include
struct node
{
int data;
int key;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
//display the list
void printList()
{
struct node *ptr = head;
printf(" [ ");
//start from the beginning
while(ptr != NULL)
{
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
6. }
printf(" ]");
}
//insert link at the first location
void insertFirst(int key, int data)
{
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
if(current == NULL){
head = link;
current = head;
}
else{
//append after head
current->next = link;
current = link;
}
}
//is list empty
bool isEmpty()
{
return head == NULL;
}
int length()
{
int length = 0;
struct node *current;
for(current = head; current != NULL; current = current->next)
{
length++;
}
return length;
}
//find a link with given key
7. struct node* find(int key){
int index = -1;
//start from the first link
struct node* cur = head;
//if list is empty
if(head == NULL)
{
return NULL;
}
//navigate through list
while( cur != NULL ){
if(cur->data == key){
index = cur->key;
break;
}
//if it is last node
if(cur->next == NULL){
index = -1;
break;
}else {
//go to next link
cur = cur->next;
}
}
if(index == -1){
printf("element not found");
}
else{
printf("element found at %d", index);
}
//if data found, return the current Link
return current;
}
int main(){
int range = 100;
for(int i = 0 ; i < 100 ; i++){
8. int item = rand() % range + 1;
insertFirst(i,item);
printf("array item %d : %d ",i,item);
}
struct node* search = find(84);
return 0;
}
Solution
Question 1,2,4 :-
-----------------------------------
Please check comments in Programme
#include
/**
* Method for Linear Search
* Loop through given array and search for given key
* IF found return index and stop
* else -1 will be returned at the end
*
*/
int search(int array[],int key,int size){
int index = -1;
for (int c = 0; c < size; c++)
{
if (array[c] == key) /* if required element found */
{
printf("%d is present at location %d. ", key, c+1);
index = c;
break;
}
}
if(index == -1)
printf("%d is not found. ", key);
return index;
}
9. /**
* Function for Binary search
* Binary search is used only for sorted arrays
* Check key with element at middle position
* if key == then return middle positon
* if key > middle position , apply same for sub array range from [middle...last]
* if key < middle position , apply same for sub array range from [0..middle]
*
*/
int binary_search(int array[],int key,int start,int end){
int index = -1;
int first = start;
int last = end;
int middle = (first+last)/2;
while (first <= last) {
//Print array between first nad last for each step
printf("Start binary search step. ");
for(int i = first ; i < last ; i++){
printf("Binary search step element.%d ",array[i]);
}
printf("Stop binary search step. ");
if (array[middle] < key)
first = middle + 1;
else if (array[middle] == key) {
index = middle;
printf("%d found at location %d. ", key, middle+1);
break;
}
else
last = middle - 1;
middle = (first + last)/2;
}
return index;
}
/**
* Recursive linear search
10. * Instead of for loop call same method with next index
* first pass 0, check if key found at index 0, if not found call same method with index 1 .....
*/
int linear_search_recursive(int array[],int key,int size,int index){
if(array[index] == key){
printf("%d found at location %d. ",key, index);
return index;
}
else if (index >= size-1){
return -1;
}
else{
return linear_search_recursive(array,key,size,++index);
}
}
/**
* Recursive Binary Search
* Recursively call Binary Search , check key found at element middle
* if key == then return middle positon
* if key > middle position , call recursive method for sub array range from [middle...last]
* if key < middle position , call recursive method for sub array range from [0..middle]
*/
int binary_search_recursive(int array[],int key,int start,int end){
//Key not found
if(start> end){
return -1;
}
int index = -1;
int first = start;
int last = end;
int middle = (first+last)/2;
//Print array between first nad last for each step
printf("Start binary search step. ");
for(int i = first ; i < last ; i++){
printf("Binary search step element.%d ",array[i]);
}
11. printf("Stop binary search step. ");
if (array[middle] < key){
first = middle + 1;
return binary_search_recursive(array,key,first,last) ;
}else if (array[middle] == key) {
printf("%d found at location %d. ", key, middle+1);
return middle;
}
else{
last = middle - 1;
return binary_search_recursive(array,key,first,last) ;
}
return -1;
}
/**
*Function to sort the array
* for Binary search we have to sort array first
*/
void sort(int number[], int size){
for (int i = 0; i < size; ++i)
{
for (int j = i + 1; j < size; ++j)
{
if (number[i] > number[j])
{
int a = number[i];
number[i] = number[j];
number[j] = a;
}
}
}
}
int main(int argc, char *argv[])
{
int range = 100;
int data[100];
12. for(int i = 0 ; i < 100 ; i++){
int item = rand() % range + 1;
data[i] = item;
printf("array item %d : %d ",i,item);
}
sort(data,range);
//search(data,120,100);
//binary_search(data,41,0,99);
//linear_search_recursive(data,41,100,0);
binary_search_recursive(data,91,0,99);
return 0;
}
Question 3 :-
-------------------------------
To test with 1000 elements
change the variable "range" , assign 1000 to it
worst Time Complexity of Linear search is alwasy O(n), becuase we have to lop throug all
elements in array
For Binary search it is O(n/2) , but array shuld be sorted first
Time complexity for Binaru Searc = Time Complexity ofSorting + Time complexity of Search
Question 5 (Using Linked List):-
--------------------------------
#include
#include
#include
#include
struct node
{
int data;
int key;
struct node *next;
};
struct node *head = NULL;
struct node *current = NULL;
//display the list
13. void printList()
{
struct node *ptr = head;
printf(" [ ");
//start from the beginning
while(ptr != NULL)
{
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
}
printf(" ]");
}
//insert link at the first location
void insertFirst(int key, int data)
{
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
link->key = key;
link->data = data;
if(current == NULL){
head = link;
current = head;
}
else{
//append after head
current->next = link;
current = link;
}
}
//is list empty
bool isEmpty()
{
return head == NULL;
}
int length()
{
14. int length = 0;
struct node *current;
for(current = head; current != NULL; current = current->next)
{
length++;
}
return length;
}
//find a link with given key
struct node* find(int key){
int index = -1;
//start from the first link
struct node* cur = head;
//if list is empty
if(head == NULL)
{
return NULL;
}
//navigate through list
while( cur != NULL ){
if(cur->data == key){
index = cur->key;
break;
}
//if it is last node
if(cur->next == NULL){
index = -1;
break;
}else {
//go to next link
cur = cur->next;
}
}
if(index == -1){
printf("element not found");
}
15. else{
printf("element found at %d", index);
}
//if data found, return the current Link
return current;
}
int main(){
int range = 100;
for(int i = 0 ; i < 100 ; i++){
int item = rand() % range + 1;
insertFirst(i,item);
printf("array item %d : %d ",i,item);
}
struct node* search = find(84);
return 0;
}