Chapter 10: Applications of Arrays and Strings - PowerPoint PPT Presentation

1 / 38
About This Presentation
Title:

Chapter 10: Applications of Arrays and Strings

Description:

Learn how to implement the sequential search algorithm. ... for(String jam : mike) System.out.println(jam); Chapter Summary. Lists. Searching lists: ... – PowerPoint PPT presentation

Number of Views:24
Avg rating:3.0/5.0
Slides: 39
Provided by: manasi61
Category:

less

Transcript and Presenter's Notes

Title: Chapter 10: Applications of Arrays and Strings


1
Chapter 10 Applications of Arrays and Strings
  • Java Programming
  • From Problem Analysis to Program Design,
  • Second Edition

2
Chapter Objectives
  • Learn how to implement the sequential search
    algorithm.
  • Explore how to sort an array using bubble sort,
    selection sort, and insertion sort algorithms.
  • Learn how to implement the binary search
    algorithm.
  • Become aware of the class Vector.
  • Learn more about manipulating strings using the
    class String.

3
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.

4
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.

5
Search
  • public static int seqSearch(int list,
  • int listLength, int searchItem)
  • int loc
  • boolean found false
  • for (loc 0 loc lt listLength loc)
  • if (listloc searchItem)
  • found true
  • break
  • if (found)
  • return loc
  • else
  • return -1

6
Sorting a List
  • Bubble sort
  • Suppose list0...n - 1 is a list of n elements,
    indexed 0 to n - 1. We want to rearrange (sort)
    the elements of list in increasing order. The
    bubble sort algorithm works as follows
  • In a series of n - 1 iterations, the successive
    elements, listindex and listindex 1, of
    list are compared. If listindex is greater than
    listindex 1, then the elements listindex
    and listindex 1 are swapped (interchanged).

7
Bubble Sort
8
Bubble Sort
9
Bubble Sort
// method to sort a list by bubble sort
public static void bbsort(int mike, int
length1) int temp, kter, ind length1
mike.length for(kter0 kterltlength1-1
kter) for(ind0 indlt
length1-1-kter ind) if(mikeindgt
mikeind1) tempmikeind
mikeindmikeind1 mikeind1temp
// end of method
10
Bubble Sort
  • For a list of length n, an average bubble sort
    makes n(n 1) / 2 key comparisons and about n(n
    1) / 4 item assignments.
  • Therefore, if n 1000, bubble sort makes about
    500,000 key comparisons and about 250,000 item
    assignments to sort the list.

11
Selection Sort
  • List is sorted by selecting list element and
    moving it to its proper position.
  • Algorithm finds position of smallest element and
    moves it to top of unsorted portion of list.
  • Repeats process above until entire list is sorted.

12
Selection Sort
13
Selection Sort
14
Selection Sort
// method to sort a list by selection sort
public static void selsort(int mike, int
length1) int ind, smlestind, minind, temp
length1 mike.length for(ind0
indltlength1-1 ind) smlestindind
for(minindind1 minindlt length1 minind)
if(mikeminindlt mikesmlestind)
smlestindminind // swap step
tempmikesmlestind mikesmlestindmike
ind mikeindtemp
// end of method
15
Selection Sort
  • For a list of length n, an average selection sort
    makes n(n 1) / 2 key comparisons and 3(n 1)
    item assignments.
  • Therefore, if n 1000, selection sort makes
    about 500,000 key comparisons and about 3000 item
    assignments to sort the list.

16
Insertion Sort
The insertion sort algorithm sorts the list by
moving each element to its proper place.
17
Insertion Sort
18
Insertion Sort
19
Insertion Sort
20
Insertion Sort
// method to sort a list by insertion sort
public static void inssort(int mike, int
length1) int outorder, location, temp
length1 mike.length for(outorder1
outorderltlength1 outorder) if
(mikeoutorderltmikeoutorder-1)
tempmikeoutorder locationoutorder
do mikelocationmikeloca
tion-1 location--
while(locationgt0 mikelocation-1gttemp)
mikelocationtemp
// end of method
21
Insertion Sort
  • For a list of length n, on average, the insertion
    sort makes (n2 3n 4) / 4 key comparisons
    and about n(n 1) / 4 item assignments.
  • Therefore, if n 1000, the insertion sort makes
    about 250,000 key comparisons and about 250,000
    item assignments to sort the list.

