Question

a) 70 marks] Write a program that Sorts a given integer array via Selection Sort obtained by rotation around 3. Searches for

PLEASE write in C Language, ( you can use array, recursive function, Pointers, Dynamic call by refernce, Structure)

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

if you have any further queries please feel free to ask

a) /////////////////program to sort array using selection sort in C/////////////

Write a program that Sorts a given integer array using Selection Sort

#include <stdio.h>

void swap(int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}
void selectionSort(int data[], int n)
{
    int i, j, index;
    for (i = 0; i < n-1; i++)
    {
        // Find the minimum element in unsorted array
        index = i;
        for (j = i+1; j < n; j++)
          if (data[j] < data[index])
            index = j;

        // Swap the minimum element with the first element
        swap(&data[index], &data[i]);
    }
}

// main program to test above functions
int main()
{   int n,i;
    int arr[30];
    printf("enter the no: of elements: \n");
    scanf("%d",&n);
    printf("enter the elements: \n");
    for(i=0;i<n;i++)
    scanf("%d",&arr[i]);
    selectionSort(arr, n);
    printf("Sorted array: \n");
    for (i=0; i < n; i++)
    printf("%d ", arr[i]);
    printf("\n");
    return 0;
}

4 5 3 2 1 19 22 0 2 3 Smain enter the no: of elements: enter the elements: Sorted array: 0 1 2 2 334 5 19 22

/////////////////////////////////////////////////

b) rotate the sorted array around a random point

#include <stdio.h>

void swap(int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}
void selectionSort(int data[], int n)
{
    int i, j, index;
    for (i = 0; i < n-1; i++)
    {
        // Find the minimum element in unsorted array
        index = i;
        for (j = i+1; j < n; j++)
          if (data[j] < data[index])
            index = j;

        // Swap the minimum element with the first element
        swap(&data[index], &data[i]);
    }
}

/* Function to print an array */
void print(int data[], int s)
{
    int i;
    for (i=0; i < s; i++)
        printf("%d ", data[i]);
    printf("\n");
}

/* rotate array to left of size n by d*/
void leftRotate(int arr[], int d, int n)
{
    int i;
    for (i = 0; i < d; i++)
        {
          int temp = arr[0], i;
          for (i = 0; i < n - 1; i++)
          arr[i] = arr[i + 1];
         arr[i] = temp;
        }
      
}

// main program to test above functions
int main()
{   int n,i,pos;
    int arr[30];
    printf("enter the no: of elements: \n");
    scanf("%d",&n);
    printf("enter the elements: \n");
    for(i=0;i<n;i++)
    scanf("%d",&arr[i]);
    selectionSort(arr, n);
    printf("Sorted array: \n");
   print(arr, n);
   printf("enter the position of element from the sorted list where rotation is needed: \n");
   scanf("%d",&pos);
   leftRotate(arr, pos, n);

   print(arr, n);
    return 0;
}

Sgcc-o main .C Şmain enter the no: of elements: 10 enter the elements: 4 5 3 2 1 19 22 0 2 3 Sorted array: 0 1 2 2 334 5 19 2

c) Searches for a key point in the rotated array in O(logn) time, where the rotation point is known in advance

to search in O(logn) we can use binary search since the array is sorted

program to perform selection sort,rotate the sorted array and search element from the sorted array using binary search which is of order O(logn)

#include <stdio.h>

void swap(int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}
void selectionSort(int data[], int n)
{
    int i, j, index;
    for (i = 0; i < n-1; i++)
    {
        // Find the minimum element in unsorted array
        index = i;
        for (j = i+1; j < n; j++)
          if (data[j] < data[index])
            index = j;

        // Swap the minimum element with the first element
        swap(&data[index], &data[i]);
    }
}



/* Function to print an array */
void print(int data[], int s)
{
    int i;
    for (i=0; i < s; i++)
        printf("%d ", data[i]);
    printf("\n");
}

/*Function to left rotate arr[] of size n by d*/
void leftRotate(int arr[], int d, int n)
{
    int i;
    for (i = 0; i < d; i++)
        {
          int temp = arr[0], i;
          for (i = 0; i < n - 1; i++)
          arr[i] = arr[i + 1];
         arr[i] = temp;
        }
      
}

