How do you sort arrays | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How do you sort arrays

as in how do you sort the values by value? as in if I had 3 numbers 7,30,2. How would I use a sort method to make it 2, 7, 30?

30th Mar 2017, 1:44 PM
JustSomeGuy
JustSomeGuy - avatar
23 Answers
+ 2
You could do something like this: #include <iostream> #include <vector> template<typename Iter> void sort(Iter begin, Iter end){ for(Iter b = begin; b != end - 1; ++b){ for(Iter e = b+1; e != begin; --e){ if(*(e-1) > *e){ std::swap(*(e-1), *e); } } } } int main(){ std::vector<int> numbers{7, 30, 2}; sort(numbers.begin(), numbers.end()); for(auto &n: numbers) std::cout << n << " "; } or if you're really lazy just include <algorithm> and use that sort.
30th Mar 2017, 2:52 PM
Dennis
Dennis - avatar
+ 1
C++11 by Bo Qian on youtube has some nice stuff, I also recommend the book Effective Modern C++ by Scott Meyers. I also use the site https://www.codingame.com. There are alot of good players there and alot of the code is visible after you complete challenges, allowing you to pick up some new tricks.
30th Mar 2017, 3:13 PM
Dennis
Dennis - avatar
+ 1
thr r some easy techniques like selection sort and bubble sort apply them!!
30th Mar 2017, 5:45 PM
Suhail Pappu
Suhail Pappu - avatar
0
Use two for loops and use it to find the lowest and set that equal to the zero index of your array and then do the same for the other one.
30th Mar 2017, 1:56 PM
Ryan
0
Well the second one will be a little different.
30th Mar 2017, 1:57 PM
Ryan
0
im not following with the for loops
30th Mar 2017, 1:58 PM
JustSomeGuy
JustSomeGuy - avatar
0
@Dennis could you explain the <algorithm> sort. it's spassing when I just throw in the three numbers
30th Mar 2017, 3:13 PM
JustSomeGuy
JustSomeGuy - avatar
0
@M.A gleason #include <iostream> #include <vector> #include <algorithm> int main(){ std::vector<int> numbers{7 , 30, 2}; std::sort(numbers.begin(), numbers.end()); for(auto &n: numbers) // This requires c++11 std::cout << n << " "; } Hopefully I didn't make any typos ^^
30th Mar 2017, 3:17 PM
Dennis
Dennis - avatar
0
I'm getting the exception |error: conversion from 'int' to non-scalar type 'std::vector<int>' requested| I wrote std::vector<int> numbers[3] = {one,two,three}; std::sort(numbers.begin(),numbers.end());
30th Mar 2017, 3:27 PM
JustSomeGuy
JustSomeGuy - avatar
0
Please post your code.
30th Mar 2017, 3:32 PM
Dennis
Dennis - avatar
0
#include <iostream> #include <vector> #include <algorithm> using namespace std; /*Roundabout way of sorting void sorter(int *oneV, int *twoV, int *threeV) { int oneH = *oneV, twoH = *twoV, threeH = *threeV; if (oneH < twoH && oneH < threeH){ *oneV = oneH; cout << "Hit 1 "; if (twoH < threeH){ *twoV = twoH; *threeV = threeH; cout << "2 3 \n"; }else{ *twoV = threeH; *threeV = twoH; cout << "3 2 \n"; } }else if (twoH < oneH && twoH < threeH){ *oneV = twoH; cout << "Hit 2 "; if (oneH < threeH){ *twoV = oneH; *threeV = threeH; cout << "1 3 \n"; }else{ *twoV = threeH; *threeV = oneH;\ cout << "3 1 \n"; } }else if(threeH < oneH && threeH < twoH){ *oneV = threeH; cout << "Hit 3 "; if (twoH < oneH){ *twoV = twoH; *threeV = oneH; cout << }else{ *twoV = oneH; *threeV = twoH; } } }*/ int main() { int one, two, three; bool test = false; do { cout << "Enter three numbers to find median." << endl; cin >> one; cin >> two; cin >> three; if (((one >= 1) && (one <= 20)) && ((two >=1) && (two <= 20)) && ((three >= 1) && (three <= 20))){ test = true; }else { cout << "incorrect numbers, try again." << endl << endl << endl << endl; test = false; } } while (test == false); /*sorter(&one,&two,&three);*/ std::vector<int> numbers[3] = {one,two,three}; std::sort(numbers.begin(),numbers.end()); cout << numbers[0] << " " << numbers[1] << " " << numbers[2]; } Ignore the roundabout way, I'm trying to do that without making an entire function. I'm trying to learn how to do stuff faster, not bre
30th Mar 2017, 3:39 PM
JustSomeGuy
JustSomeGuy - avatar
0
You do not need [3], vector is a dynamic array, so it resizes itself while you add to it. you also don't need to use std:: because you used using namespace std
30th Mar 2017, 3:40 PM
Dennis
Dennis - avatar
0
Thought I didn't, but t didn't like me the first time, so i just copy pasta'd yours to be sure
30th Mar 2017, 3:42 PM
JustSomeGuy
JustSomeGuy - avatar
0
@Dennis do you need C++11 to do _all_ of that, or just the for loops you showed? Because new error came up. |70|error: in C++98 'numbers' must be initialized by constructor, not by '{...}'| |70|error: could not convert '{one, two, three}' from '<brace-enclosed initializer list>' to 'std::vector<int>'|
30th Mar 2017, 3:45 PM
JustSomeGuy
JustSomeGuy - avatar
0
c++11 is needed for the way I outputted everything. You need at least c++0x for the vector initialization I highly recommend upgrading, it'll make life so much easier. for c++98 you can do this: std::vector<int> numbers(3); //Initial size of 3 for(int i = 0; i < numbers.size(); ++i){ std::cout << "Input number " << i + 1 << ": "; std::cin >> numbers[i]; } std::sort(numbers.begin(), numbers.end()); for(int i = 0; i < numbers.size(); ++i){ std::cout << numbers[i] << " "; }
30th Mar 2017, 3:55 PM
Dennis
Dennis - avatar
0
@Dennis code::blocks has me running '98, where can I find an upgrade for free/cheap.
30th Mar 2017, 4:02 PM
JustSomeGuy
JustSomeGuy - avatar
0
In codeblocks you can go to Settings -> Compiler and in Compiler settings you can check "Have g++ follow the c++11 ISO C++ language standard". If you do not, redownload it.
30th Mar 2017, 4:07 PM
Dennis
Dennis - avatar
0
"Have g++ follow the C++11 ISO C++ language standard [-std=c++11]"?
30th Mar 2017, 4:10 PM
JustSomeGuy
JustSomeGuy - avatar
0
Yes , that one
30th Mar 2017, 4:11 PM
Dennis
Dennis - avatar
0
AHA! it works now! Thank you so so much @Dennis! Do you have discord, because there's a coders server I'm in and we'd love to have you
30th Mar 2017, 4:13 PM
JustSomeGuy
JustSomeGuy - avatar