Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ScalaMethod #25

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions src/main/scala/DynamicProgramming/ScalaMethod
Original file line number Diff line number Diff line change
@@ -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)
}
}
78 changes: 78 additions & 0 deletions src/main/scala/Sort/CycleSort.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package Sorts;

import static Sorts.SortUtils.less;
import static Sorts.SortUtils.print;

class CycleSort implements SortAlgorithm {


@Override
public <T extends Comparable<T>> 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 extends Comparable<T>> 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);
}

}