How to resolve the algorithm Sort numbers lexicographically step by step in the Java programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Sort numbers lexicographically step by step in the Java programming language

Table of Contents

Problem Statement

Given an integer   n,   return   1──►n   (inclusive)   in lexicographical order.

Show all output here on this page.

Given   13, return:   [1,10,11,12,13,2,3,4,5,6,7,8,9].

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Sort numbers lexicographically step by step in the Java programming language

Java Program to Generate Numbers in Lexicographical Order

This Java program demonstrates how to generate numbers in lexicographical order, which is also known as dictionary order or alphabetical order. The program uses Java 8 streams to achieve this in an efficient and concise manner.

Key Implementation Details:

  1. lexOrder() Method:

    • This method takes an integer n as input and returns a list of integers in lexicographical order within the range [1, n].
    • It adjusts the range [1, n] to [n, 1] if n is negative.
    • It uses Java 8 streams to convert the range of integers to strings, sort them lexicographically, and convert them back to integers.
    • The resulting list is collected and returned.
  2. main() Method:

    • The program defines an array ints containing various integer values.
    • For each integer n in the array, it invokes the lexOrder() method and prints the generated list in lexicographical order.
    • The program demonstrates the lexicographical ordering of numbers in different scenarios, including positive, negative, and zero values.

Output:

The output of the program is as follows:

In lexicographical order:

 0: [0]
 5: [1, 2, 3, 4, 5]
13: [1, 10, 11, 12, 13, 2, 3, 4, 5, 6, 7, 8, 9]
21: [1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 3, 4, 5, 6, 7, 8, 9]
-22: [-22]

This demonstrates the program's ability to generate numbers in lexicographical order for various input values.

Source code in the java programming language

import java.util.List;
import java.util.stream.*;

public class LexicographicalNumbers {

    static List<Integer> lexOrder(int n) {
        int first = 1, last = n;
        if (n < 1) {
            first = n;
            last = 1;
        }
        return IntStream.rangeClosed(first, last)
                        .mapToObj(Integer::toString)
                        .sorted()
                        .map(Integer::valueOf)
                        .collect(Collectors.toList());
    }

    public static void main(String[] args) {
        System.out.println("In lexicographical order:\n");
        int[] ints = {0, 5, 13, 21, -22};
        for (int n : ints) {
           System.out.printf("%3d: %s\n", n, lexOrder(n));
        }
    }
}


  

You may also check:How to resolve the algorithm Ordered words step by step in the Liberty BASIC programming language
You may also check:How to resolve the algorithm Random Latin squares step by step in the Ring programming language
You may also check:How to resolve the algorithm Roman numerals/Encode step by step in the OCaml programming language
You may also check:How to resolve the algorithm Hofstadter Figure-Figure sequences step by step in the Euler Math Toolbox programming language
You may also check:How to resolve the algorithm Conditional structures step by step in the Ruby programming language