TCC102-Unit1 - Part - 2 - Searching - Sorting - Recursion

You might also like

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 26

Ap pl ic at ions of Arr ay s:

Sea rc hin g, S ort in g and


Re cursi on
Java Programming:
From Problem Analysis to Program Design,
Second Edition
Learning Objectives
 Learn how to implement the sequential search
algorithm.
 Explore how to sort an array using insertion sort
algorithms.
 Learn how to implement the binary search algorithm.
 Learn more about manipulating strings using the
class String.

Java Programming: From Problem Analysis to Program Design, Second Edition 2


List Processing
 List: A set of values of the same type.
 Basic operations performed on a list:
 Search list for given item.
 Sort list.
 Insert item in list.
 Delete item from list.

Java Programming: From Problem Analysis to Program Design, Second Edition 3


Search
 Necessary components to search a list:
 Array containing the list.
 Length of the list.
 Item for which you are searching.
 After search completed:
 If item found, report “success” and return location in
array.
 If item not found, report “failure.”

Java Programming: From Problem Analysis to Program Design, Second Edition 4


Search
public static int seqSearch(int[] list,
int listLength, int searchItem)
{
int loc;
boolean found = false;
for (loc = 0; loc < listLength; loc++)
if (list[loc] == searchItem)
{
found = true;
break;
}
if (found)
return loc;
else
return -1;
}

Java Programming: From Problem Analysis to Program Design, Second Edition 5


Insertion Sort
The insertion sort algorithm sorts the list by moving each element
to its proper place.

Java Programming: From Problem Analysis to Program Design, Second Edition 6


Insertion Sort

Java Programming: From Problem Analysis to Program Design, Second Edition 7


Insertion Sort

Java Programming: From Problem Analysis to Program Design, Second Edition 8


Insertion Sort

Java Programming: From Problem Analysis to Program Design, Second Edition 9


Insertion Sort
public static void insertionSort(int[] list,
int noOfElements)
{
int firstOutOfOrder, location;
int temp;
for (firstOutOfOrder = 1;
firstOutOfOrder < noOfElements;
firstOutOfOrder++)
if (list[firstOutOfOrder] < list[firstOutOfOrder - 1])
{
temp = list[firstOutOfOrder];
location = firstOutOfOrder;
do
{
list[location] = list[location - 1];
location--;
}
while(location > 0 && list[location - 1] > temp);
list[location] = temp;
}
} //end insertionSort
Java Programming: From Problem Analysis to Program Design, Second Edition 10
Binary Search Algorithm
 Can only be performed on a sorted list.
 Search item is compared with middle element of
list.
 If search item < middle element of list, search is
restricted to first half of the list.
 If search item > middle element of list, search is
restricted to second half of the list.
 If search item = middle element, search is
complete.

Java Programming: From Problem Analysis to Program Design, Second Edition 11


Binary Search Algorithm
Determine whether 75 is in the list.

Java Programming: From Problem Analysis to Program Design, Second Edition 12


Binary Search Algorithm
public static int binarySearch(int[] list, int listLength,
int searchItem)
{
int first = 0;
int last = listLength - 1;
int mid;
boolean found = false;
while (first <= last && !found)
{
mid = (first + last) / 2;
if (list[mid] == searchItem)
found = true;
else
if (list[mid] > searchItem)
last = mid - 1;
else
first = mid + 1;
}
if (found)
return mid;
else
return –1;
} //end binarySearch
Java Programming: From Problem Analysis to Program Design, Second Edition 13
Recursive Definitions
 Recursion:

 Process of solving a problem by reducing it to


smaller versions of itself.

 Recursive definition:

 Definition in which a problem is expressed in terms


of a smaller version of itself.

 Has one or more base cases.

Java Programming: From Problem Analysis to Program Design, Second Edition 14


Recursive Definitions
 Recursive algorithm:
 Algorithm that finds the solution to a given problem by
reducing the problem to smaller versions of itself.
 Has one or more base cases.
 Implemented using recursive methods.
 Recursive method:
 Method that calls itself.
 Base case:
 Case in recursive definition in which the solution is
obtained directly.
 Stops the recursion.

Java Programming: From Problem Analysis to Program Design, Second Edition 15


Recursive Definitions
 General solution:

 Breaks problem into smaller versions of itself.

 General case:

 Case in recursive definition in which a smaller


version of itself is called.

 Must eventually be reduced to a base case.

Java Programming: From Problem Analysis to Program Design, Second Edition 16


Tracing a Recursive Method
Recursive method:

 Has unlimited copies of itself.

 Every recursive call has its own:

 Code

 Set of parameters

 Set of local variables

Java Programming: From Problem Analysis to Program Design, Second Edition 17


Tracing a Recursive Method
 After completing a recursive call:

 Control goes back to the calling environment.

 Recursive call must execute completely before control


goes back to previous call.

 Execution in previous call begins from point


immediately following recursive call.

Java Programming: From Problem Analysis to Program Design, Second Edition 18


Designing Recursive Methods
 Understand problem requirements.

 Determine limiting conditions.

 Identify base cases.

Java Programming: From Problem Analysis to Program Design, Second Edition 19


Designing Recursive Methods
 Provide direct solution to each base case.

 Identify general cases.

 Provide solutions to general cases in terms of smaller


versions of general cases.

Java Programming: From Problem Analysis to Program Design, Second Edition 20


Recursive Factorial Method

public static int fact(int num)


{
if (num = = 0)
return 1;
else
return num * fact(num – 1);
}

Java Programming: From Problem Analysis to Program Design, Second Edition 21


Recursive Factorial Method

Java Programming: From Problem Analysis to Program Design, Second Edition 22


Largest Value in Array
public static int largest(int[] list,
int lowerIndex,
int upperIndex)
{
int max;
if (lowerIndex == upperIndex)
return list[lowerIndex];
else
{
max = largest(list, lowerIndex + 1,
upperIndex);
if (list[lowerIndex] >= max)
return list[lowerIndex];
else
return max;
}
}
Java Programming: From Problem Analysis to Program Design, Second Edition 23
Largest Value in Array

Java Programming: From Problem Analysis to Program Design, Second Edition 24


Recursion or Iteration?
 Two ways to solve particular problem:
 Iteration
 Recursion
 Iterative control structures use looping to repeat a set
of statements.
 Tradeoffs between two options:
 Sometimes recursive solution is easier.
 Recursive solution is often slower.

Java Programming: From Problem Analysis to Program Design, Second Edition 25


Summary
 Lists
 Searching lists:
 Sequential searching
 Sequential searching on an order list
 Binary search
 Sorting lists:
 Insertion sort
 Recursion

Java Programming: From Problem Analysis to Program Design, Second Edition 26

You might also like