Hi I was given homework on arrays, but this new stuff is very confusing to follow. I'm asked to create an array with 10 integers from user input and have the values from 0-20. If the array is not sorted then sort it i.e. array [0] < array [1] < array [2] < ...
If anybody can walk me through I would appreciate it thank you.
Thank you for the coding boost lexical cast, but is there a way it can be done without constexpr, auto, and const? I haven't learned any of those key words yet
Achilles23
There are many sorting algorithms, but the classic one is "bubble sort" - try googling that. It consists of successive passes down an array, swapping pairs of elements that are out of order, so that the "lightest" (i.e smallest) gradually "bubble" to the top.
I think boost lexical cast actually has a version of it above , in his lines 38-50, but it may be easier to flush it out on paper. His lines 45-47 are just a swapping routine (for objects that can be added or subtracted), but you can probably code a swap(a,b) for arbitrary objects, irrespective of whether they can participate in arithmetic operations.
There are tweaks that make bubble sort go faster. The largest element ends up at the end of the array on the first pass, so you only have to check to the penultimate element on the second pass etc. and your swapping passes get shorter each time. Similarly, if you make no swaps on one pass then you know that you are sorted, so you can go home early.
Other routines include QuickSort, but there are many others.
That's what we were taught, but I did not know there was actually a name for that thanks I'll be sure to look it up
Tibrado
Thank you for the code however its not asking for an input right away its asking to ask for user input on the monitor so anybody can use it, but it did work though.
Thank you for the coding boost lexical cast, but is there a way it can be done without constexpr, auto, and const?
Replace all occurences of auto with unsignedshortint.
Remove all keywords constexpr and const.
The code will still work.
P.S. I used auto so the compiler will deduce the type. I used constexpr because SIZE is constant and will be used as a constant expression. I used const because NEXT won't change.