int search(int arr[], int l, int h, int key)
{
    if (l > h) return -1;

    int mid = (l+h)/2;
    if (arr[mid] == key) return mid;

    /* If arr[l...mid] is sorted */
    if (arr[l] <= arr[mid])
    {
        /* As this subarray is sorted, we can quickly
        check if key lies in half or other half */
        if (key >= arr[l] && key <= arr[mid])
        return search(arr, l, mid-1, key);

        return search(arr, mid+1, h, key);
    }

    if (key >= arr[mid] && key <= arr[h])
        return search(arr, mid+1, h, key);

    return search(arr, l, mid-1, key);
  
}

// main program to test above functions
int main()
{   int n,i,j,pos,key;
    int arr[30];
    printf("enter the no: of elements: \n");
    scanf("%d",&n);
    printf("enter the elements: \n");
    for(i=0;i<n;i++)
    scanf("%d",&arr[i]);
    selectionSort(arr, n);
    printf("Sorted array: \n");
   print(arr, n);
   printf("enter the position of element from the sorted list where rotation is needed: \n");
   scanf("%d",&pos);
   leftRotate(arr, pos, n);

   print(arr, n);
   printf("enter the element to be searched: \n");
   scanf("%d",&key);
   j = search(arr, 0, n-1, key);

    if (j != -1)
   printf("Index of the element is : %d", j);
   else
   printf("Index of the element is not found");

    return 0;
}

Smain enter the no: of elements: 1O enter the elements: Sorted array: 0 1 2 2 33 4 5 19 22 enter the position of element from

d) Repeat above by first finding the unknown rotation point via sequential search and binary search (you forget the random rotation point and re-compute it)

#include <stdio.h>

void swap(int *a, int *b)
{
    int temp = *a;
    *a = *b;
    *b = temp;
}
void selectionSort(int data[], int n)
{
    int i, j, index;
    for (i = 0; i < n-1; i++)
    {
        // Find the minimum element in unsorted array
        index = i;
        for (j = i+1; j < n; j++)
          if (data[j] < data[index])
            index = j;

        // Swap the minimum element with the first element
        swap(&data[index], &data[i]);
    }
}



/* Function to print an array */
void print(int data[], int s)
{
    int i;
    for (i=0; i < s; i++)
        printf("%d ", data[i]);
    printf("\n");
}

/*Function to left rotate arr[] of size n by d*/
void leftRotate(int arr[], int d, int n)
{
    int i;
    for (i = 0; i < d; i++)
        {
          int temp = arr[0], i;
          for (i = 0; i < n - 1; i++)
          arr[i] = arr[i + 1];
         arr[i] = temp;
        }
      
}

int search(int arr[], int l, int h, int key)
{
    if (l > h) return -1;

    int mid = (l+h)/2;
    if (arr[mid] == key) return mid;

    /* If arr[l...mid] is sorted */
    if (arr[l] <= arr[mid])
    {
        /* As this subarray is sorted, we can quickly
        check if key lies in half or other half */
        if (key >= arr[l] && key <= arr[mid])
        return search(arr, l, mid-1, key);

        return search(arr, mid+1, h, key);
    }

    if (key >= arr[mid] && key <= arr[h])
        return search(arr, mid+1, h, key);

    return search(arr, l, mid-1, key);
  
}

// main program to test above functions
int main()
{   int n,i,j,element,pos,key;
    int arr[30];
    printf("enter the no: of elements: \n");
    scanf("%d",&n);
    printf("enter the elements: \n");
    for(i=0;i<n;i++)
    scanf("%d",&arr[i]);
    selectionSort(arr, n);
    printf("Sorted array: \n");
   print(arr, n);
   printf("enter the element from the sorted list where rotation is needed: \n");
   scanf("%d",&element);
   //sequential search to find the position of the element in the sorted array
   for(i=0;i<n;i++)
   {
   if(arr[i]== element)
   pos=i;
   }
   leftRotate(arr, pos, n);

   print(arr, n);
   printf("enter the element to be searched: \n");
   scanf("%d",&key);
   j = search(arr, 0, n-1, key);

    if (j != -1)
   printf("Index of the element is : %d", j);
   else
   printf("Index of the element is not found");

    return 0;
}

T

Smain enter the no: of elements 10 enter the elements: 45 3 2 1 19 22 0 2 3 Sorted array: 0 1 2 2 33 4 5 19 22 enter the elem

