C# or C++ better for game design?While there are hundreds of programming languages that can be used for gaming applications, game developers use only a few. C sharp (C #) and C + + are among the most popular languages in game design, which begs the question, “Is C# or C++ better for game design?” We...
Demonstrating split string method in Java
import java.util
/**
* I created two separate versions of the algorithm
*
* quicksort_create : creates new arrays and concatenates them.
* This function runs slower, but it has good speedup when you parallelize it.
*
*
* quickSort_inplace: uses the "inplace" version of quicksort, where instead of creating new arrays, you sort the original one inplace
* This function runs faster, but the speedup is poor, because when many threads try to access the same memory,
* there is actually a performance bottleneck
*
* Both of them run in parallel with .par functions
* Both of them are recursive
* Both of them have a "currentDepth" parameter to see how far down the recursive tree they are
* After a certain level, the methods no loger spawn new threads, to prevent creating too many threads
*
* Example: if you sort a 1000 elements arrays, you shouldn't actually create that many threads
*
* To see serial performance in any of them, set the max_depth variable to -1, and it will only run in single threaded mode
*
*/
object QuickSort extends App {
// create an array of random ints
val r = scala.util.Random
valrandomArray = (for (_ <- 1 to 1000000) yield r.nextInt(10000)).toArray
valcorrectSorted = randomArray.sortWith(_ < _)
var sortedArray = time {
quickSort_inplace(randomArray)
// quicksort_create(randomArray)
}
if (sortedArray.deep == correctSorted.deep) println("correct")
def time[R](block: => R): R = {
val t0 = System.nanoTime()
val result = block // call-by-name
val t1 = System.nanoTime()
println("Elapsed time: " + (t1 - t0) / 1000000 + "ms")
result
}
/** Method that sorts the array
* Called "create" because it doesn't work in place, but instead it creates new arrays and concatenates them
*
* @param array : input array, may contain duplicates
* @return A sorted array
*/
def quicksort_create(array: Array[Int]): Array[Int] = {
/**
* @param array Sub array
* @param depth : the current recursivity level.
* Also indicates how many tasks have been spawn so far
* Used to limit the creation of too many tasks to avoid unnecessary overhead
* @return
*/
def inner_sort(array: Array[Int], depth: Int): Array[Int] = {
if (array.length<= 1) return array
// val pivot = array(scala.util.Random.nextInt(array.length))
val pivot = array(array.length / 2)
val pivots = array.filter(pivot ==)
val (left, right) = array.filter(pivot !=).partition(pivot >=)
valnew_depth = depth + 1
// set this variable to -1 to see serial version
// for best performance, this should be set somewhere between 3 and 10, depending on your system and the array size
valmax_depth = 4
if (depth inner_sort(subarray, new_depth))
* then
* results = a list that contains the 2 subarrays, but sorted
*
* It is the .par that makes it parallel
* It will execute each sort inner_sort call on a different thread
* we call .toArray at the end as a join of sorts, to make sure that both tasks are finished
*/
val results = arrays_to_sort.par.map(subarray =>inner_sort(subarray, new_depth)).toArray
results(0) ++ pivots ++ results(1)
}
else {
inner_sort(left, new_depth) ++ pivots ++ inner_sort(right, new_depth)
}
}
inner_sort(array, 0)
}
/**
* Array sorting method
* Called inplace because it modifies the original array inplace instead of creating new data structures
*
* @param array : the array to be sorted, may contain duplicates
* @return : new array, sorted
*/
def quickSort_inplace(array: Array[Int]): Array[Int] = {
def swap(i: Int, j: Int) {
val t = array(i)
array(i) = array(j)
array(j) = t
}
/**
* Recursive method
*
* @param interval : the interval in which this call will sort
* For example, if it's called with the interval (2,10)
* It will sort the element of the array between indexes 2 and 10
* The first call to this method is made with the interval(0,array.lenght)
* - it will sort the whole array
* @param depth : same as before, current depth to avoid creating too many threads
*/
def inner_sort(interval: (Int, Int), depth: Int) {
val l = interval._1
val r = interval._2
val pivot = array((l + r) / 2)
var i = l
var j = r
// partion in place, standard algorithm
while (i<= j) {
while (array(i) < pivot) i += 1
while (array(j) > pivot) j -= 1
if (i<= j) {
swap(i, j)
i += 1
j -= 1
}
}
valmax_depth = -1
if (depth inner_sort(x, depth + 1))
}
else {
//make the recursive calls serial
if (l < j) inner_sort((l, j), depth + 1)
if (i< r) inner_sort((i, r), depth + 1)
}
}
inner_sort((0, array.length - 1), 0)
array
}
}
Test lists in Java
/**
*
*/
package testdatastructures;
import com.chuckkann.datastructures.DataStructureException;
import com.chuckkann.datastructures.DsIterator;
import com.chuckkann.datastructures.IndexedList;
import com.chuckkann.datastructures.indexedlist.ArrayList;
import com.chuckkann.datastructures.indexedlist.DoubleLinkedList;
import com.chuckkann.datastructures.indexedlist.InArrayLinkedList;
import com.chuckkann.datastructures.indexedlist.LinkedList;
/**
* @author Charl
*
*/
public class TestList {
/**
* @param args
*/
public static void main(String[] args) {
IndexedList list;
// Generate a list of size 4 and fill it.
// adding element, and adding at an index.
// Note: this tests the stringify function,
// and all of the DSIterator and Iterator
// functionality stringify relies on this
// logic.
System.out.println("DoubleLinkedList cases 1 - adding individual items");
System.out.println("DoubleLinkedList case 1a");
list = new DoubleLinkedList();
System.out.println("DoubleLinkedList case 1b");
list.add("input 1");
System.out.println("DoubleLinkedList case 1c");
list.add("input 2");
System.out.println("DoubleLinkedList case 1d");
list.add(0, "input 3");
System.out.println(list.debug());
list.remove(1);
System.out.println(list.debug());
System.out.println("DoubleLinkedList case 1e");
list.add(1, "input 4");
System.out.println("DoubleLinkedList case 1f");
list.add("input 5");
System.out.println(list.stringify());
// Add a collection to itself. This is illegal
System.out.println("DoubleLinkedList case 2");
try {
list.add(list);
System.out.println(list.stringify());
} catch(DataStructureExceptiondse){
dse.printStackTrace();
}
// Copy a Collection, and add it to the current Collection
System.out.println("DoubleLinkedList case 3");
try {
//
IndexedList list1 = new ArrayList();
list1.add(list);
list.add(list1);
System.out.println(list.stringify());
} catch(DataStructureExceptiondse){
dse.printStackTrace();
}
// Make an array from the collection, and add it
// to the current Collection.
System.out.println("DoubleLinkedList case 4");
String[] list1 = new String[list.size()];
list.toArray(list1);
list.add(list1);
System.out.println(list.stringify());
// Find first instance of element input 2
System.out.println("DoubleLinkedList case 5");
System.out.println(list.indexOf("input 2"));
System.out.println(list.lastIndexOf("input 2"));
// Find an element not in the list
System.out.println("DoubleLinkedList case 6");
System.out.println(list.indexOf("la-ti-dah"));
System.out.println(list.lastIndexOf("la-ti-dah"));
// Remove first instance of input 2
System.out.println("DoubleLinkedList case 7");
System.out.println(list.remove(list.indexOf("input 2")));
System.out.println(list.stringify());
// Retrieve an element from the list
System.out.println("DoubleLinkedList case 8");
System.out.println(list.retrieve(3));
// Remove item 1 then 0
System.out.println("DoubleLinkedList case 9");
System.out.println(list.stringify());
list.remove(1);
list.remove(0);
list.remove(12);
System.out.println(list.debug());
list.add("input new");
System.out.println(list.debug());
// Set a value in the list
System.out.println("DoubleLinkedList case 10");
list.set(3, "la-ti-dah");
System.out.println(list.stringify());
System.out.println(list.debug());
// Test Forward iterator
System.out.println("DoubleLinkedList case 11");
DsIterator iterator = list.iterator();
while(iterator.hasNext()) {
System.out.print(iterator.next() + ", ");
}
System.out.println();
// Test Backwards iterator
System.out.println("DoubleLinkedList case 12");
DsIteratorbackIterator= list.iterator();
backIterator.setIteratorToEnd();
while(iterator.hasPrevious()) {
System.out.print(iterator.previous() + ", ");
}
System.out.println();
}
}