Question

Just Q3 and Q4

Q1] Write a C function to implement the binary search algorithm over an array of integer numbers and size n. The function shoRules and Conditions The IsPartiallySoreted() MUST have the header int IsPartiallySoreted(int size, int data[], int m, ... ),

0 0
Add a comment Improve this question Transcribed image text
Answer #1

below c code is complete implementation of all questions.

- x C:\Users\Shivraj kumar\Desktop\che imperfectSorted.exe solution for question number 4 Enter the size of array Enter the

#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>

void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}

int search(int arr[], int l, int r, int x)
{
if (r >= l)
{
int mid = l + (r - l) / 2;
if (arr[mid] == x)
{
return mid;
}
if (arr[mid] > x)
{
return search(arr, l, mid - 1, x);
}

else
{
return search(arr, mid + 1, r, x);
}

}
return -1;
}
void selectionSort(int arr[], int n)
{
int i, j, index;
for (i = 0; i < n-1; i++)
{

index = i;
for (j = i+1; j < n; j++)
{
if (arr[j] < arr[index])
{
index = j;
}

}
swap(&arr[index], &arr[i]);
}
}
int IsPartiallySorted(int high,int m ,int a[],int arr[],int low,int plow)
{
if(low<=high)
{
int index=search(arr,plow,high,a[low]);
if(abs(index-low)<=m)
{
return IsPartiallySorted(high,m,a,arr,low+1,plow);
}
else
{

return 0;
}
}
return 1;
}
int* EnterArrayData(int n)
{
int i;
int *a;
a=(int*)malloc(sizeof(int)*n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
return a;
}
void PrintArrayData(int *a,int n)
{
int i;
for(i=0;i<n;i++)
{
printf("%d ",a[i]);
}
printf("\n");
}
int main()
{
printf("solution for question number 4 \n");
int n,i,j;
printf("Enter the size of array\n");
scanf("%d",&n);
int *a,arr[n];
printf("Enter the array\n");
a=EnterArrayData(n); //takes input from array
printf("\n");
PrintArrayData(a,n);
for(i=0;i<n;i++)
{
arr[i]=a[i];
}
int m;
printf("Enter the degree of array in m\n");
scanf("%d",&m);

selectionSort(arr,n); //selection sort
printf(" testing question number 3\n");
int check =IsPartiallySorted(n-1,m,a,arr,0,0); //method to check partially sorted via recursion
if(check)
{
printf("yes array is partially sorted with degree m\n");
}
else
{

printf("no array is partially sorted with degree m\n");
}

printf(" testing question 1 , question 2\n");
printf("array are printed in sorted order\n");
PrintArrayData(arr,n);

printf("searching by binary search\n");
int p;
printf("enter the value want to search\n");
scanf("%d",&p);
int s=search(a,0,n-1,p);
if(s>=0)
{
printf("%d is present at index %d ",p,s);
}
else
{

printf("%d is not present in array and it return %d",p,s);
}


return 0;
}

Add a comment
Know the answer?
Add Answer to:
Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • You are going to implement Treesort algorithm in C++ to sort string data. Here are the...

    You are going to implement Treesort algorithm in C++ to sort string data. Here are the steps to complete the homework 1) Use the following class definition for binary search tree nodes. Its constructor is incomplete you should first complete the constructor. class TreeNode t public: string data; / this is the string stored in the node TreeNode left: TreeNode right; TreeNode (string element, TreeNode 1t, TreeNode rt //your code here 2) Write a function that will insert a string...

  • You are making a .h file. Implement a recursive binary search function. bSearch passes in an...

    You are making a .h file. Implement a recursive binary search function. bSearch passes in an array of integers, the size of the array, and the value the function is searching for. The function should return the index where found or -1 if not found. You will want to implement a recursive helper function that passes in the array, the value to be located, and the beginning and ending of the range of values to search within. Use the provided...

  • Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template...

    Objective: 1. Understand sorting algorithm 2. Implement bubble sort in C++ Check slides for a template of the solution, if you need Write a program that asks users to input 10 integers into an array, write a function that takes the array as its argument, use bubble sort to sort the array and output the sorted array in increasing order. #include <iostream> using namespace std; C:IWINDOWSSystems2cmd.exe lease input 10 integers: void bubblesort(int a[10]) int help; int b[10]; for (int i...

  • Question 10 15 pts In this question, you are asked to implement a recursive function that...

    Question 10 15 pts In this question, you are asked to implement a recursive function that checks whether two increasingly sorted arrays of integers are disjoint or not (two arrays are disjoint iff they share no common elements). Your function needs to follow the following signature: int is Disjoint (int" sorted 1, int size 1, int* sorted 2, int size2) Here is the description of the input and output parameters of is Disjoint: • int" sorted 1: pointer to the...

  • (a) Write a program in Java to implement a recursive search function int terSearch(int A[], int...

    (a) Write a program in Java to implement a recursive search function int terSearch(int A[], int l, int r, int x) that returns the location of x in a given sorted array of n integers A if x is present, otherwise -1. The terSearch search function, unlike the binary search, must consider two dividing points int d1 = l + (r - l)/3 int d2 = d1 + (r - l)/3 For the first call of your recursive search function...

  • Write and test a function in C++ that uses the binary search algorithm to search an...

    Write and test a function in C++ that uses the binary search algorithm to search an array of sorted strings – use a do..while loop to allow user to perform multiple searches w/o terminating the program – see sample output below. Use this name array: string names[SIZE] = { "Collins, Bill", "Smith, Bart", "Allen, Jim",         "Griffin, Jim", "Stamey, Marty", "Rose, Geri", "Taylor, Terri",         "Johnson, Jill", "Allison, Jeff", "Looney, Joe", "Wolfe, Bill",         "James, Jean", "Weaver, Jim", "Pore, Bob",...

  • Write a C function that implements the Liner Search algorithm instead of Binary Search. However, this...

    Write a C function that implements the Liner Search algorithm instead of Binary Search. However, this linear search algorithm returns the indices of the longest sorted subset of numbers in an array of integers of size n. The longest sorted subset of numbers must satisfy three conditions. First, the subset consists of unique numbers (no duplicate); second, all the numbers in this subset is divisible by m, the minimum size of the subset is two elements. In the main method...

  • JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration...

    JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration skill Problem description: Write the following eight methods and write a main function to test these methods // return the index of the first occurrence of key in arr // if key is not found in arra, return -1 public static int linearSearch(int arr[], int key) // sort the arr from least to largest by using select sort algorithm public stati void selectSort(int arr[])...

  • C programming (you don't need to write program) Problem 1 [Linear Search with Early Stop] Below...

    C programming (you don't need to write program) Problem 1 [Linear Search with Early Stop] Below you will find a linear search function with early stop. A linear search is just a naive search - you go through each of the elements of a list one by one. Early stop works only on sorted list. Early stop means, instead of going through whole list, we will stop when your number to search can no longer be possibly found in the...

  • create a file homework_part_1.c a) Implement the function initialize_array that receives two parameters: an array of...

    create a file homework_part_1.c a) Implement the function initialize_array that receives two parameters: an array of integers and the array size. Use a for loop and an if statement to put 0s in the odd positions of the array and 5s in the even positions. You must use pointers to work with the array. Hint: review pointers as parameters. b) Implement the function print_array that receives as parameters an array of integers and the array size. Use a for statements...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT