Skip to content

Instantly share code, notes, and snippets.

View gaurav1780's full-sized avatar

Gaurav Gupta gaurav1780

  • Macquarie University
  • Sydney
View GitHub Profile
@gaurav1780
gaurav1780 / linearSearch1.java
Last active September 20, 2017 01:14
LinearSearch1
/**
* @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!
/**
* @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!
/**
* @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!
/**
* @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
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;
public class Node {
private Node next;
private int data;
public int getData() {
return data;
}
public Node getNext() {
return next;
}
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;
class Node {
private Node next;
private int data;
public int getData() {
return data;
}
public Node getNext() {
return next;
}
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);
//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];