alfers101
How can I arrange the values of a,b,c in ascending order where a will gets the lowest value and variable c gets the highest value the values of the variables are: a equals 15,b equals 10,c equals 2
Delete
Share
This Question is Closed
agdgdgdgwngo
Best Response
You've already chosen the best response.
0
use quicksort.
agdgdgdgwngo
Best Response
You've already chosen the best response.
0
bubble sort works fine too.
alfers101
Best Response
You've already chosen the best response.
1
can u show me the code please?
agdgdgdgwngo
Best Response
You've already chosen the best response.
0
one moment
agdgdgdgwngo
Best Response
You've already chosen the best response.
0
oh which language?
alfers101
Best Response
You've already chosen the best response.
1
C++
agdgdgdgwngo
Best Response
You've already chosen the best response.
0
alright, but for C++ I think there's a library function
alfers101
Best Response
You've already chosen the best response.
1
its okay, ^^
alfers101
Best Response
You've already chosen the best response.
1
then what?
Ishaan94
Best Response
You've already chosen the best response.
2
Make a loop, Compare first element with subsequent elements if A[1] > A{2} => A[1]=b => A[1] = A[2] => A{2]=b Then compare A[3] and so on
alfers101
Best Response
You've already chosen the best response.
1
this is the code?
alfers101
Best Response
You've already chosen the best response.
1
i'll just put the values??
agdgdgdgwngo
Best Response
You've already chosen the best response.
0
only the quickSort function at the very top is the sorting function
alfers101
Best Response
You've already chosen the best response.
1
this one?
#include <iostream>
using std::cout;
using std::endl;
void quickSort(int A[], int left, int right)
{
int i = left, j = right;
int tmp;
int pivot = A[(left + right) / 2];
while (i <= j) {
while (A[i] < pivot)
i++;
while (A[j] > pivot)
j--;
if (i <= j) {
tmp = A[i];
A[i] = A[j];
A[j] = tmp;
i++;
j--;
}
};
if (left < j) quickSort(A, left, j);
if (i < right)quickSort(A, i, right);
}
agdgdgdgwngo
Best Response
You've already chosen the best response.
0
right
alfers101
Best Response
You've already chosen the best response.
1
okay so where will i put the A,B,C ??
agdgdgdgwngo
Best Response
You've already chosen the best response.
0
well, except the #include <iostream> using std::cout; using std::endl; part...
alfers101
Best Response
You've already chosen the best response.
1
oh okay. then where will i put their values?
agdgdgdgwngo
Best Response
You've already chosen the best response.
0
you can set A,B, and C, to be integer elements of an array.
agdgdgdgwngo
Best Response
You've already chosen the best response.
0
and simply sort the array :-D
alfers101
Best Response
You've already chosen the best response.
1
actually im just new on C++
alfers101
Best Response
You've already chosen the best response.
1
and i dont know that array sorry
agdgdgdgwngo
Best Response
You've already chosen the best response.
0
I'm new to C++ too; that code is from a C implementation. In C++ there are nicer data structures like vectors and sets.
agdgdgdgwngo
Best Response
You've already chosen the best response.
0
well if you haven't gotten to arrays and pointers yet do it Ishaan's way
alfers101
Best Response
You've already chosen the best response.
1
can u put the A,B and C on the code and their values? im just a newbee seriously
FoolForMath
Best Response
You've already chosen the best response.
2
No need to apply sorting algorithm for only 3 values,simply write a macro that will do the job for you ..
FoolForMath
Best Response
You've already chosen the best response.
2
btw C has qsort() inbuilt which could be used but still overkill.
agdgdgdgwngo
Best Response
You've already chosen the best response.
0
yeah qsort() is a C standard library function (though it's not usually used)
agdgdgdgwngo
Best Response
You've already chosen the best response.
0
just #include <cstdlib> and call qsort()
FoolForMath
Best Response
You've already chosen the best response.
2
lolz I used it often in SPOJ and I am sure many do still now
alfers101
Best Response
You've already chosen the best response.
1
what??
alfers101
Best Response
You've already chosen the best response.
1
confused O__O"
FoolForMath
Best Response
You've already chosen the best response.
2
#include <cstdlib> is C++ header
FoolForMath
Best Response
You've already chosen the best response.
2
#include <stdlib.h> is more C
alfers101
Best Response
You've already chosen the best response.
1
we were using C++
alfers101
Best Response
You've already chosen the best response.
1
okay lets go back to my problem. so how can i start doing it?
FoolForMath
Best Response
You've already chosen the best response.
2
start writing a macro it's really simple.
alfers101
Best Response
You've already chosen the best response.
1
what's macro? sorry :(
FoolForMath
Best Response
You've already chosen the best response.
2
Something like this:
#define M(a,b) ((a)>(b))?(a):(b)
alfers101
Best Response
You've already chosen the best response.
1
then?
FoolForMath
Best Response
You've already chosen the best response.
2
sorry.., I am not doing your homework
Ishaan94
Best Response
You've already chosen the best response.
2
Hmm you can use If-else for comparing 3 integers
agdgdgdgwngo
Best Response
You've already chosen the best response.
0
yeah, either a function or a macro
FoolForMath
Best Response
You've already chosen the best response.
2
if else would be cubersome Ishaan ..
FoolForMath
Best Response
You've already chosen the best response.
2
*cumbersome
alfers101
Best Response
You've already chosen the best response.
1
actually this isn't a homework. i just want to learn but i can't
FoolForMath
Best Response
You've already chosen the best response.
2
Max = M(M(a,b),c); there you go..
FoolForMath
Best Response
You've already chosen the best response.
2
if-else would be too much for my really small brain ...
FoolForMath
Best Response
You've already chosen the best response.
2
and in C++ you can use stl algorithms then it will be cake-walk
alfers101
Best Response
You've already chosen the best response.
1
the one that you've given to me,the code, i will use that for my problem?
alfers101
Best Response
You've already chosen the best response.
1
here's the thing. can u show me how to do it? then i'll try to do that and see if i can make another one.
Ishaan94
Best Response
You've already chosen the best response.
2
Right If-else would be tough
alfers101
Best Response
You've already chosen the best response.
1
i just dont know how to do it. please show me the code then i'll make another one which i will answer all by myself.
alfers101
Best Response
You've already chosen the best response.
1
please? anyone? :((
Tomas.A
Best Response
You've already chosen the best response.
0
if you new to programming choose another language to learn, like JAVA or Python if it's not your homework :D
alfers101
Best Response
You've already chosen the best response.
1
yep it wasn't but i want to learn this thing. if u dont want me to help then fine :/
Tomas.A
Best Response
You've already chosen the best response.
0
why you want to learn C++?
agdgdgdgwngo
Best Response
You've already chosen the best response.
0
to build computer viruses!