Question

In: Computer Science

Please solve this problem, in c++ (algoritms) write code and explaination how to solve it! 1)...

Please solve this problem, in c++ (algoritms) write code and explaination how to solve it!

1) N numbers are given. The first K of them are sorted in ascending order, the remaining NK are also sorted in ascending order. Sort numbers in ascending order in O (N).

Input data format N - number of numbers, K - number of numbers in the first half (sorted).

Example input

10 6

1 2 5 6 8 9 3 4 7 12

Sample Output

1 2 3 4 5 6 7 8 9 12

Solutions

Expert Solution

C++program execution starts with main() function. So define a main() function and declare required variables.

  1. Read n and k from user.
  2. Create an array, arr of size n to store input data
  3. Create another array, sorted of size n to store sorted items.
  4. Read n inputs to array arr.
  5. Initialize a=0 (a corresponds to index in first sorted half) and b= k (b corresponds to index in last sorted half.)
  6. For i varies from 0 to n-1,
  7. if b>=n or  (a<k and arr[a]<arr[b]), then sorted[i] = arr[a] and increment a.
  8. otherwise sorted[i] = arr[b] and increment b.
  9. At the end of execution array sorted will be having all sorted elements.
  10. Print elemets in sorted array.

Loop body has fixed number of statements and hence loop body has constant time complexity, and we are using only one for-loop for sorting, and i varies from 0 to n-1. Hence the complexity of sorting = O(N)

Program:


#include <iostream>
using namespace std;
int main()
{
    int n, k;                           /* declare variables */
    cin>>n>>k;                          /* read n and k */
    int arr[n];                         /* declare array arr to store input data */
    int sorted[n];                      /* declare array sorted to store sorted elements */
    for(int i=0;i<n;i++){               /* read n inputs */
        cin>>arr[i];
    }
    int a=0, b=k;                       /* initialize a=0 and b=k */
    
    for(int i=0;i<n;i++){               /*for i varies from 1 to n-1 */
        if(b>=n||a<k&&arr[a]<arr[b]){   /* if b>n or (a<k and arr[a]<arr[b]) */
            sorted[i] = arr[a];         /* add element from first half to sorted */
            a++;                        /* increment a */
        }
        else{                           /* otherwise add element from last half to sorted */
            sorted[i] = arr[b];
            b++;                        /* increment b */
        }
    }
    
    for(int i=0;i<n;i++){               /* print sorted array */
        cout<<sorted[i]<<" ";
    }
    return 0;
}

Screenshot:

Output:

Please don't forget to give a Thumbs Up.


Related Solutions

Please use C programming to write the code to solve the following problem. Also, please use...
Please use C programming to write the code to solve the following problem. Also, please use the instructions, functions, syntax and any other required part of the problem. Thanks in advance. Use these functions below especially: void inputStringFromUser(char *prompt, char *s, int arraySize); void songNameDuplicate(char *songName); void songNameFound(char *songName); void songNameNotFound(char *songName); void songNameDeleted(char *songName); void artistFound(char *artist); void artistNotFound(char *artist); void printMusicLibraryEmpty(void); void printMusicLibraryTitle(void); const int MAX_LENGTH = 1024; You will write a program that maintains information about your...
Please write code for C language Problem: Write a couple of functions to process arrays. Note...
Please write code for C language Problem: Write a couple of functions to process arrays. Note that from the description of the function you have to identify what would be the return type and what would be part of the parameter. display(): The function takes an int array and it’s size and prints the data in the array. sumArray(): It takes an int array and size, and returns the sum of the elements of the array. findMax(): It takes an...
PLEASE DO IN C++ AND USE REPL TO WRITE CODE The following problem statement is based...
PLEASE DO IN C++ AND USE REPL TO WRITE CODE The following problem statement is based on a problem in the C++ text by Friedman & Koffman: The results of a survey of the households in your township are available for public scrutiny. Each record (struct-type entity) contains input data for one household, including a four-digit integer identification number the annual income for the household the number of household members. Assuming that no more than 25 households were surveyed, write...
Explain your code with comments. Solve in C++. 1. Write a str2int() function that convers a...
Explain your code with comments. Solve in C++. 1. Write a str2int() function that convers a string to integer. The function will take the number as a string then will return it as integer. E.g. str2int(“123”) will return 123 str2int(“005”) will return 5 str2int(“102”) will return 102
Please solve the following problem for MATLAB Write a user-defined function (name it F to C)...
Please solve the following problem for MATLAB Write a user-defined function (name it F to C) that converts temperature in degrees F to temperature in degrees C. Use the function to solve the following problem. The change in the length of an object, ∆L, due to a change in the temperature, ∆T, is given by: ∆L = αL∆T, where a is the coefficient of thermal expansion. Determine the change in the area of a rectangular(4.5 m by 2.25m) aluminum (α=23·10-61/˚C)...
please write the code in C not c++, and not to use Atoi or parseint to...
please write the code in C not c++, and not to use Atoi or parseint to parse the string, Thank you. #include <stdio.h> #include <stdbool.h> /* * The isinteger() function examines the string given as its first * argument, and returns true if and only if the string represents a * well-formed integer. A well-formed integer consists only of an * optional leading - followed by one or more decimal digits. * Returns true if the given string represents an...
You cna hand write this if you want, Please code this in C Thank you PROBLEM...
You cna hand write this if you want, Please code this in C Thank you PROBLEM DESCRIPTION: Write a program to implement the following requirement: The program will read from standard input two things - a string str1 on the first line of stdin (this string may be an empty string) - a string str2 on the second line of stdin (this string may be an empty string) Note that stdin does not end with '\n'. The program will output...
Please answer with code for C language Problem: Counting Numbers Write a program that keeps taking...
Please answer with code for C language Problem: Counting Numbers Write a program that keeps taking integers until the user enters -100. In the end, the program should display the count of positive, negative (excluding that -100) and zeros entered. Sample Input/Output 1: Input the number: 0 2 3 -9 -6 -4 -100 Number of positive numbers: 2 Number of Negative numbers: 3 Number of Zero: 1
Define a problem utilizing Stacks. Write the design, code and display output. please in c++ oop?
Define a problem utilizing Stacks. Write the design, code and display output. please in c++ oop?
Write pseudo-code to solve the following problem using MapReduce and explain how it works. Each line...
Write pseudo-code to solve the following problem using MapReduce and explain how it works. Each line in the file lists a user ID, the ID of the movie the user watched, the rating the user gave for the movie, and the timestamp. For example line 1 indicates that the user’s ID is 196, the movie ID is 242, the user gave this movie a rating of 3, and the timestamp is 881250949. Given the file, find out the top similar...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT