Question

Consider an array of length n containing positive and negative integers in random. Write a C++...

Consider an array of length n containing positive and negative integers in random. Write a C++ code that rearranges the integers so that the negative integers appear before the positive integers. Your solution should use:

a. O(n^2)

b. O(n)

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

a) O(n2)

#include <iostream>

#include <stdio.h>

using namespace std;  

void printfunc(int array[], int n)        // function to print the array

{

    for (int i = 0; i < n; i++)

        printf("%d ", array[i]);

       

    printf("\n");   

}

void arrange(int array[], int n)   // Function to Rearrange positive and negative

{

    int current_element, j;

    for(int i = 1; i < n; i++)

    {

      current_element= array[i];   

        if (current_element > 0) // current element is positive just continue

            continue;

        j = i - 1;              

        while (j >= 0 && array[j] > 0) //to rearrange the elements of array      

        {

            array[j + 1] = array[j];

            j = j - 1;

        }

        array[j + 1] = current_element;

    }

}

int main()

{

    int array[] = { -8, -1, 3, 11, -4, -14, 6, 6, 28 };

    int n = sizeof(array) / sizeof(array[0]);

    printf("initial array   :\t");

    printfunc(array,n);

    arrange(array, n);

    printf("rearranged array:\t");

    printfunc(array, n);

    return 0;

}

C++14 (Gcc 6.3) 5 void printtunc (int arrayint n) // function to print the array for (int i-0: 1 <n: 1++) printf(%d , array

b) O(n)

#include <iostream>

#include <bits/stdc++.h>

using namespace std;

void printfunc(int array[], int n)        // function to print the array

{

    for (int i = 0; i < n; i++)

        printf("%d ", array[i]);

       

    printf("\n");   

}

void arrange(int array[], int n)         // function to rearrange

{

    int j = 0;

    for (int i = 0; i < n; i++) {       

        if (array[i] < 0) {             // to check whether array[i] i.e., array element is negative or positive

            if (i != j)

                swap(array[i], array[j]); // rearange the array elements

            j++;

        }

    }

}

int main()                               //main function

{

    int array[] = { -8, -1, 3, 11, -4, -14, 6, 6, 28 };

    int n = sizeof(array) / sizeof(array[0]);

    printf("initial array   :\t");

    printfunc(array,n);

    arrange(array, n);

    printf("rearranged array:\t");

    printfunc(array, n);

    return 0;

}

C++14 (Gcc 6.3) #include <iostream> #include <bits/stdc++.h> 3 using nanespace std 2 6 void printfunc (int array[], int n) //

Add a comment
Know the answer?
Add Answer to:
Consider an array of length n containing positive and negative integers in random. Write a C++...
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
  • Consider an array of length n containing positive and negative integers in random order. Write C++...

    Consider an array of length n containing positive and negative integers in random order. Write C++ code that rearranges the integers so that the negative integers appear before the positive integers. Your solution should use O(n2) operations O(n) operations

  • Consider the array called myArray declared below that contains positive V and negative integers. Write a...

    Consider the array called myArray declared below that contains positive V and negative integers. Write a loop statement to count and display the number of positive integers in the array. That is, your loop should display the message "Number of positive integers is 5". int myArray[]={-1,3,-9,9,33,-4,-5,100,4,-23};

  • Let A be an array containing n numbers (positive and negative). Develop a divide and conquer algo...

    need help in this algorithm question Let A be an array containing n numbers (positive and negative). Develop a divide and conquer algorithm that finds the two indices 1 sisjsn such that A[k] (the sum of the elements from i to j) is maximized. For example, in the array A [10,-5,-6,5, 7,-2,4, -11], the sub-array A[4:6] has the sum 5+ 7-2+4-14 and no other sub-array contains elements that sum to a value greater than 14, so for this input the...

  • Write a recursive C program that receives an array of positive integers and its length and...

    Write a recursive C program that receives an array of positive integers and its length and returns the largest number in the array. For example: int arr[5] = {-2, 31, 4, 12, -8}; findMax(arr, ...) -> 31 int findMax ( int * arr , int p , int max ) { }

  • Suppose you are given an array A holding n distinct integers (negative values are allowed) in...

    Suppose you are given an array A holding n distinct integers (negative values are allowed) in sorted order; in other words, A[i] < A[i + 1] for each i ∈ [0, n − 2]. We say the ith element is self referential if A[i] = i. Design an O(log n) time algorithm to determine if there is a self referencial element in the array. Your solution must include a) Statement of your algorithm in plain English. (Pseudo-code is optional.) b)...

  • in matlab (a)Write a function to generate n random integers between -b and b. (b) Using...

    in matlab (a)Write a function to generate n random integers between -b and b. (b) Using the function you wrote in (a) generate 50 random numbers in a vector A where b = 100; (c) This array A will contain both negative, positive and possibly zero entries. Write a MATLAB script to compute the percentage of positive, negative and zero entries in the array.

  • 2) Given an array of n nonzero real numbers a[0]…a[n-1], write a function to partition the...

    2) Given an array of n nonzero real numbers a[0]…a[n-1], write a function to partition the array (not sort) so that all its negative elements come before all its positive elements. Your algorithm should have O(n) time complexity. The function prototype is void negpospartition(float a[], int n) Please use C language

  • Given an array A of integers, -100,000 <= A[i] <= 100,000, 1 <= 10,000 (n =...

    Given an array A of integers, -100,000 <= A[i] <= 100,000, 1 <= 10,000 (n = size of array), some elements appear multiple times and others appear once. write a function that returns all the elements that appear multiple times in this array. (solution should be O(n) or faster) public static List<Integer> find duplicates (List<Integer> nums) { }

  • Write a C Program that inputs an array of integers from the user along-with the length...

    Write a C Program that inputs an array of integers from the user along-with the length of array. The program then prints out the array of integers in reverse. (You do not need to change (re-assign) the elements in the array, only print the array in reverse.) The program uses a function call with array passed as call by reference. Part of the Program has been given here. You need to complete the Program by writing the function. #include<stdio.h> void...

  • Write an application that does the following: (1) fill a 32-bit array with 10 pseudo-random integers...

    Write an application that does the following: (1) fill a 32-bit array with 10 pseudo-random integers between -50 and +49 ; (2) Loop through the array, displaying each value, and count the number of negative values; (3) After the loop finishes, display the count. Below is an assembly program template that you can refer, and complete the programming by filling in the codes into the specified place: Comment ! Title: Counting Array Values Description: Write an application that does the...

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