user2851616
user2851616

Reputation: 5

How to sort an array in C++ in a specific way

I want somehow sort an array, so that it looks like - a[0]>=a[1]<=a[2]>=a[3]<=a[4] I don't know where to start. Any suggestion would be appreciated!

Upvotes: 0

Views: 2437

Answers (5)

JordanYankovich
JordanYankovich

Reputation: 26

Here's the code, obviously you can alter the array length and numbers to meet your specifications.

#include <iostream>
#include <algorithm> 
 using namespace std;
void special_Sort(int *array, int size){
//doesn't return a value, changes the values inside the array
int temp; 
//for swapping purposes 
sort(array, array+size);

//sorts the array in ascending order
for(int i=0; i<size; i=i+2){
    temp=array[i];
    array[i]=array[i+1];
    array[i+1]=temp; 
}

//array is now sorted 
}

int main(){
// array declaration, call the function, etc...
int array[10]={2,4,1,5,6,3,7,9,8,10};
int *pointer; 
pointer=&array[0];

special_Sort(pointer, 10);

// if you want to print the result 
//   for(int i =0; i<10; i++)
//      cout<<array[i]<<" ";    

return 0;
}

Upvotes: 1

ryyker
ryyker

Reputation: 23218

Steps taken:
1) generate some random array
2) sort array
3) switch elements as needed with alternate <=, >= comparisons

Here's the code that does that: (disregard the random generator, its just an easy way to generate an array)

#define sizeArr 50
int main(void)
{
    int array[sizeArr];
    int i, temp;

    for(i=0;i<sizeArr;i++)
    {
        array[i]=randomGenerator(1, 1000);
        Sleep(2);//force clock tick for new srand() to be effective in rand() generator
    }

    //sort array
    qsort(array, sizeArr, sizeof(int), cmpfunc);

    //pick the first non repeat 90th percent and print
    for(i=0;i<sizeArr-1;i++)
    {
        if(i%2==0)//alternate between >= && <=
        {
            if(array[i+1] >= array[i])
            {
                temp = array[i+1];
                array[i+1]=array[i];
                array[i]=temp;
            }
        }
        else
        {
            if(array[i+1] <= array[i])
            {
                temp = array[i+1];
                array[i+1]=array[i];
                array[i]=temp;
            }
        }
    }
    getchar();  

    return 0;
}


int cmpfunc (const void * a, const void * b)
{
   return ( *(int*)a - *(int*)b );
}

int randomGenerator(int min, int max)
{
    int random=0, trying=0;

    trying = 1;         
    srand(clock());
    while(trying)
    {
        random = (rand()/32767.0)*(max+1);
        (random >= min) ? (trying = 0) : (trying = 1);
    }

    return random;
}

Upvotes: 0

Leeor
Leeor

Reputation: 19706

I'm assuming here that the relations are inclusive (in the sense that they continue to the end of the line - a[0]>=max(a[1],a[2],...), and a[1]<=min(a[2],a[3],..) and so on). Otherwise this isn't uniquely defined, as {5,4,3,2,1} can get sorted for example into {5,1,4,3,2} or {3,2,5,1,4}.

So, assuming this is the case, it's easily solved by sorting the entire array in descending order, then just interleave them -

 a[0], a[n-1], a[1], a[n-2], ...

and so on. Just loop with two indices, one starting from the beginning and one from the end, or use something like this -

for (i=0; i<n/2; i++) {
    result[i*2] = sorted[i];
    result[i*2+1] = sorted[n-i];
}
if (n%2) 
    result[n-1] = sorted[n/2]

Upvotes: 1

Matt N.
Matt N.

Reputation: 197

If you are only sorting it in a way that you want values to rise and fall arbitrarily, you can achieve this by checking values in your array and swapping elements if they do not satisfy the constraints of your sort.

Don't have a compiler on me at the moment and you'd have to implement the swap but something like this could work:

for(i=0; i < a.length(); i++){
  //If index is even
  if(i%2 == 0){
     if(a[i] < a[i+1]){
        swap(a[i], a[i+1]);
     }
  } else {      ///If index is odd
     if(a[i]>a[i+1]){
        swap(a[i], a[i+1];
     }
    }
  }

I don't disagree with the other answers posted here so you will have to find what you need depending on the relation of the even and odd indexed elements.

Upvotes: 0

theAlias
theAlias

Reputation: 396

Sort the entire array (Choose any sort algorithm you wish to). Then take each pair from the beginning and swap the elements in the pair

2,4,1,5,6,3,7,9,8,10

Sorted to : 1,2,3,4,5,6,7,8,9,10

Pair and swap : (2,1),(4,3),(6,5),(8,7),(10,9)

result : 2,1,4,3,6,5,8,7,10,9

Upvotes: 2

Related Questions