Home > database >  How to sort an array to start with 2 then 1 and then 3?
How to sort an array to start with 2 then 1 and then 3?

Time:11-05

I've got this program that generates random numbers from 1 to 3 and put it into an array:

int main() {
    int size;

    cin >> size;

    int *array = new int [size];

    for(int i = 0; i < size; i  ) {
        array[i] = rand() % (2   1)   1;
    }

    std::sort(array, array   size);

    for(int i = 0; i < size; i  ) {
       cout << array[i] << endl;
    }
}

and it sorts numbers in ascending order:

1
1
2
2
3
3

but I want it to start with 2 then 1 then 3, for example:

2
2
1
1
3
3

anyone knows how to do it? thanks in advance

CodePudding user response:

You can create a custom comapre function like this:

bool cmp(const int &lhs, const int &rhs)
{
    std::map<int, int> compareValue;
    compareValue.insert(std::pair<int, int>(1, 2));
    compareValue.insert(std::pair<int, int>(2, 1));
    compareValue.insert(std::pair<int, int>(3, 3));

    return compareValue[lhs] < compareValue[rhs];
}

int main() 
{
    int size{};

    std::cin >> size;

    std::vector<int> array(size); // using std::vector instead

    for(int i = 0; i < size; i  ) {
        array[i] = rand() % (2   1)   1;
    }

    std::sort(array.begin(), array.end(), cmp);

    for(const auto &i : array) // use ranged-based for loop instead
    {
        std::cout << i << '\n';
    }
}

CodePudding user response:

It is easy with a special less comparison function.

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>

bool less(int lhs, int rhs) {
  static const int cmp[] = { 0, 2, 1, 3 };
  return cmp[lhs] < cmp[rhs];
}

int main() {
  int size;
  std::srand(time(nullptr));
  if (!(std::cin >> size))
    return 1;

  std::vector<int> array(size);
  for (auto &a : array)
    a = rand() % 3   1;

  std::sort(array.begin(), array.end(), less);
  for (const auto &a : array)
    std::cout << a << '\n';
}

https://onlinegdb.com/TA-WTMx9g

You can use std::priority_queue instead of std::sort.

CodePudding user response:

#include <iostream>
#include <string>
#include <time>
#include <windows.h>

using namespace std;

void llenar_arr(int arr[], int num , int ini , int fin){

     for(int i = ini; i<fin; i  )
     {
         arr[i] = num;
     }
}

int main(){


    srand(time(NULL));

    int n;
    int *arr;

    cout<<"Ingrese el numero de elentos :";
    cin>>n;

    arr = new int[n];

    int n1 = 0;
    int n2 = 0;
    int n3 = 0;

    for(int i = 0; i<n; i  )
    {
        int num = rand() % 3 1;

        arr[i] = num;

        switch(num)
        {
           case 1: n1  ; break;
           case 2: n2  ; break;
           case 3: n3  ; break;
        }
    }

    for(int i = 0; i<n; i  )cout<<arr[i]<<" ";

    cout<<endl;

    llenar_arr(arr,2,0,n2);
    llenar_arr(arr,1,n2,n1 n2);
    llenar_arr(arr,3,n1   n2,n1   n2   n3);

    for(int i = 0; i<n; i  )cout<<arr[i]<<" ";


    delete[] arr;

    cout<<endl;
    system("pause");

     return 0;
}

CodePudding user response:

You can use this lazy algorithm.

for (int i = 0; i <= size - 1; i  ) {
    if (array[i] == 1) {
        one  ;
    }
    else if (array[i] == 2) {
        two  ;
    }
    else if (array[i] == 3) {
        three  ;
    }
    
}

for (int i = two; i > 0; i--) {
    cout << 2 << '\x0A';
}
for (int i = one; i > 0; i--) {
    cout << 1 << '\x0A';
}
for (int i = three; i > 0; i--) {
    cout << 3 << '\x0A';
}

It basically just takes note of how many 1s, 2s, and 3s are there. Next, it prints out 1s or 2s or 3s in the 2-1-3 order "i" number of times, "i" being the number of occurrences of that number.

  •  Tags:  
  • c
  • Related