Question

In: Computer Science

Write program that pass unsorted one dimensional array and a key to function, the function will...

Write program that pass unsorted one dimensional array and a key to function,
the function will search for the key(using linear search) and if it is found the
function will sort the array from the beginning to the position of the key, and f
it is not found the function will sort all the array
Note: please solve it by c , not c++

Solutions

Expert Solution

Please find the program below in C language:

#include<stdio.h>

//Function to swap the value of two variables
void swap(int* first, int* second)
{
int temp = *first;
*first = *second;
*second = temp;
}
  
// Function to perform Selection Sort
void selectionSort(int array[], int no)
{
int i, j, min;

for (i = 0; i < no - 1; i++) {   
// Find the minimum element in unsorted array
min = i;
for (j = i + 1; j < no; j++)
if (array[j] < array[min])
min = j;
  
// Swap the found minimum element with the first element
swap(&array[min], &array[i]);
}
}

// Function to implement search operation
int findElement(int array[], int no, int key)
{
int i;
for (i = 0; i < no; i++)
if (array[i] == key)
return i;
  
return -1;
}

// Function to print an array
void printArray(int arr[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", arr[i]);
printf("\n");
}
  
// Driver Code
int main()
{
int arr[] = {12, 34, 10, 6, 40}; /*Array elements. You can change these values according to your requirement*/
int n = sizeof(arr) / sizeof(arr[0]);
   int key, position;
  
printf("Enter the key value you want to search: ");
scanf("%d",&key); //Take input of the key value you want to search
  
position = findElement(arr, n, key); //To find if the key value is present in array
  
if (position == - 1) //If key value is not found
selectionSort(arr,n); //Sort the full array
else //If key value is found
selectionSort(arr,position+1); //Sort the array upto the position of key value
      
   printArray(arr,n); //Print the array
  
return 0;
}

Snapshot of the code output:

1. When the key value is present in the array:

2. When the key value is not present in the array:


Related Solutions

One dimensional dynamic array Write a function that returns the number of integers in an input...
One dimensional dynamic array Write a function that returns the number of integers in an input file stream with the following interface: int findNumber(ifstream &x); Then, use this number to dynamically allocate an integer array. Write another function that reads each number in an input file stream and assign the value to the corresponding array element with the following interface: void assignNumber(ifstream &x, int y[ ]); In your main( ), first open “in.dat” as an input file. Next, apply findNumber(...
Python: Write a function that receives a one dimensional array of integers and returns a Python...
Python: Write a function that receives a one dimensional array of integers and returns a Python tuple with two values - minimum and maximum values in the input array. You may assume that the input array will contain only integers and will have at least one element. You do not need to check for those conditions. Restrictions: No built-in Python data structures are allowed (lists, dictionaries etc). OK to use a Python tuple to store and return the result. Below...
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers...
Write a program in Java to do the following: -Create a one-dimensional array of 7 integers as follows: Assign {35,20,-43,-10,6,7,13} -Create a one dimensional array of 7 Boolean values as follows: Assign {true,false,false,true,false,true,false} -Create a one dimensional array of 7 floating-point values as follows: Assign {12.0f,1.5f,-3.5f,-2.54f,3.4f,45.34f,22.13f} -Declare sum as integer and set it to 0. -Declare sumf as float and set it to 0.0f. -Use a for loop to go through each element of the Boolean array, and if an...
Q1) Write a program to implement the quick sort algorithm in a one dimensional array? Q2)...
Q1) Write a program to implement the quick sort algorithm in a one dimensional array? Q2) Write a program to implement the merge sort algorithm in a one dimensional array?
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test...
C++ ASSIGNMENT: Two-dimensional array Problem Write a program that create a two-dimensional array initialized with test data. The program should have the following functions: getTotal - This function should accept two-dimensional array as its argument and return the total of all the values in the array. getAverage - This function should accept a two-dimensional array as its argument and return the average of values in the array. getRowTotal - This function should accept a two-dimensional array as its first argument...
C Programming Only Write a program that declares a one-dimensional array of integers with 24 elements....
C Programming Only Write a program that declares a one-dimensional array of integers with 24 elements. Fill the array with random integers (use a loop). Neatly output each element in the one-dimensional array. Next convert your one-dimensional array of 24 elements into a two-dimensional array of 6 x 4 elements. Neatly output each element of the two-dimensional array. The values will be identical to the one-dimensional array – you’re just converting from one dimension to two.
In C Write a program to read a one-dimensional array, print sum of all elements using...
In C Write a program to read a one-dimensional array, print sum of all elements using Dynamic Memory Allocation.
Write a function to sort data (in increasing order) in an array using a. pass by...
Write a function to sort data (in increasing order) in an array using a. pass by value b. and pass by reference.
Write Matrix Addition 2 D (dimensional) Array program in c++.
Write Matrix Addition 2 D (dimensional) Array program in c++.
Write a program that creates a two-dimensional array initialized with test data. The program should have...
Write a program that creates a two-dimensional array initialized with test data. The program should have the following functions: Hi There I really appreciate your help with this project. ▪ getTotal . This function should accept a two-dimensional array as its argument and return the total of all the values in the array. ▪ getAverage . This function should accept a two-dimensional array as its argument and return the average of all the values in the array. ▪ getRowTotal ....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT