Organizing numbers

Hi, i am trying to reorganize the numbers so that the odds are first from smallest to largest and the evens are after from largest to smallest.
for example: {-19, 270, 76, -61, 54} would be {-61, -19, 270, 76, 54}

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
using namespace std;

void moveAndSortInt(int[], int);
void displayName(); 

int main() {
	
	int ary1[] = { -19, 270, 76, -61, 54 };

	int size = 5;

	int i;

	int ary2[] = {9, 8, -103, -73, 74, 53};

	int size2 = 6;

	int j;

	


	displayName();

	cout << endl;

	cout << "Original ary1[]" << endl;

	for (i = 0; i < size; i++) {
		cout << "  " << ary1[i] << " ";
	}

	cout << endl;
	cout << "\nCallingMoveAndSortInt() --\n " << endl;

	moveAndSortInt(ary1, size);

	cout << "Updated ary1[]" << endl;

	for (i = 0; i < size; i++) {
		cout << "  " << ary1[i] << " ";
	}

	cout << endl;

	cout << "\nOriginal ary2[]" << endl;

	for (j = 0; j < size2; j++) {
		cout << "  " << ary2[j] << " ";
	}

	cout << endl;

	cout << "\nCallingMoveAndSortInt() --\n" << endl;

	moveAndSortInt(ary2, size2);

	cout << "Updated ary2[]" << endl;

	for (j = 0; j < size2; j++) {
		cout << "  " << ary2[j] << " ";
   }
}


void moveAndSortInt(int ary[], int size) {
	int i, j;
	int temp;

	for (i = 0; i < 1 + size / 2; i++) {
		if (ary[i] % 2 == 0) {
			
			for (j = size - 1; j > size / 2; j--) {
				
				if (ary[j] % 2 != 0) {
					temp = ary[i]; 
					ary[i] = ary[j];
					ary[j] = temp; 
					
	                 j = 0;

				}
			}
		}
	}

	return;
}
I would (to start with) go through your values and put the odds into one vector and the evens into another vector, then sort, then concatenate.

Why are you using horrible c-style arrays?
My assignment says i must use arrays.
I am already able to separate the odds the evens but now i'm stuck on how to sorting the odds, smallest to largest and evens, largest to smallest
Last edited on
If you have all the odds in one array and all the evens in another, then just use any old sort then.
http://www.cplusplus.com/forum/general/127295/
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <iostream>
#include <iomanip>
#include <utility>  //swap
using namespace std;

void sort(int A[],int n)
{
    for(int i = 0; i < n; i++)
    {
        for(int j = i+1; j < n; j++)
        {
            if(A[0]%2 == 0) //even
            {
                 if(A[i] < A[j])
                 {
                     swap(A[i],A[j]);
                 }
            }
            else
            {
                if(A[i] > A[j])
                {
                    swap(A[i],A[j]);
                }
            }

        }

    }
}
void showArray(int A[], int n)
{
    for(int i = 0; i < n; i++)
       cout<<setw(5)<<A[i];
    cout<<endl;
}
int main()
{
    int array_1[] = { -19, 270, 76, -61, 54 };

    int n_odds = 0,n_evens = 0;
    int size_1 = sizeof(array_1)/sizeof(array_1[0]);

    for(int i = 0; i < size_1; i++)
    {
        (array_1[i]%2 == 0) ? ++n_evens : ++n_odds;
    }
    int array_2[n_evens]; //even
    int array_3[n_odds];    //odd

    for(int i = 0,j = 0,k = 0; i < size_1; i++)
    {
        (array_1[i]%2 == 0) ? array_2[j++] = array_1[i] : array_3[k++] = array_1[i];
    }

    cout<<"Originl array:\n";
    showArray(array_1,size_1);

    sort(array_2,n_evens);
    sort(array_3,n_odds);

    for(int i = 0; i < size_1; i++)
    {
        (i < n_odds) ? array_1[i] = array_3[i] : array_1[i] = array_2[i+1-n_evens];
    }

    cout<<"\nAfter sorting:\n";
    showArray(array_1,size_1);

    cin.ignore();
    return 0;
}

@shadowCODE C++ standard does not support variable lenght arrays. Your lines 48 and 49 are thus a syntax error.

Besides, additional arrays are not necessary:
http://www.cplusplus.com/reference/algorithm/partition/
1
2
3
auto bound = std::partition( myvector.begin(), myvector.end(), IsOdd );
std::sort( myvector.begin(), bound );
std::sort( bound, myvector.end(), std::greater<int>() );

You obviously cannot use these directly, but you can study the idea behind them.
@keskiverto i wrote the code in codeblocks and it compiled and ran successfully with 0 errors.
Besides, kni9ht says "My assignment says i must use arrays. .
Codeblocks probably uses GCC and GCC offers by default has some non-standard extensions, including VLA for C++. Not standard, not portable. The C++ standard method for dynamic-sized array is std::vector.


Practically every early assignment says must use brains. Use of all language features is tertiary. One can call std::partition and std::sort with array, but that is not the goal.

We, the caller of std::partition, have only one array. What does that function do? Does it use additional memory as workspace? It does not have to. You can peek the documentation in that link. You will see code. However, that could turn your assigment task from inventing a wheel to adapting a Ferrari's wheel into your bicycle. Different part of the brain.

You partition by copying to two new arrays. You could reserve three arrays of same constant size. You do count odds and evens as you copy them, so you will know how much of each array you do use. These arrays are tiny, so the waste of memory is insignificant; VLA is simply not needed.


What about the two calls to std::sort?

How can same algorithm sort differently? There is a fundamental trick there; we can give a function a helper function as parameter that changes what the primary function does. In this case we could simply have a sort function that takes a bool parameter to choose between ascending and descending.

There is more. Your sort operates on the first N elements of an array. The std::sort operates on range. Yet another useful concept to divert the already overloaded kni9ht.
@keskiverto, thanks for the insight on the code. This is a beginner forum. I kept it simple and precised as per the question.

I clearly understand the need for not using 2 separate arrays.

About the 2 calls to sort, there is a condition that handles the result to sorting in ascending or descending order.

Also, about the std::sort that operates on range, i choose to sort the first N elements because of my original concept of using 2 arrays.
Topic archived. No new replies allowed.