Lecture 23:Applications of Arrays - PowerPoint PPT Presentation

About This Presentation
Title:

Lecture 23:Applications of Arrays

Description:

List has no more elements left. Average number of comparisons made by the ... Uses the 'divide and conquer' technique. Compare search item to middle element ... – PowerPoint PPT presentation

Number of Views:14
Avg rating:3.0/5.0
Slides: 17
Provided by: xw
Category:

less

Transcript and Presenter's Notes

Title: Lecture 23:Applications of Arrays


1
Lecture 23Applications of Arrays
  • Introduction to Computer Science
  • Spring 2006

2
Sequential Search
  • Sequential search search a list for an item
  • Compare search item with other elements until
    either
  • Item is found
  • List has no more elements left
  • Average number of comparisons made by the
    sequential search equals half the list size
  • Good only for very short lists

3
int seqSearch(const int list, int listLength,
int searchItem) int loc for (loc 0 loc
lt listLength loc) if (listloc
searchItem) return loc return -1
4
Sorting a List Bubble Sort
  • Suppose list0...listn - 1 is a list of n
    elements, indexed 0 to n 1
  • Bubble sort algorithm
  • In a series of n - 1 iterations, compare
    successive elements, listindex and listindex
    1
  • If listindex is greater than listindex 1,
    then swap them

5
(No Transcript)
6
(No Transcript)
7
Bubble Sort Algorithm
for i 0 to n-1 do        for j 0 to n - i -
1 do        if listj gt listj1 then        
Swap(listj , listj1)
void bubbleSort(int list, int length) int
temp int i, j for (i 0 i lt length -
1 i) for (j 0 j lt length -
1 - i j) if (listj gt listj
1) temp
listj listj listj 1
listj 1 temp

8
Sorting a List Selection Sort
  • Selection sort rearrange list by selecting an
    element and moving it to its proper position
  • Find the smallest (or largest) element and move
    it to the beginning (end) of the list
  • On successive passes, locate the smallest item in
    the list starting from the next element

9
Selection Sort Algorithm
  • for (index 0 index lt length - 1 index)
  • Find the location, smallestIndex, of the smallest
    element in listindexlistlength-1.
  • Swap the smallest element with listindex. That
    is, swap listsmallestIndex with listindex

void selectionSort(int list, int length)
int index int smallestIndex int
minIndex int temp for (index 0 index lt
length - 1 index) //Step
a smallestIndex index for (minIndex
index 1 minIndex lt length minIndex) if
(listminIndex lt listsmallestIndex) smalles
tIndex minIndex //Step b temp
listsmallestIndex listsmallestIndex
listindex listindex temp
10
Sequential Search on an Ordered List
  • General form of sequential search algorithm on a
    sorted list

11
Sequential Search on an Ordered List
int seqOrderedSearch(const int list, int
listLength, int searchItem) int
loc //Line 1 bool 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
12
Binary Search
  • Binary search can be applied to sorted lists
  • Uses the divide and conquer technique
  • Compare search item to middle element
  • mid (firstlast)/2
  • first is the starting index of the search list
  • last is the ending index of the search list
  • mid is the index of the middle element of the
    search list
  • If search item is less than middle element,
    restrict the search to the lower half of the list
  • Otherwise search the upper half of the list

13
Search 75 in the following list
14
Binary Search
int binarySearch(const int list, int
listLength, int searchItem) int first
0 int last listLength - 1 int mid bool
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
15
Binary Search (continued)
  • Every iteration cuts size of search list in half
  • If list L has 1000 items
  • At most 11 iterations needed to determine if an
    item x is in list
  • Every iteration makes 2 key (item) comparisons
  • Binary search makes at most 22 key comparisons to
    determine if x is in L
  • Sequential search makes 500 key comparisons
    (average) to if x is in L for the same size list

16
End of lecture 23
  • Thank you!
Write a Comment
User Comments (0)
About PowerShow.com