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
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