Add a comment
Know the answer?
Add Answer to:
PLEASE write in C Language, ( you can use array, recursive function, Pointers, Dynamic call by re...
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
  • (Use C programming language) 1-)Write a program that • Sorts a given integer array via Selection...

    (Use C programming language) 1-)Write a program that • Sorts a given integer array via Selection Sort • Rotates the sorted array around a random point, e.g., 1 2 3 4 5 à 3 4 5 1 2 is obtained by rotation around 3. • Searches for a key point in the rotated array in O(logn) time, where the rotation point is known in advance. • Repeats above by first finding the unknown rotation point via sequential search and binary...

  • please help with this c++ question! Write a function that returns a dynamic array, of size...

    please help with this c++ question! Write a function that returns a dynamic array, of size n, composed of sequential odd integers starting from 1: 1,3,5,7, etc.. int* odd_array(int n) {

  • In C language Write a program that includes a function search() that finds the index of...

    In C language Write a program that includes a function search() that finds the index of the first element of an input array that contains the value specified. n is the size of the array. If no element of the array contains the value, then the function should return -1. The program takes an int array, the number of elements in the array, and the value that it searches for. The main function takes input, calls the search()function, and displays...

  • Just Q3 and Q4 Q1] Write a C function to implement the binary search algorithm over...

    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 should return the index of the search key if the search key exists and return - 1 if the search key doesn't exist. [10 Points] Q2] Write a C function to implement the selection sort algorithm, to sort an array of float values and size n. The function should sort the array in ascending...

  • In C++, develop a class that supports array rotation. Rotating an array is an operation where...

    In C++, develop a class that supports array rotation. Rotating an array is an operation where you shift all elements of the array some number of positions left or right, and elements that are shifted off of the left or right end of the array "wrap around" to the right or left end, respectively. For example, if we rotate the array [1, 2, 3, 4, 5] to the right by 1, we get the array [5, 1, 2, 3, 4]....

  • Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write...

    Remove srand(time(NULL)); from this C++ code so that it still finds random numbers correctly. Then, Write a program that adds the following to the fixed code. • Add a function that will use the BubbleSort method to put the numbers in ascending order. – Send the function the array. – Send the function the size of the array. – The sorted array will be sent back through the parameter list, so the data type of the function will be void....

  • C++ Object Oriented assignment Can you please check the program written below if it has appropriately...

    C++ Object Oriented assignment Can you please check the program written below if it has appropriately fulfilled the instructions provided below. Please do the necessary change that this program may need. I am expecting to get a full credit for this assignment so put your effort to correct and help the program have the most efficient algorithm within the scope of the instruction given. INSTRUCTIONS Create a fraction class and add your Name to the name fraction and use this...

  • CAN SOMEONE COMPLETE THIS CODE PLEASE THNK YOU!! Question - Write a program in c++ that...

    CAN SOMEONE COMPLETE THIS CODE PLEASE THNK YOU!! Question - Write a program in c++ that keeps an appointment book. Make a class Appointment that stores a description of the appointment, the appointment day, the starting time, and the ending time. Your program should keep the appointments in a sorted vector. Users can add appointments and print out all appointments for a given day. When a new appointment is added, use binary search to find where it should be inserted...

  • In below C++ sort.cpp 1- Implement the insertion_sort function. 2- Implement the compareSensorPtr function and the...

    In below C++ sort.cpp 1- Implement the insertion_sort function. 2- Implement the compareSensorPtr function and the code in main to create and sort the array of pointers. The places to make modifications are indicated by TODO: comments. You should not have to make modifications anywhere else. 3- what's big O and runtime for 100000 items. #include <iostream> #include <algorithm> #include <numeric> #include <vector> #include <string> #include <cstdlib> #include <cassert> using namespace std; // Set this to false to skip the...

  • Posted this a few hours ago. Can anyone help with this? IN C++ Perform the following:...

    Posted this a few hours ago. Can anyone help with this? IN C++ Perform the following: Read 10 bowlers with 3 scores each into 1 string array and 1 numeric array(2 dimension double array) Sort the data by individual bowlers averages, Don't forget to sort their names also. Calculate the average across and store in your existing array. Calculate the average down and store in your existing array. Print out the contents of both arrays. This will print the averages...

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