private static void BinarySearchSqrt2(int x, InOut.Ergebnis erg) { int it = 0, curr = 0; if (!Helfer.BinarySearch(0, x / 2, ref curr, ref it, num => (num * num).CompareTo(x))) { curr = -1; } erg.Setze(curr, it, Complexity.LOGARITHMIC, Complexity.CONSTANT); }
private static Pos BinarySearchMatrix2(Helfer.Matrix <int> mat, int tar, ref int iterations, int current = 0) { if (Helfer.BinarySearch(0, mat.Length, ref current, ref iterations, (ind) => mat.GetElementAtIndex(ind).CompareTo(tar))) { return(new Pos(mat.GetCol(current) + 1, mat.GetRow(current) + 1)); } else { return(new Pos(-1, -1)); } }
//SOL public static void TwoSum_BinarySearch(Input inp, InOut.Ergebnis erg) { int[] arr = inp.arr; int tar = inp.target; int it = 0; Output outp = new Output(null); for (int i = 0; i < arr.Length - 1; i++, it++) { int curr = 0; if (Helfer.BinarySearch(i + 1, arr.Length - 1, ref curr, ref it, ind => arr[ind].CompareTo(tar - arr[i]))) { outp = new Output(new int[] { i + 1, curr + 1 }); outp.exp = arr[i] + " + " + arr[curr] + " = " + tar; break; } } // log n + log n-1 + log n-2 ... => log (n * (n-1) *(n-2)...) log (n!) => log (n) erg.Setze(outp, it, Complexity.LINEARITHMIC, Complexity.CONSTANT, "Time is actually Log ((n-1)!)"); }