Slick Slick - 3 months ago 8
Java Question

how to input n numbers and print it in ascending order without using array

I was trying to create a program that takes

n
number of input in a loop and capable of showing output in ascending order. The main theme of program is it should not use arrays.

I tried it by creating a program that takes input and shows smallest number first then 2nd smallest and 3rd smallest and so on. But I am new to programming so my program could do only like 1st smallest and it does not work.




I also saw heap data structure in one of the similar questions but couldn't figure out how it can work without arrays.

Please help me how can we use heap data structure or give me a good suggestion to do it.

Answer

You could use Streams:

    StreamSupport.stream(
            Spliterators.spliteratorUnknownSize(new Scanner(System.in), Spliterator.ORDERED),
            false)
            .limit(10)
            .sorted()
            .forEach(System.out::println);
Comments