Skip to main content
added 251 characters in body
Source Link
dfhwze
  • 14.2k
  • 3
  • 40
  • 101

Performance

You have implemented Floyd’s Cycle-Finding Algorithm which adheres to \$0(1)\$ storage space. An alternative exists Brent’s Cycle Detection Algorithm which uses the same storage space. Check out this review on Computer Science SE for a comparison. It appears in general, Brent's algorithm is faster.

According to Brent's paper, the complexity of Floyd's algorithm is between 3max(m,n) and 3(m+n), and that of Brent's is at most 2max(m,n)+n, which is always better than 3max(m,n).

courtesy of Yuval Filmus' answer at CS

Style Guidelines

  • use var to declare a variable, specially when the type can be inferred from code
  • use a separate line for declaring attributes on top of members
  • use a white space after a method name and the opening parenthesis
  • use a white space after the while statement
  • use white space around operators (!=)
  • remove redundant nested else branches if the if branch always performs a return

Performance

You have implemented Floyd’s Cycle-Finding Algorithm which adheres to \$0(1)\$ storage space. An alternative exists Brent’s Cycle Detection Algorithm which uses the same storage space. Check out this review on Computer Science SE for a comparison. It appears in general, Brent's algorithm is faster.

Style Guidelines

  • use var to declare a variable, specially when the type can be inferred from code
  • use a separate line for declaring attributes on top of members
  • use a white space after a method name and the opening parenthesis
  • use a white space after the while statement
  • use white space around operators (!=)
  • remove redundant nested else branches if the if branch always performs a return

Performance

You have implemented Floyd’s Cycle-Finding Algorithm which adheres to \$0(1)\$ storage space. An alternative exists Brent’s Cycle Detection Algorithm which uses the same storage space. Check out this review on Computer Science SE for a comparison. It appears in general, Brent's algorithm is faster.

According to Brent's paper, the complexity of Floyd's algorithm is between 3max(m,n) and 3(m+n), and that of Brent's is at most 2max(m,n)+n, which is always better than 3max(m,n).

courtesy of Yuval Filmus' answer at CS

Style Guidelines

  • use var to declare a variable, specially when the type can be inferred from code
  • use a separate line for declaring attributes on top of members
  • use a white space after a method name and the opening parenthesis
  • use a white space after the while statement
  • use white space around operators (!=)
  • remove redundant nested else branches if the if branch always performs a return
Source Link
dfhwze
  • 14.2k
  • 3
  • 40
  • 101

Performance

You have implemented Floyd’s Cycle-Finding Algorithm which adheres to \$0(1)\$ storage space. An alternative exists Brent’s Cycle Detection Algorithm which uses the same storage space. Check out this review on Computer Science SE for a comparison. It appears in general, Brent's algorithm is faster.

Style Guidelines

  • use var to declare a variable, specially when the type can be inferred from code
  • use a separate line for declaring attributes on top of members
  • use a white space after a method name and the opening parenthesis
  • use a white space after the while statement
  • use white space around operators (!=)
  • remove redundant nested else branches if the if branch always performs a return