This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param arr: array in which we need to look for target | |
* @param target: item to be searched in array arr | |
* @return true if target exists in arr, false if it doesn't | |
*/ | |
boolean contains(int[] arr, int target) { | |
if(arr == null) | |
return false; | |
for(int i=0; i < arr.length; i++) { | |
if(arr[i] == target) { //found it! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param arr: array in which we need to look for target | |
* @param target: item to be searched in array arr | |
* @return index of the first occurrence of target in arr (-1 if not found) | |
*/ | |
int indexOf(int[] arr, int target) { | |
if(arr == null) | |
return -1; | |
for(int i=0; i < arr.length; i++) { | |
if(arr[i] == target) { //found it! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param arr: array in which we need to look for target | |
* @param target: item to be searched in array arr | |
* @return index of the ***LAST*** occurrence of target in arr (-1 if not found) | |
*/ | |
int lastIndexOf(int[] arr, int target) { | |
if(arr == null) | |
return -1; | |
for(int i=arr.length - 1; i >= 0; i--) { //back to front | |
if(arr[i] == target) { //found it! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param arr: array in which item should be searched. assumed to be sorted in ascending order | |
* @param: target: item that needs to be searched | |
* @return index at which target found, -1 if not found | |
*/ | |
int binarySearch(int[] arr, int target) { | |
if(arr == null) //we meet again, old friend! | |
return -1; | |
int first = 0; //starting index | |
int last = arr.length - 1; //ending index |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package classesObjects1.oneStepAtATime.iteration8toString; | |
public class Rectangle { | |
private double width, height; | |
public void setWidth(double w) { | |
if(w < 0) //invalid | |
width = -w; | |
else //valid | |
width = w; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Node { | |
private Node next; | |
private int data; | |
public int getData() { | |
return data; | |
} | |
public Node getNext() { | |
return next; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class BuildingList { | |
public static void main(String[] args) { | |
Node node = null; | |
int[] data = {70, 20, 90, 30}; | |
for(int i=0; i < data.length; i++) { | |
Node temp = new Node(data[i], node); | |
node = temp; | |
} | |
Node current = node; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node { | |
private Node next; | |
private int data; | |
public int getData() { | |
return data; | |
} | |
public Node getNext() { | |
return next; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int n = (int)random(5, 20); | |
float ps; | |
void setup() { | |
size(400, 400); | |
background(255); | |
strokeWeight(3); | |
println("number of partitions: "+n); | |
ps = width*1.0 / n; | |
println("partition size: "+ps); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//helper 1 | |
public static void swap(int[] a, int idx1, int idx2) { | |
if (a == null) nothing to do | |
return; | |
if (idx1 < 0 || idx1 >= a.length) //invalid index 1 | |
return; | |
if (idx2 < 0 || idx2 >= a.length) //invalid index 2 | |
return; | |
int temp = a[idx1]; | |
a[idx1] = a[idx2]; |
OlderNewer