How to sort an unsorted set of values using vectors and for loops in C++?

How to sort an unsorted set of values using vectors and for loops in C++?

Background:
I have a beginner knowledge and experience with vectors, so I know how to use it.


Question:
Imagine you are given this unsorted set of vectors:

vector <int> unsorted = {12, 36, 7, 50, 3, 80, 2};

Explain how I can make this sorted like this output :

2, 3, 7, 12, 36, 50, 80.

Please explain your logic and give examples if you can in C++. (Modern version, no std).

Thanks.

---------------------------------------
Here is my own attempt: (Link)

cpp.sh/2kgt

It has some logical mistakes. I am beginner :)
Last edited on
You should decide which sorting algorithm to use and then implement it.

Two of the easiest sorting algorithms to implement (but not the most efficient) are insertion sort and bubble sort.

https://en.wikipedia.org/wiki/Insertion_sort
https://en.wikipedia.org/wiki/Bubble_sort

But there are are many other sorting algorithms to choose from.

https://en.wikipedia.org/wiki/Sorting_algorithm#Popular_sorting_algorithms
Last edited on
Modern version, no std


So you want to do C++ w/o using the std namespace or do you mean stl in which case it would contradict the phrase 'modern version' since the standard library (as the erstwhile stl has come to be known) is one of the bedrocks of 'modern c++'?

I can think of at least five ways of sorting the std::vector:
- a C-style array sort as I think you're trying to do in your code-link
- sort using the std::sort() algorithm (Peter87 refers to these 2 in his reply)
- using a functor with (a) a named object or (b) by calling its default ctor
- writing a lambda function
Underlying each of these would be some of the basic sorting algorithms but the user interface would vary in each case. Perhaps there are yet other ways I haven't covered

You can find information about all of these on-line, if something is unclear after search come back here.
@Peter87, thanks for your information.

@gunnerfunner, you know when people use std::cout and some stuff like that. I am not use to that, so in case if some users actually posted their code, I wanted them to write it in this way which I call "modern version" cout << .
Also thank you for the information you provided.
You might call it
modern version
but far more experienced programmers than you or me usually call it 'bad practice':

http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice
At worst, the use of using namespace xxx can cause the behavior of your program to silently change.

This happens when (e.g.) code in a third-party module introduces a colliding name (an anaphor) which is more preferable the overload resolution rules than your own.

Compare the two programs -- before and after some third-party updates their code in namespace foo:
http://coliru.stacked-crooked.com/a/7e7727a0b7aecd03
http://coliru.stacked-crooked.com/a/6e0da24c972debca

Prefer introducing only specific names (i.e., write using std::cout;), if you absolutely must use them. Do not place file-scope using-declarations of any sort in a header file, but instead limit using-declarations locally if you choose to use them at all.

Even restricted to introducing only particular names in a local scope, they're still subject to the same problems. In the case of ambiguity the programmer has to know about overload resolution and all the participating candidates. That's not always easy especially in the case where participating overloads are eliminated by a metaprogram or some other sorts of SFINAE techniques. If that's not enough, the programmer needs to actually read the using statement in order to consider this.

For good reason, the consensus is that it's simpler and easier to just type the full name, or at least an alias of that full name.

Happy New Year!
Last edited on
Topic archived. No new replies allowed.