Ordering an array into ascending evens/descending odds

So I'm not very proud to ask a question without using any code,but it has to be done.

So I would like to sort an array into ascending evens and descending odds.

For exemple,if I have initialize an array

myArray={0,1,2,3,4,5,6,7,8,9}
,
after the sorting,myArray would be:

myArray={0,2,4,6,8,9,7,5,3,1}

I was thinking maybe I could split my array into odds and even using some sort of Divide et Impera (the problem is that I dont know if it's possible,and if it is,well...I cant figure out the algorithm),and then QuickSort the odds and the evens..

Can you halp,cplusplus.com members?
Last edited on
I would use std::sort with a custom comparator:
http://www.cplusplus.com/reference/algorithm/sort/
Let me know if you can't figure it out, but all the comparator has to do is make evens come before odds and make evens sort normally and odds sort in reverse. It will be one boolean expression ;)
I cant use std::sort..
It's pretty much a homework and well..I'm supposed to learn the algorithm logic,not the syntax,I guess.
Do you know how to write your own sorting algorithm? Start with that first and then change it to sort like this instead.
Last edited on
I was looking for an efficient way,not for just a way.
I don't know what you want me to tell you. If you cannot use std::sort and you find yourself incapable of writing a sorting algorithm that you consider efficient enough for your desires, what do you expect me to say?
It is the efficient way.

The OP mentions QuickSort with implication that the student (1) can write it and (2) considers it to be efficient.

What L B did suggest was that you write that QuickSort. In the algorithm you will compare the elements. You will probably write (a < b). Replace that expression with the comparator mentioned by L B.

[Edit] I cannot remember what tricks QuickSort uses, so maybe it isn't the optimal, but since OP must have studied variaous sorting algorithms, he knows the features of the alternatives.
Last edited on
I realize the topic I started is pretty vague.

I'm sorry for crowding up the forum,I really am..

I can write a QuickSort,I just wasn't sure how do the odd/even splitting thing,that's what I was searching for.

Sorry for bothering, cplusplus.com.
I just wasn't sure how do the odd/even splitting thing

Are you now sure, i.e. how do you see the L B's suggestion now?
No,I'm not,but I haven't got the time to code until now. I will probably figure it out.

I respect everyone's suggestions equally. Just the fact that they took the time to write it down it's something I respect.

I'll start coding now and post what I come up with to see if you have any suggestions on improving my code.
Last edited on
OK. This is what I came up with:

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

#include <iostream>

using namespace std;

	int * myArray;
	int n;
	int x;

bool ascending(int left,int right){
	return left<right;
}

bool descending(int left,int right){
	return left>right;
}

int pivot(int myArray[],int left,int right,bool (*comparision)(int,int));
int firstSort(int myArray[],int left,int right);
void quickSort(int myArray[],int left,int right,bool (*comparision)(int,int));
void init();

int main(){
	init();
	x=firstSort(myArray,0,n-1);
	quickSort(myArray,0,x-1,ascending);
	quickSort(myArray,x,n-1,descending);

	for(int i=0;i<n;i++)
		cout<<myArray[i] << " " ;
	system("pause");
	return 0;
}
void init(){
	cout << "n=";
	cin >> n;
	myArray=new int[n];
	
	for(int i=0;i<n;i++){
		cout << "myArray[" << i << "]=" ;
		cin >> myArray[i];
	}

}


int firstSort(int myArray[],int left,int right){
	while(left<right)
		if(myArray[left]%2==1)
			if(myArray[right]%2==0)
				swap(myArray[left],myArray[right]);
			else right--;
		else left++;
	return right;
}

int pivot(int myArray[],int left,int right,bool (*comparision)(int,int)){
	int nrOfSwaps=0;
	while(left<right){
		if(!comparision(myArray[left],myArray[right])){
			swap(myArray[left],myArray[right]);
			nrOfSwaps++;
		}
		if(nrOfSwaps%2==0)right--;
		else left++;
	}
	return right;
}

void quickSort(int myArray[],int left,int right,bool (*comparision)(int,int)){
	if(left<right){
		quickSort(myArray,left,pivot(myArray,left,right,comparision)-1,comparision);
		quickSort(myArray,pivot(myArray,left,right,comparision)+1,right,comparision);
	}
}


What do you think?
Topic archived. No new replies allowed.