diff --git a/src/main/scala/DynamicProgramming/ScalaMethod b/src/main/scala/DynamicProgramming/ScalaMethod new file mode 100644 index 0000000..217f764 --- /dev/null +++ b/src/main/scala/DynamicProgramming/ScalaMethod @@ -0,0 +1,51 @@ +object NewtonsMethod { + + def main(args: Array[String]) { + driver + } + + /** + * A "driver" function to test Newton's method. + * Start with (a) the desired f(x) and f'(x) equations, + * (b) an initial guess and (c) tolerance values. + */ + def driver { + // the f(x) and f'(x) functions + val fx = (x: Double) => 3*x + math.sin(x) - math.pow(math.E, x) + val fxPrime = (x: Double) => 3 + math.cos(x) - math.pow(Math.E, x) + val initialGuess = 0.0 + val tolerance = 0.00005 + // pass f(x) and f'(x) to the Newton's Method function, along with + // the initial guess and tolerance + val answer = newtonsMethod(fx, fxPrime, initialGuess, tolerance) + println(answer) + } + + /** + * Newton's Method for solving equations. + * @todo check that |f(xNext)| is greater than a second tolerance value + * @todo check that f'(x) != 0 + */ + def newtonsMethod(fx: Double => Double, + fxPrime: Double => Double, + x: Double, + tolerance: Double): Double = { + var x1 = x + var xNext = newtonsMethodHelper(fx, fxPrime, x1) + while (math.abs(xNext - x1) > tolerance) { + x1 = xNext + println(xNext) // debugging (intermediate values) + xNext = newtonsMethodHelper(fx, fxPrime, x1) + } + xNext + } + + /** + * This is the "x2 = x1 - f(x1)/f'(x1)" calculation + */ + def newtonsMethodHelper(fx: Double => Double, + fxPrime: Double => Double, + x: Double): Double = { + x - fx(x) / fxPrime(x) + } +} diff --git a/src/main/scala/Sort/CycleSort.scala b/src/main/scala/Sort/CycleSort.scala new file mode 100644 index 0000000..c8e6b28 --- /dev/null +++ b/src/main/scala/Sort/CycleSort.scala @@ -0,0 +1,78 @@ +package Sorts; + +import static Sorts.SortUtils.less; +import static Sorts.SortUtils.print; + +class CycleSort implements SortAlgorithm { + + + @Override + public > T[] sort(T[] arr) { + int n = arr.length; + + // traverse array elements + for (int j = 0; j <= n - 2; j++) { + // initialize item as starting point + T item = arr[j]; + + // Find position where we put the item. + int pos = j; + for (int i = j + 1; i < n; i++) + if (less(arr[i], item)) pos++; + + // If item is already in correct position + if (pos == j) continue; + + // ignore all duplicate elements + while (item.compareTo(arr[pos]) == 0) + pos += 1; + + // put the item to it's right position + if (pos != j) { + item = replace(arr, pos, item); + } + + // Rotate rest of the cycle + while (pos != j) { + pos = j; + + // Find position where we put the element + for (int i = j + 1; i < n; i++) + if (less(arr[i], item)){ + pos += 1; + } + + + // ignore all duplicate elements + while (item.compareTo(arr[pos]) == 0) + pos += 1; + + // put the item to it's right position + if (item != arr[pos]) { + item = replace(arr, pos, item); + } + } + } + + return arr; + } + + private > T replace(T[] arr, int pos, T item){ + T temp = item; + item = arr[pos]; + arr[pos] = temp; + return item; + } + + + + public static void main(String[] args) { + Integer arr[] = { 4, 23, 6, 78, 1, 26, 11, 23 , 0, -6, 3, 54, 231, 9, 12 }; + CycleSort cycleSort = new CycleSort(); + cycleSort.sort(arr); + + System.out.println("After sort : "); + print(arr); + } + +}