This file contains 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
NSArray *sortedArray; | |
sortedArray = [drinkDetails sortedArrayUsingComparator:^NSComparisonResult(id a, id b) { | |
NSDate *first = [(Person*)a birthDate]; | |
NSDate *second = [(Person*)b birthDate]; | |
return [first compare:second]; | |
}]; |
This file contains 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 static int increasingSubsequence(int[]seq){ | |
int[]L=new int[seq.length]; | |
L[0]=1; | |
for(int i=1;i<L.length;i++){ | |
int maxn=0; | |
for(int j=0;j<i;j++){ | |
if(seq[j]<seq[i]&&L[j]>maxn){ | |
maxn=L[j]; | |
} | |
} |
This file contains 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 static int binarySearch(int[]array,int key){ //looks for the index of the element that has value key, returns index. array must be sorted | |
int min=0; //initializes min, max, guess | |
int max=array.length-1; | |
int guess=(min+max)/2; | |
while(array[guess]!=key){ //keeps on looking until key is found | |
if(min==guess||max==guess){//if min=guess, max=guess, the key does not exist in the array | |
return(-1);//returns -1 if key is not found | |
} | |
if(array[guess]>key){//you only have to search between min and guess | |
max=guess; |
This file contains 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 static String primeFactorization(long num){ //method signature. takes long, returns string of factorization | |
String ans=""; //creates the answer | |
for(int i=2;i<=num;i++){ //loops from 2 to the num | |
if(num%i==0){ //checks if i is a divisor of num | |
ans+=i+"*"; //writes i in prime factorization | |
num=num/i; //since it is written down, num=num/i | |
i--; //just in case their are multiple factors of same number. For example, 12=2*2*3 | |
} | |
} | |
return(ans.substring(0,ans.length()-1)); //takes away last asterisk. Factorization of 4=2*2*=>2*2 |
This file contains 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 static int[] gnomeSort(int[] nums){ //takes unsorted array, returns sorted | |
int index=1; //start of search | |
int temp; | |
while(index<nums.length){ //until the array is fully sorted | |
if(nums[index]<nums[index-1]){ //compares nums[index] with nums[index-1]. if smaller, switch. | |
temp=nums[index]; | |
nums[index]=nums[index-1]; | |
nums[index-1]=temp; | |
index--; //must decrease index to recheck. since they have been swapped, the array may still be out of order | |
if(index==0){ //prevents index from going lower than 1 |
This file contains 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 static int GCD(int a, int b){ //method signature. Takes two values and returns the GCD. | |
int temp; | |
if(a==b){ //Euclidean algorithm implementation. Recursion stopping case. | |
return(a); | |
} | |
if(a<b){ //Recursion. if a<b; switch to make a>b | |
temp=a; | |
a=b; | |
b=temp; | |
} |
This file contains 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 static int totient(int num){ //euler's totient function calculator. returns totient | |
int count=0; | |
for(int a=1;a<num;a++){ //definition of totient: the amount of numbers less than num coprime to it | |
if(GCD(num,a)==1){ //coprime | |
count++; | |
} | |
} | |
return(count); | |
} | |
public static int GCD(int a, int b){ //faster euclidean algorithm-see GCD for explanation |
This file contains 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 static Boolean isPrime(int num){ //method signature. returns Boolean, true if number isPrime, false if not | |
if(num==2){ //for case num=2, function returns true. detailed explanation underneath | |
return(true); | |
} | |
for(int i=2;i<=(int)Math.sqrt(num)+1;i++){ //loops through 2 to sqrt(num). All you need to check- efficient | |
if(num%i==0){ //if a divisor is found, its not prime. returns false | |
return(false); | |
} | |
} | |
return(true); //if all cases don't divide num, it is prime. |
This file contains 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 static int fibonacci(int num){ //non-recursive fibonacci. returns the nth fibonacci number. | |
double phi= (1+Math.sqrt(5))/2; | |
return((int)((Math.pow(phi,num)-Math.pow(1-phi,num))/Math.sqrt(5))); //finding fibonnaci number using formula. | |
} | |
public static int fibonacciRec(int num){ //recurive fibonacci. Used to demonstrate fibonacci | |
if(num==1||num==2){ //stopping case. F(1)=1, F(2)=1 by definition. | |
return(1); | |
} | |
return(fibonacciRec(num-1)+fibonacciRec(num-2)); //definition of Fibonacci sequence: F(n)=F(n-1)+F(n-2) | |
} |
This file contains 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 static void quicksort(int[] array) | |
{ | |
array = quicksort(array, 0, array.length-1); | |
} | |
private static int[] quicksort(int[] array, int lo, int hi) { | |
if (hi > lo) | |
{ | |
int partitionPivotIndex = (int)(Math.random()*(hi-lo) + lo); | |
int newPivotIndex = partition(array, lo, hi, partitionPivotIndex); | |
quicksort(array, lo, newPivotIndex-1); |
OlderNewer