Search Algorithms in Java - Data Structures

farrightΛογισμικό & κατασκευή λογ/κού

15 Αυγ 2012 (πριν από 5 χρόνια και 3 μήνες)

285 εμφανίσεις

Circular Array

Three integers

1

front

points to where
the first element is

rear

points to where
next element goes

count
= 8

2

front

rear

front

rear

Count: 0

Count: 6

front

rear

Count: 8

front

rear

Count: 9

front

rear

Count: 10

3

3

public void switcher()

{


if (count > 1) // or if (count >= 2)


{


int last;


if ( rear == 0 )


last = items.length
-

1;


else


last = rear
-
1;



Object temp = items[last];


items[last] = items[front];


items[front] = temp;


}

}

4

4

public void switcher()

{


if (count > 1)


{


int last;


// Can we do it without IF?


last = (rear


1 + items.length) % items.length;



Object temp = items[last];


items[last] = items[front];


items[front] = temp;


}

}

5

5

public Object removeBottomStackElement( Stack s,


int stackSize )

{


Stack tempStack = new Stack( stackSize );



if ( s.isEmpty() )


return null;



while ( !s.isEmpty() )


tempStack.push( s.pop() );




Object obj = tempStack.pop();



// s = tempStack; // Have to use loop here!


// Java always pass by value


while (!tempStack.isEmpty())


s.push(tempStack.pop());



return obj;

}

Schedule


Prog5


Group signup (5 points)


Group plan (5 points)


Complete by the due date: Nov 22


Tuesday


Lab PEOC
-

Parameters, Exceptions, Output, Command Line


Wednesday


Test 2 Review


Friday



Test 2

6

Lab PEOC

/**

Test bed main for this class.


@param args
-

not used in this program.

*/

public static void main( String [] args )

{


if (args.length == 0)


{


// display message


return;


}


. . .

}

7

Lab PEOC

Command line

No NetBeans


Compile Java file


javac *.java


Run Java class

java className


Command line parameters

java.exe className arg1 arg2 arg3



8

Lab PEOC

public static void main( String [] args )

{


if (args.length == 0)


{


System.out.println ( "Error. Call as java PEOC <filename>." );


return;


}



String fileName;


fileName = args[0];



int count = Integer.parseInt(args[1]);


double value = Double.parseDouble(args[2]);


. . .

}

9

Binary Search

int [] a = new int[MAX_SIZE];

int size, target;

// a[] is sorted in ascending order


While not done


Find index of the middle element


If middle element = target


return index


else if middle element is smaller


search the second half


else


search the first half


Return
-
1


10

10

10

public int binarySearch ( int a[], int size, int val )

{


int lo, hi, mid;


lo = 0;


hi = size
-

1;



//while ( ? )


while ( lo <= hi )


{


// mid = ?


mid = ( lo + hi ) / 2;


if ( val == a[mid] )


return mid;


else if ( val < a[mid] )


// update lo or hi?


// what’s the new hi?


hi = mid
-

1;


else


lo = mid + 1;


}


return
-
1;

}


11

11

11

public class SortedList

{


private Comparable [] items;


private int num;




public int BinarySearch(Comparable x)


{


int lo = 0, hi = num
-

1;


while ( lo <= hi )


{


int mid = ( lo + hi ) / 2;


int result = items[mid].CompareTo(x);


if ( result == 0 )


return mid;


if ( result > 0 )


hi = mid
-

1;


else


lo = mid + 1;


}


return
-
1;



}


. . .

}








.....

}


12

Tracing
Binary Search

Array items[] has 9 values and sorted in ascending order.

Binary search is used to find a target. Tracing the execution assuming the target is
smaller than all values in the array.





low


mid


high


0


8


4


3


1


0


0


-
1



Terminate when high =
-
1, since low = 0

13

13

13

Exercise


Prog5 Signup

14

14

14