22
Sequential Ordered Search
public static int seqOrderedSearch(int list,
int listLength, int searchItem)
int loc
//Line 1 boolean found false
//Line 2 for (loc 0 loc lt
listLength loc) //Line 3 if
(listloc gt searchItem) //Line 4
found true
//Line 5 break
//Line 6 if (found)
//Line 7
if (listloc searchItem) //Line 8
return loc
//Line 9 else
//Line 10 return -1
//Line 11 else
//Line 12 return
-1 //Line 13
23
Binary Search
  • Can only be performed on a sorted list.
  • Uses divide and conquer technique to search list.
  • If L is a sorted list of size n, to determine
    whether an element is in L, the binary search
    makes at most 2 log2n 2 key comparisons.
  • (Faster than a sequential search.)

24
Binary Search Algorithm
  • Search item is compared with middle element of
    list.
  • If search item lt middle element of list, search
    is restricted to first half of the list.
  • If search item gt middle element of list, search
    is restricted to second half of the list.
  • If search item middle element, search is
    complete.

25
Binary Search Algorithm
Determine whether 75 is in the list.
26
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 lt last !found)
mid (first last) / 2 if
(listmid searchItem) found
true else if (listmid gt
searchItem) last mid - 1
else first mid 1
if (found) return mid else
return 1 //end binarySearch
27
Vectors
  • The class Vector can be used to implement a list.
  • Unlike an array, the size of a Vector object can
    grow/shrink during program execution.
  • You do not need to worry about the number of data
    elements in a vector.

28
Members of the class Vector
29
Members of the class Vector
30
Members of the class Vector
31
Members of the class Vector
32
Vectors
  • Every element of a Vector object is a reference
    variable of the type Object.
  • To add an element into a Vector object
  • Create appropriate object.
  • Store data into object.
  • Store address of object holding data into Vector
    object element.

33
Vectors
VectorltStringgt stringList new
VectorltStringgt() stringList.addElement("Spring")
stringList.addElement("Summer") stringList.addE
lement("Fall") stringList.addElement("Winter")
34
  • // to create a vector of 5 objects
  • import java.util.
  • public class vector1
  • public static void main(String moi)
  • Scanner console new Scanner(System.in)
  • Vector ltStringgt mike new Vector ltStringgt()
  • int i
  • for(i0 ilt5 i)
  • mike.addElement(console.next())

35
  • // to print the vector
  • System.out.println(mike)
  • System.out.println()
  • // to print the elements vertically
  • for (i0 iltmike.size() i)
  • System.out.println(mike.elementAt(i))
  • // using the for each loop to print the elements
    to uppercase
  • for(String rest mike)
  • System.out.println(rest.toUpperCase())

36
Testing some methods
  • // to create a vector of variable objects
  • import java.util.
  • public class vector2
  • public static void main(String moi)
  • Scanner console new Scanner(System.in)
  • Vector ltStringgt mike new Vector ltStringgt()
  • System.out.println("Please, enter four string
    objects")
  • int i
  • for(i0 ilt4 i)
  • mike.addElement(console.next())
  • // to add additional item
  • mike.addElement("Wow1")
  • mike.addElement("Wow2")
  • mike.insertElementAt("Wow3",2)

37
Chapter Summary
  • Lists
  • Searching lists
  • Sequential searching
  • Sequential searching on an order list
  • Binary search
  • Sorting lists
  • Bubble sort
  • Selection sort
  • Insertion sort

38
Chapter Summary
  • Programming examples
  • The class Vector
  • Members of the class Vector
  • The class String
  • Additional methods of the class String
Write a Comment
User Comments (0)
About PowerShow.com