Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
What can I used to arrange number from big until small without using array in C++?

What I have tried:

*This is a question.
Cause I find out most of the example will use array to solve this problem.
So without array, I don't know what can I used?
Posted
Updated 10-Jun-22 11:21am
Comments
Richard MacCutchan 9-Jun-22 8:39am    
The question is not clear. If you cannot use an array, or one of the STL containers then you cannot solve the question. In order to arrange a set of anything you must be able to store them somewhere. Maybe you need to reword your question.
enhzflep 9-Jun-22 11:00am    
I could re-create the functionality of an STL container to achieve the same result as if I'd used one.
Is this beyond you? (I know it's not) Hint: Linked list or Tree.
Richard MacCutchan 9-Jun-22 11:09am    
No it's not beyond me, but I cannot speak for the OP.
enhzflep 9-Jun-22 11:12am    
Exactly my point. ;)

You have to use a collection (like the array), at least to store the numbers.
That said, using a std::multiset could be an advantageous, try:
C++
#include <iostream>
#include <set>
using namespace std;

int main()
{
  multiset<int, greater<int> > ms{1, 12, -3, 27, 2, 12, 42, 1, 53};

  for (auto i : ms)
    cout << i << " ";
  cout << endl;
}
 
Share this answer
 
Comments
The Kings 9-Jun-22 8:20am    
But if I cant used std::multiset and array what else can I use? cause I'm just beginner so not allow to use.
[no name] 10-Jun-22 17:50pm    
You can sort to output (stdout) with a state machine. No arrays or temporary storage required.
Quote:
Cause I find out most of the example will use array to solve this problem.

Because arrange (sort) is easier done with array.
Quote:
So without array, I don't know what can I used?

Many data structures exist each with their own stength and weaknesses.
Main Data Structures are:
-Array
-Single Linked Lists
-Doubly Linked Lists
-Binary Tree
-Balanced binary Tree
...
Data Structures - GeeksforGeeks[^]

choose your poison :-)
 
Share this answer
 
Comments
merano99 10-Jun-22 17:22pm    
+5
Patrice T 10-Jun-22 19:26pm    
Thank you
Quote:

What can I used to arrange number from big until small without using array in C++?

If you can't use arrays or STL containers, then I would see the linked list data structure as the best alternative.
https://www.geeksforgeeks.org/data-structures/linked-list/
The alternatives of Patrice T's list are also conceivable.
 
Share this answer
 
Comments
Patrice T 10-Jun-22 19:28pm    
+5
When a set of numbers is of arbitrary size, I can't see a way to sort them without using something that is equivalent to an array, no matter what name someone chooses to call it.

There are n numbers, so something of size n has to hold them. That's basically the definition of an array.

If n is fixed and small, you could put the numbers in variables a, b, c... and sort them in a series of if statements that assigns the highest one to a, then the second highest one to b, and so on. But that gets very silly for n > 3.

I'm surprised that anyone would expect to find a way to do this without using an array, so I wonder if you might be trolling us.
 
Share this answer
 
Comments
enhzflep 9-Jun-22 10:57am    
The thing that immediately stands out to me is that reasonable, efficient use of an array requires one to know the number of elements first.
Neither insertion-sort nor tree-sort have this limitation. Neither of em need an array either. :p

But without more info from the OP, one can only guess at the aim of the exercise. I could see such a question asked to learners serving any one of a number of purposes.
Greg Utas 9-Jun-22 12:03pm    
I'd argue that insertion sort requires an array. But you've got a good point with tree sort.
enhzflep 9-Jun-22 19:44pm    
I'd then turn around and point out the fact you can use a linked list when memory is fragmented and there isn't a contiguous section of memory large enough to hold the data.

Linked-lists and arrays are nothing like one-another, no?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900