What's new

# C & C++Patulong mga ka PHC Bubble sort in c++ with a twist

#### b4byshark

##### Forum Veteran
Good day PHC!
Pwedi po patulong regarding dito nagawa ko naman na yang bubble sort though di ko makuha kung pano ko imomove yung zero and negative numbers while still yung non zero or positive numbers naka sort pa din sa ascending orders.
Thank you po sa tutulong.

#### Attachments

Good day!

You basically just need to have 2 array. 1 for positive integers and another for your zero and negative integers.
And then have 1 final array to iterate those two array into the final array.
Then to get the index, nag lagay lang ako ng Boolean to catch the first negative/0 integer input.

Sa picture, the index variable output should be 2.

This is not the best way to write this but I wrote it this way para maintindihan mo yung flow and logic.

I will do it like this: (I used vector on this para mabilis. Pero nag nilagay ko pa rin jan yung logic to sort it on variable arr[] for you to understand it.)
You do not have permission to view the full content of this post. Log in or register now.

C++:
``````#include <iostream>
#include <Windows.h>
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;

#define MAX 100

int main()
{
//array declaration
int arr[MAX], finalOutput[MAX] = {};
int n, i, j;
int temp;

//read total number of elements/integers to read
cout << "Enter total number of elements to read: ";
cin >> n;

//check range
if (n<0 || n>MAX)
{
cout << "Input valid range!!!" << endl;
return -1;
}

//input n elements/integers and split into two arrays
int positiveCounter = 0, negativeCounter = 0;
//int positiveInput[], negativeInput[];
vector<int> positiveInput, negativeInput;

//declare index point
int index = 0;
bool indexCheck = false;
for (i = 0; i < n; i++)
{
int input;
cout << "Enter element [" << i + 1 << "] ";
cin >> input;
arr[i] = input;
if (input > 0) { positiveInput.push_back(input); positiveCounter++; }
if (input <= 0) {
if (indexCheck == false) { index = i; indexCheck = true; }
negativeInput.push_back(input);
negativeCounter++;
}

}

//print input elements
cout << "Unsorted Array elements:" << endl;
for (i = 0; i < n; i++)
cout << arr[i] << "\t";
cout << endl;

//sorting logic
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (arr[i] > arr[j])
{
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
}

sort(positiveInput.begin(), positiveInput.end());

int counter = 0;
for (i = 0; i < positiveInput.size(); i++)
{

finalOutput[i] = positiveInput[i];
counter++;
}

for (i = 0; i < negativeInput.size(); i++)
{
finalOutput[counter] = negativeInput[i];
counter++;
}

//print sorted array elements
cout << "Sorted (Ascending Order) Array elements:" << endl;
for (i = 0; i < n; i++)
cout << finalOutput[i] << "\t";
cout << endl;

cout << "Index of the first zero/negative number in the array: ";
cout << index;

return 0;

}``````

Let me know kung nakatulong.

Members online
1,237
Guests online
1,419
Total visitors
2,656