Imagine that you have ten different variables with decimal values and you have to sort them from lowest to highest. I am familiar with the different sorting alogorithms using different programming languages, but in this case, I have to build the algorithm in an end-user application that just allow to enter some especific constructions: it allows to use "for", "while", "if", but nothing to do with arrays, it means, it cannot deal with something like a[i] where "a" is an array. Please, can anybody give me a clue?. Thanks a lot!
Asked
Active
Viewed 112 times
-1
-
Your problem is not clear. Firstly, if you cannot do write on `a`, you cannot do inplace sorting. You can make a copy of it, sort it and return that array. Or if you have to find the min/ max element as the input is given to you, use `heap` – vish4071 Apr 26 '16 at 07:46
-
So you are trying to sort in a language that doesn't support arrays? – M Oehm Apr 26 '16 at 07:50
-
1This solves your problem http://stackoverflow.com/questions/25070577/sort-4-numbers-without-array . Extend 4 with your number. – Karthikeyan Gopall Apr 26 '16 at 07:54
-
Thanks. Yes, the problem is that I have to deal with an application that allows you to create scripts but these scripts are not provided with the usual elements/instructions that you can use when you use a full language. So, in that sense, it is like I use a language that doesn't support arrays. – chufabit Apr 26 '16 at 07:56
-
Thanks a lot @KarthikeyanGopall, I was thinking something like that, but in my case it is a 10-variables problem, so writing the code can be a pain. I just wondering if it is possible to deal with it in a more efficient way. – chufabit Apr 26 '16 at 07:59
-
Definitely, I think I can deal with the problem using the solution by @KarthikeyanGopall, I have to use 32 comparators, that is a tolerable pain : ) Thanks again! – chufabit Apr 26 '16 at 08:09
2 Answers
1
you mean something like this ?
c++ code :
void sort_4(int *a1, int *a2, int *a3, int *a4)
{
if (a1 == NULL) return;
if (a2 == NULL) return;
if (*a2 < *a1) swap(*a1, *a2);
sort_5(a1, NULL, NULL, NULL);
if (a3 == NULL) return;
if (*a2 < *a3) swap(*a2, *a3);
sort_5(a1, a2, NULL, NULL);
if (a4 == NULL) return;
if (*a4 < *a3) swap(*a3, *a4);
sort_5(a1, a2, a3, NULL);
}
you can expand it to 10 elements by copy-and-paste or by a code generation script.

Ke Yang
- 218
- 1
- 6
-
Thanks @Ke Yang, but in this case I cannot invoke a method, no recursively way. – chufabit Apr 26 '16 at 08:07
-
then you might need to use a large amount of code to implement the sort. for example, about 50 times compare for 10 elements using bubble sort. You can write a script to generate the code. – Ke Yang Apr 26 '16 at 08:29
0
Well, you could hard-code a bubble sort. For example, imagine you have 10 variables, a, b, c, d, e, f, g, h, i, j
:
for (int x = 0; x < 9; ++x)
{
if (a > b) swap(a,b);
if (b > c) swap(b,c);
if (c > d) swap(c,d);
if (d > e) swap(d,e);
if (e > f) swap(e,f);
if (f > g) swap(f,g);
if (g > h) swap(g,h);
if (h > i) swap(h,i);
if (i > j) swap(i,j);
}
That's not terribly efficient, but you're not going to notice the inefficiency in a UI application that only has 10 or so items. You can make it slightly more efficient by nesting the conditionals:
if (x < 9)
{
if (a > b) swap(a,b);
if (x < 8)
{
if (b > c) swap(b,c);
if (x < 7)
{
....
But that gets unwieldy in a hurry and again the small efficiency it gives you won't be noticed for such a small list.

Jim Mischel
- 131,090
- 20
- 188
- 351