-2

I have an array of ints;

int bar [5] = { 10, 20, 30 }; 

Lets say instead of 3 integers, I have 10,000 in my array.

How would I unsort so that there is no order?

CodeDoctorJL
  • 1,216
  • 4
  • 16
  • 29
  • 4
    how about shuffle it? – taocp Mar 21 '14 at 19:44
  • Do you want *no order at all*, or just *random order*? – templatetypedef Mar 21 '14 at 19:44
  • 7
    There is _always_ an order. The question is, what do you want that order not to be? And why? – Lightness Races in Orbit Mar 21 '14 at 19:45
  • 1
    why not just assume that it's unsorted in the first place – Sam I am says Reinstate Monica Mar 21 '14 at 19:45
  • It's not logically possible for an array to have "no order". Please describe the problem more clearly. And keep in mind that `std::random_shuffle`, or any similar solution, might randomly leave the array in sorted order. – Keith Thompson Mar 21 '14 at 19:45
  • 1
    just random, because my professor is making us insert all the numbers into a bst and a sorted array would make the tree inefficient – CodeDoctorJL Mar 21 '14 at 19:46
  • 2
    possible duplicate of [Is it possible to random\_shuffle an array of int elements?](http://stackoverflow.com/questions/14720134/is-it-possible-to-random-shuffle-an-array-of-int-elements) – Shomz Mar 21 '14 at 19:47
  • @Shomz: No, not really. – Lightness Races in Orbit Mar 21 '14 at 19:47
  • 1
    To _unsort_ the array, it would be sufficient just to swap the first two distinctly sorted entries :/ ... – πάντα ῥεῖ Mar 21 '14 at 19:50
  • 1
    @LightnessRacesinOrbit And after you read the question and OP's comment? – Shomz Mar 21 '14 at 19:50
  • @Shomz: Yes, after all of that. A duplicate question is just that: a question that asks the same thing as another question. It's hard to imagine suggesting that another answer called "is it possible to use `random_shuffle` in X scenario" can be the same as this answer, when _this_ OP doesn't even know that `random_shuffle` might be something he wants to use. They are not the same question. – Lightness Races in Orbit Mar 21 '14 at 19:57
  • 1
    @LightnessRacesinOrbit Neither this nor the duplicate question I suggested have the best titles. So yeah, if you compare like that, they make no sense, but I meant that the answer to one question completely answers the other. After all, people are here because of the answers, not the questions. – Shomz Mar 21 '14 at 20:02
  • @LightnessRacesinOrbit I'm with Shonz here. _Duplicate_ doesn't means the exact question/content for me, rather having an appropriate SO Q&A , that solves the current question. – πάντα ῥεῖ Mar 21 '14 at 20:12

2 Answers2

6

Just use std::random_shuffle():

std::random_shuffle( std::begin( bar ) , std::end( bar ) );

Note:

Is a good practice to pass your custom random number generator to the algorithm:

std::random_device rd;
std::mt19937 g(rd());

std::random_shuffle( std::begin( bar ) , std::end( bar ) , g );
Manu343726
  • 13,969
  • 4
  • 40
  • 75
  • could you show me an example in code how to use the random_shuffle for int bar [5] = { 10, 20, 30 };? I've been reading the documentation, but i'm confused. – CodeDoctorJL Mar 21 '14 at 20:08
  • 1
    @CodeDoctorJL There is an example in the answer, is the first code snippet. Also, the second code snippet is an example using a custom random number generator – Manu343726 Mar 21 '14 at 20:12
1

You can shuffle your array with std::random_shuffle

taocp
  • 23,276
  • 10
  • 49
  • 62