0
\$\begingroup\$

I'm printing a diamond shape in Java. It's working fine, but is there a way to re-write this code to make it faster?

Scanner sc = new Scanner(System.in);

int n = sc.nextInt();

sc.close();

String ns = String.valueOf(n);
int z = n/2;

int k = n%2;


for (int j = 0 ; j<n;j++) {

    for (int i = 0; i < z; i++)
        System.out.print(" ");
    for (int i = 0; i < k ; i++){
        System.out.print("*");
    if (i==k-1)
        System.out.print("\n");
    }

    if ((k-n)==0)
        break;
    z--;
    k=k+2;

}

 z = (n)/2;

 k = (n)%2;
int w = k;
int y = n-2;

for (int j = 0 ; j<z  ;j++) {

    for (int i = 0 ; i<w;i++)
    System.out.print(" ");


    for (int i = 0 ; i< y ;i++)
        System.out.print("*");
    w=w+1;
    y=y-2;

    System.out.print("\n");

}
\$\endgroup\$
8
  • 3
    \$\begingroup\$ Welcome to CR! Can you define what you expect by "faster"? This is a pretty vague term. Fewer operations (likely)? Improved Big O (unlikely)? \$\endgroup\$
    – ggorlen
    Commented Mar 9, 2019 at 18:46
  • \$\begingroup\$ I meant by fewer operations \$\endgroup\$
    – Victoria
    Commented Mar 9, 2019 at 21:49
  • \$\begingroup\$ Do you use an IDE? Asking because the code is not indented as usual - one point of perceivability. \$\endgroup\$
    – greybeard
    Commented Mar 10, 2019 at 7:33
  • \$\begingroup\$ Yes I use IntelliJ , sorry for inconvenience but this is my first question to ask here and I don’t know how to indent right. :) \$\endgroup\$
    – Victoria
    Commented Mar 10, 2019 at 12:37
  • \$\begingroup\$ As already said, you need to be more specific about what is it that you expect. If you just want to tinker with the code and see how much shorter you can make it, then move your question to codegolf.stackexchange.com \$\endgroup\$
    – givanse
    Commented Mar 10, 2019 at 20:43

2 Answers 2

1
\$\begingroup\$

After reviewing your code, I'll point out that:

It would be better if you used more descriptive variable names. It improves readability and might even help you better understand your own code, ex:

int width = sc.nextInt(); // or whatever it represents

Always use curly braces for if and for statements, even if they wrap around a single statement. Shorter code isn't faster, but makes it easier to introduce bugs. You never pay a penalty for extra curly braces or parentheses, so use them as much as you want to clarify intention. For example this snippet can be ambiguous:

for (int i = 0 ; i< y ;i++)
    System.out.print("*");
w=w+1;
y=y-2;

Did you mean

for (int i = 0 ; i< y ;i++) {
    System.out.print("*");
    w=w+1;
    y=y-2;
}

or

for (int i = 0 ; i< y ;i++) {
    System.out.print("*");
}
w=w+1;
y=y-2;
\$\endgroup\$
1
  • \$\begingroup\$ I meant this one : for (int i = 0 ; i< y ;i++) { System.out.print("*"); } w=w+1; y=y-2; ////////// \$\endgroup\$
    – Victoria
    Commented Mar 10, 2019 at 22:17
1
\$\begingroup\$

Your code works well for odd inputs:

9
    *
   ***
  *****
 *******
*********
 *******
  *****
   ***
    *

However, the output is weirdly wrong for even inputs:

8
       **
  ****
 ******
********
******
 ****
  **
 

If you want to improve performance (in a way that would be noticeable to benchmarks, but not to the human eye), the most important change you should make is to reduce the number of System.out.print() calls. Each .print() results in a system call, which would require synchronization (acquiring a lock, in case your program is multithreaded, so that the output of multiple simultaneous .print() calls won't get interleaved with each other), as well as context switching (putting your Java code on hold to run code in the operating system kernel). All of that overhead is huge, compared to any other change you can make to your algorithm.

Therefore, for better performance, you should create a StringBuilder and repeatedly append the entire diamond to it, then call System.out.println() just once per line, or, better yet, just once at the end. Here is an example that prints an hourglass shape, which is closely related to your diamond shape.

\$\endgroup\$
1
  • \$\begingroup\$ And yes I wanted an odd integer not even. I should've said that, apologies. \$\endgroup\$
    – Victoria
    Commented Mar 11, 2019 at 20:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.