## alfers101 4 years ago 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

1. agdgdgdgwngo

use quicksort.

2. agdgdgdgwngo

bubble sort works fine too.

3. alfers101

can u show me the code please?

4. agdgdgdgwngo

one moment

5. agdgdgdgwngo

oh which language?

6. alfers101

C++

7. agdgdgdgwngo

alright, but for C++ I think there's a library function

8. alfers101

its okay, ^^

9. Tomas.A

http://www.cplusplus.com/reference/algorithm/sort/ go to CS area

10. alfers101

then what?

11. Ishaan94

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

12. agdgdgdgwngo
13. alfers101

this is the code?

14. alfers101

i'll just put the values??

15. agdgdgdgwngo

only the quickSort function at the very top is the sorting function

16. alfers101

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); }

17. agdgdgdgwngo

right

18. alfers101

okay so where will i put the A,B,C ??

19. agdgdgdgwngo

well, except the #include <iostream> using std::cout; using std::endl; part...

20. alfers101

oh okay. then where will i put their values?

21. agdgdgdgwngo

you can set A,B, and C, to be integer elements of an array.

22. agdgdgdgwngo

and simply sort the array :-D

23. alfers101

actually im just new on C++

24. alfers101

and i dont know that array sorry

25. agdgdgdgwngo

I'm new to C++ too; that code is from a C implementation. In C++ there are nicer data structures like vectors and sets.

26. agdgdgdgwngo

well if you haven't gotten to arrays and pointers yet do it Ishaan's way

27. alfers101

can u put the A,B and C on the code and their values? im just a newbee seriously

28. FoolForMath

No need to apply sorting algorithm for only 3 values,simply write a macro that will do the job for you ..

29. FoolForMath

btw C has qsort() inbuilt which could be used but still overkill.

30. agdgdgdgwngo

yeah qsort() is a C standard library function (though it's not usually used)

31. agdgdgdgwngo

just #include <cstdlib> and call qsort()

32. FoolForMath

lolz I used it often in SPOJ and I am sure many do still now

33. alfers101

what??

34. alfers101

confused O__O"

35. FoolForMath

36. FoolForMath

#include <stdlib.h> is more C

37. alfers101

we were using C++

38. alfers101

okay lets go back to my problem. so how can i start doing it?

39. FoolForMath

start writing a macro it's really simple.

40. alfers101

what's macro? sorry :(

41. FoolForMath

Something like this: #define M(a,b) ((a)>(b))?(a):(b)

42. alfers101

then?

43. FoolForMath

sorry.., I am not doing your homework

44. Ishaan94

Hmm you can use If-else for comparing 3 integers

45. agdgdgdgwngo

yeah, either a function or a macro

46. FoolForMath

if else would be cubersome Ishaan ..

47. FoolForMath

*cumbersome

48. alfers101

actually this isn't a homework. i just want to learn but i can't

49. FoolForMath

Max = M(M(a,b),c); there you go..

50. FoolForMath

if-else would be too much for my really small brain ...

51. FoolForMath

and in C++ you can use stl algorithms then it will be cake-walk

52. alfers101

the one that you've given to me,the code, i will use that for my problem?

53. alfers101

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.

54. Ishaan94

Right If-else would be tough

55. alfers101

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.

56. alfers101

57. Tomas.A

if you new to programming choose another language to learn, like JAVA or Python if it's not your homework :D

58. alfers101

yep it wasn't but i want to learn this thing. if u dont want me to help then fine :/

59. Tomas.A

why you want to learn C++?

60. agdgdgdgwngo

to build computer viruses!