algorithm time analysis examples

7
ALGORITHM TIME ANALYSIS EXAMP Salim Malakouti

Upload: thane-mccullough

Post on 31-Dec-2015

28 views

Category:

Documents


2 download

DESCRIPTION

Algorithm Time Analysis Examples. Salim Malakouti. Linear Search. public int count( int [] array, int item) { for ( int i = 0; i < array.length ; i ++) { if ( array [i] == item ) return true ; } return count ; }. Remove from the list?. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Algorithm Time Analysis Examples

ALGORITHM TIME ANALYSIS EXAMPLES

Salim Malakouti

Page 2: Algorithm Time Analysis Examples

Linear Search

public int count(int[] array, int item) {

for (int i = 0; i < array.length; i++) {

if (array[i] == item) return true; }

return count;

}

Page 3: Algorithm Time Analysis Examples

Remove from the list?

public void remove(ArrayList<Integer> list,Integer item){

for (int i = 0; i < list.size(); i++) {

if(list.get(i).equals(item)){ list.remove(i);

}

} }

Page 4: Algorithm Time Analysis Examples

Removing from a list

What is a better solution?What is its time complexity?

Page 5: Algorithm Time Analysis Examples

Finding A substring

String source = “I hope you are not tired. home”

String pattern = “home”

Exists?

What is the algorithm?

Page 6: Algorithm Time Analysis Examples

Finding sub pattern public boolean findSubString(String source, String pattern){ if(pattern==null||source==null) return false; if(pattern.length()==0||source.length()==0) return false;

for (int i = 0; i < source.length(); i++) { if(source.charAt(i)==pattern.charAt(i)){ boolean t=true; for (int j = i; j < pattern.length(); j++) { if(source.charAt(j)!=pattern.charAt(j-i)){ t=false; break; } } if(t) return true; } } return false; }

Page 7: Algorithm Time Analysis Examples

Boolean search