2

I want to sort numbers without using Arrays. I have this:

 Scanner s = new Scanner(System.in);
    System.out.print("Input 3 numbers: ");
    int a = s.nextInt();
    int b = s.nextInt();
    int c = s.nextInt();

    System.out.print("Increscent: ");

    if (a >= b) { 
        int temp; 

        temp = a;
        a = b;
        b = temp;
    }

    if (c < a) 
    {
        System.out.println(c + " " + a + " " + b);
    } else if (c > b) 
    {
        System.out.println(a + " " + b + " " + c);
    } else 
    {
        System.out.println(a + " " + c + " " + b);
    }

But what should I do if I want to use more numbers? Is there some better code or I must use this way all the time?

Simon
  • 45
  • 1
  • 9
  • You'd have to use *some* kind of data structure if you wanted to add a large amount of variables. – Makoto Jul 03 '18 at 19:51
  • Arrays are used for large amounts of variables like this. Why would you not take advantage of them? – Nordii Jul 03 '18 at 19:51
  • 1
    You basically must use an array. There really isn't any other way. You can use `if` statements like you have, but it only works for fixed numbers of elements, and doesn't scale up well past three or four. You could also use a linked list, but arrays are vastly superior. Question: WHY do you not want to use an array? – markspace Jul 03 '18 at 19:51
  • @markspace I want to use arrays, but I'm doing some tasks and I'm required to sort it without them – Simon Jul 03 '18 at 19:56
  • Using Binary Search , divide and conquer it can be done in O(log n) time –  Mar 09 '19 at 12:31

3 Answers3

2

The purpose of arrays are so that you can have multiple of similar data. Like an integer. In hindsight you could probably place hundreds of variables, or you can do it on one line.

what do you think is better?

int a;
int b;
int c;
int d;

or

int a[];

There are methods to have many arguments for example like varargs, for functions. But in the long run those work like arrays anyways.

For terms of sorting algorithsm, there are hundreds of different sorting agorithms, and around a few super common ones.

varargs An example of use of varargs in C

sorting algorithms https://en.wikipedia.org/wiki/Sorting_algorithm

edit: Just realized this was Java, but the same theory applies Java variadic function parameters

Nova Ardent
  • 161
  • 1
  • 16
0

Well, you can do something close to this: Sort 4 numbers without array

Let me help you:

int tmp;

if (a > b) { tmp = a; a = b; b = tmp; }
if (a > c) { tmp = a; a = c; c = tmp; }
if (b > c) { tmp = b; b = c; c = tmp; }

System.out.println(a + " " + b + " " + c);
R. Karlus
  • 2,094
  • 3
  • 24
  • 48
  • This is, btw, basically an insertion sort or a bubble sort done without an array. If you follow the algorithm for a 3 element array and write down the steps, this is what you end up with. That knowledge allows you to use other sort of sorting algorithms or other numbers of elements, if needed. https://en.wikipedia.org/wiki/Insertion_sort – markspace Jul 03 '18 at 20:44
0

Here's the answer check it out

public static long sortNumber(long num,boolean operator) {
        long answer = 0;
        long doneNums  = 0;
        boolean check = true; 
        int zeroCount = 0;
       while(num > 0 ) {
           long temp = num%10;
           if(!operator && check && temp==0)
               zeroCount++;
           else
               check = false;
           long div = num/=10;
           long count = 1;
           count = getMeCount(div, temp, true,operator);
           count += getMeCount(doneNums, temp, false,operator);
           doneNums  = doneNums * 10 + temp ;
           answer += temp * ((long)Math.pow(10, count-1));
       }
       if(zeroCount>0)
           answer = answer * ((long)Math.pow(10, zeroCount));
        return answer;
    }
    
    public static long getMeCount(long num,long temp,boolean checkItOut,boolean operator) {
        long count = checkItOut?1:0;
        while(num > 0) {
               long subTemp = num%10;
               if(operator) {
                   if(checkItOut && subTemp >= temp) {
                       count++;
                   }else if(subTemp > temp)
                       count++;
               }else {
                   if(checkItOut && subTemp <= temp) {
                       count++;
                   }else if(subTemp < temp)
                       count++;
               }
               num/=10;
        }
        return count;
    }

    public static void main(String[] args) {
        
        System.out.println(sortNumber(6732,false));
        System.out.println(sortNumber(6732,true));
    }