I have an assignment for my own implementation of a class to handle the unlimited size integer, and its After comparing the sequence of my implementation, when I measured and sorted the sequence of my concert function for Java's BigIntega, it was a parabol, which means that the time for a large theta of Ө (N2) to run. I do not have a nested loop, so I hope it will be Ө (n) and it does not understand why it is not. I suspect that it may be that I use
string + integer
inside a loop in my add method. I am not sure how this operation is implemented, is it the runtime of Ө (N)? If not, why can not someone tell me why my code is not Ө (n)?
Here is my add method and the constructor calls it.
Public wide integer (string wall) invalid throws Exercise exception {string temp = ""; Boolean pioneer = true; // Valid input to check (int i = 0; i & lt; val.length (); i ++) {if (val.charAt (i) & lt; '0' || Val. Chaat (I) > '9') // Check if each digit is a number from 0 to 9 if the (i! = 0 = val.charAt (i)! = '-') // does not throws points, 'The string's character is the new illegal random exception ("input string should be a number"); } Remove the leading zero for // (int i = 0; i & lt; val.length (); i ++) {if (! Leading || val.charAt (i)! = '0') temp + = Val.charAt (i); If (Well Chat (I)> '0' & amp;;; Val.charAt (i) & lt; = '9') // First non zero score major = false; } If (temp == "") // This happens when the input was a string of zero = temp = "0"; Val = temp; If (val.charAt (0)! = '-') {// no negative sign points = new int [val.length ()]; For (int i = 0; i & lt; val.length (); i ++) {number [i] = (int) (val.charAt (val.length () - 1-i) - 48); // ASCII four '0' == 48} negative = false; } And {number = new int [val.length () - 1]; '-' for the mark number [i-1] = (int) (after val.length ()) (int i = 1; ilait; vine.lamp (); i ++) {// Loop starts for - I) - 48); // ASCII in four '0' == 48} negative = true; }} Add Public Large Integer (Large Integer H) {int carry = 0; Integer size = number Height & gt; H. DIGITS Langhai? Points. Length: H. Digits Laugh; Choose // greater number # string yoga = ""; String horoscope = ""; Int big = 0; Boolean swapped = false; Int temporary; Int sign = 1; Int hsign = 1; // Specify the signal on the basis of the negative; if negative (negative & amp;; h. NHG) marks = -1; If (! Negative & amp; H.H. Negative) hsign = -1; // Compare comparisons 1 means bigger and 1 means h is larger (digits.length> gt; h.digits.length) big = 1; And if (digit length = l = hits.length) is greater = -1; Else {// For equal length (int i = 0; i & lt; digits.length; i ++) {// Both digit arrays have the same length (if digits (digits). Digits.length-1 -i]) {big = 1; break; } If (points [number-long -1]] = digits.length) is temporary = h.digits [i] * hsign + carry; And if (i> = h.digits.length) is the temporary = number [i] * sign + layer; Other temporary = points [i] * sign + h. Digits [i] * HSING + Layer; If (temporal> 9) {// adds digits for the string, the increment increases which is used in the next frequency - = 10; Yoga + = Floating; Lay = 1; } And if (temp & lt; 0) {temp + = 10; Lay = -1; Yoga + = Floating; } And {sum + = temp; Larry = 0; }} If (layer == 1) yoga = = 1; If (negative & amp; h. neagul || swap) yoga + = '-'; // for flip string (int i = 0; i & lt; sum.length (); i ++) {sumFlipped + = sum.charAt (sum.length () - 1 - i); } Massive Integer sumHugeInteger = New Massive Integer (sumFlipped); Return SumHugeInteger; }
No comments:
Post a Comment