Ascending order HELP.

May 12, 2017 at 6:46pm
Hello guys, I really want your help cause im confused. So what i need to do is to write a program that randomise array and after that the first numbers need to be odds and on ascending order, after them the numbers need to be even and again on ascending order. Here is an example of an input "7 2 5 1 8 4" and the expected output need to be "1 5 7 2 4 8" , Im getting "1 5 7 8 4 2".
Here is my code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>

using namespace std;

int main(){
    int n[6];
	for(int i=0; i<6; i++)
        cin>>n[i];
    for (int i = 0; i < 6; i++){
         cout << n[i] << " ";
	}
	cout<<endl;
	for(int i=1;i<=6-1;i++){
	    for(int j=0;j<6-i;j++){
	        if(n[j]>n[j+1]){
                int s=n[j+1];
                n[j+1]=n[j];
                n[j]=s;
	        }
	        if(n[j]%2==0){
                int e=n[j+1];
                n[j+1]=n[j];
                n[j]=e;
	        }
	    }
	}
	for(int k=0;k<6;k++){
	    cout<<n[k]<<" ";
	}
}
Last edited on May 12, 2017 at 6:55pm
May 12, 2017 at 7:23pm
If you can use them, the algorithms std::sort() and std::stable_partition() will do what you want. If you cannot, then take a look at the reference implementations of each for hints about how to make it work.
Topic archived. No new replies allowed.