If I have the user enter 10 random numbers and I want to order them from smallest to biggest what is the best method to do this using the most basic C++ language.
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
vector<int> vec;
vec.push_back(1);
vec.push_back(4);
vec.push_back(3);
vec.push_back(2);
sort( vec.begin(), vec.end() );
for (vector<int>::const_iterator it=vec.begin(); it!=vec.end(); ++it) {
cout << *it << " ";
}
cout << endl;
return 0;
}
std::vector<int> numbers;
// get the numbers from the user here.
std::sort(numbers.begin(), numbers.end());
-
-
1@Matthieu: It's likely faster to do sorting just once at the end. – Johan Kotlinski Feb 24 '11 at 8:53
-
@kotlinski: while I agree with that, and using a
vector
for small input sets, it does seem a premature optimization! The asymptotic performance is equivalent O(N log N), so you're going for constant factor here, at the cost of complicating the code. – Matthieu M. Feb 24 '11 at 9:23 -
@Matthieu: I don't know,
sort
is quite explicit about what it does andvector
seems like the simplest possible container. It just doesn't seem very complicated to me, I guess. – Johan Kotlinski Feb 24 '11 at 12:49 -
1@Matthieu M.: I have at times, and it would certainly work. Personally, I've developed a bit of the same attitude as @kotlinkski. I need a bit more than just the fact that the container is always sorted to justify using it. A multiset is not only slower, but uses quite a bit more memory (data + 2 pointers per node). On x64 Windows, for example, a [multi]set of 10 ints has 40 bytes of data and 160 bytes of pointers. IMO, this situation doesn't justify that. – Jerry Coffin Feb 24 '11 at 16:27
Use a structure that maintains ordering: std::multiset
#include <iostream>
#include <set>
#include <boost/lexical_cast.hpp>
int main(int argc, char* argv[])
{
std::multiset<int> set;
for (int i = 1; i != argc; ++i) {
set.insert(boost::lexical_cast<int>(argv[i]));
}
for (int i: set) { std::cout << i << " "; }
std::cout << "\n";
}
Invocation:
$ yourprogram 1 5 4 6 7 82 6 7 8
(Note: the number of arguments is not constrained)
-
1
-
2@kotlinski: C++0x new
range-based for
statement (6.5.4 [stmt.ranged]), I don't know if any compiler already implement it though. – Matthieu M. Feb 24 '11 at 13:42
It depends on your requirements. If you just want to sort them, and speed is only of moderate concern, an insertion sort would be fine for such a small n-value (10). Quick to implement (from scratch), and suitable for small set sizes.
//this is sorting min--->max without pointers
#include<iostream>
using namespace std;
int main()
{int n;
cout<<"How much numbers you wanna sort? "<<endl;
cin>>n;
int broj[n];
cout<<"Enter numbers: "<<endl;
for(int k=0;k<n;k++)
{
cin>>broj[k];
}
int min=0;
for(int z=0;z<n;z++)
{
loop:
min=broj[z];
for(int i=z;i<n;i++)
{
if(min<=broj[i])
{
}
else
{
min=broj[i];
broj[i]=broj[z];
broj[z]=min;
goto loop;
}
}
}
cout<<endl<<"--------------"<<endl;
for(int j=0;j<n;j++)
{
cout<<broj[j]<<endl;
}
return 0;
}
-
-
1so this is just old school program with just if,for and goto loops..so the core of this program is in this "i" for loop which replaces numbers on different index on array..min is variable which we use for easier "transport" of that values..all in all my advice if you have visual studio or some program which can go step by step (line by line) when you run it,it will help you understand this concept...it's a bit longer but with this you will develop your programming way of thinking – Leo Ivas Feb 5 '16 at 21:03
You can write something yourself, but really should use qsort function.
-
8Quite the contrary. In C++ there's rarely (never?) a good reason to use
qsort
. – Jerry Coffin Feb 24 '11 at 8:20 -
3
-
5