swap the values inside array

i want to make sure that all the elements with values of <= 25 are on the left hand side and >25 are on the right hand side.
some thing like if the array is
43 36 14 36 16 30 45 11 16 40 31 48 39 30 17

and the result should be

17 16 14 11 16 30 45 36 36 40 31 48 39 30 43

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
#include <iostream>
#include <ctime>
using namespace std;

void constructArray(int [],int);
void printArray(int [],int);
void swapElements(int [],int);
void iterativeSwap();
void recursiveSwap();
const int MAX=20;

int main()
{
	srand ( time(NULL) );
	int a[MAX];
	int size;
	size = rand() % 10 + 10;
	constructArray(a,size);
	printArray(a,size);
}

void constructArray(int a[],int size)
{
	for(int i=0;i<=size;i++)
	{
		a [i] = rand() %50;  
	}
}

void printArray(int a[],int size)
{
	cout << "Given the following array" << endl;
	for(int i=0;i <= size;i++)
	{
		cout << "  " <<  a[i];
	}
	cout << endl;
}

void swapElements(int a[], int size)
{	 
	for(int i=0;i <= size; i++)
	{
		for(int )
		if(a[i])
	}
}

void iterativeSwap(int a[], int size)
{
	for(int i=0;i <=size; i++)
	{
		
	}
}

void recursiveSwap()
{

}



please can some1 guide me?
i believe this comes close to what you need. i did not test it.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int b[MAX];
int lowerThen26=0;
void swapElements(int a[], int size)
{	 
	for(int i=0;i <= size; i++)
	{
		if (a[i]<=25) {
                     lowerThen26++;
                }
	}
        counter=lowerThen26;
        for(int i=0;i <= size; i++)
	{
		if (a[i]<=25) {
                     b[lowerThen26-1]=a[i]
                     lowerThen26--;
                } else {
                     b[counter]=a[i]
                     counter++;
	}
}
thank you for your help.. but i wanna break it down into 2 functions where swapelements only do the swap.. after i run iterative swap when the condition is correct it'll perform swapelements
i have one simple solution for you why aren't you just sort your array that will be your solution..
sorry i don't get what you mean...
The appropriate C++ function is partition()

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <algorithm>
#include <functional>
int main()
{
   int a[] = {43, 36, 14, 36, 16, 30, 45, 11, 16, 40, 31, 48, 39, 30, 17};
   std::size_t sz = sizeof a / sizeof *a;

   std::cout << "Before partitioning: \n";
   for(std::size_t n = 0; n<sz; ++n)
        std::cout << a[n] << ' ';
   std::cout << '\n';

   std::partition(a, a+sz, std::bind2nd(std::less_equal<int>(), 25));

   std::cout << "After partitioning: \n";
   for(std::size_t n = 0; n<sz; ++n)
        std::cout << a[n] << ' ';
   std::cout << '\n';
}
Before partitioning: 
43 36 14 36 16 30 45 11 16 40 31 48 39 30 17 
After partitioning: 
17 16 14 11 16 30 45 36 36 40 31 48 39 30 43 



thank you for you guys for answers. but it's too advanced for me. may be i need to read more....
Topic archived. No new replies allowed.