page194.html
来自「Data Structures And Algorithms With Obje」· HTML 代码 · 共 61 行
HTML
61 行
<HTML><HEAD><TITLE>Finding Items in a Sorted List</TITLE></HEAD><BODY bgcolor="#FFFFFF"> <a href="../index.html" target="_top"><img src="../icons/usins.gif" alt="Logo" align=right></a><b>Data Structures and Algorithms with Object-Oriented Design Patterns in Python</b><br><A NAME="tex2html3443" HREF="page195.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html3441" HREF="page191.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html3435" HREF="page193.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html3445" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <BR><HR><H3><A NAME="SECTION007213000000000000000">Finding Items in a Sorted List</A></H3><P>Program <A HREF="page194.html#progsortedListAsArrayd"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A> defines the two methodsused to locate items in a sorted list.Both of these methods make use of the <tt>findOffset</tt> methoddescribed above.<P><P><A NAME="10452"> </A><A NAME="progsortedListAsArrayd"> </A> <IMG WIDTH=575 HEIGHT=485 ALIGN=BOTTOM ALT="program10147" SRC="img811.gif" ><BR><STRONG>Program:</STRONG> <tt>SortedListAsArray</tt> class <tt>find</tt> and <tt>findPosition</tt> methods.<BR><P><P>The <tt>find</tt> method takes a given objectand finds the object contained in the sorted list which matches(i.e., compares equal to) the given one.It calls <tt>findOffset</tt> to determine by doing a binary searchthe array index at which the matching object is found.<tt>find</tt> returns the matching object, if one is found;otherwise, it returns <tt>None</tt>.The total running time of <tt>find</tt> is dominated by <tt>findOffset</tt>.Therefore, the running time is <IMG WIDTH=56 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline59347" SRC="img400.gif" >.<P>The <tt>findPosition</tt> method also takes an object,but it returns a <tt>Cursor</tt> instead.<tt>findPosition</tt> determines the position in the arrayof an object which matches its second argument.<P>The implementation of <tt>findPosition</tt> is trivial:It calls <tt>findOffset</tt> to determinethe position at which the matching object is foundand returns an instance of the nested class <tt>Cursor</tt>.(The <tt>Cursor</tt> class is derived from the class of the same name shownin Program <A HREF="page175.html#progorderedListAsArrayh"><IMG ALIGN=BOTTOM ALT="gif" SRC="../icons/cross_ref_motif.gif"></A>).The <tt>Cursor</tt> overridesthe inherited <tt>insertAfter</tt> and <tt>insertBefore</tt> methodswith methods that raise a <tt>TypeError</tt> exception.These insert operations are not provided for sorted listsbecause they allow arbitrary insertion,but arbitrary insertions do not necessarily result in sorted lists.<P>The total running time of the <tt>findPosition</tt> methodis dominated by <tt>findOffset</tt>.Therefore like <tt>find</tt>,the running time of <tt>findPosition</tt> is <IMG WIDTH=56 HEIGHT=24 ALIGN=MIDDLE ALT="tex2html_wrap_inline59347" SRC="img400.gif" >.<P><HR><A NAME="tex2html3443" HREF="page195.html"><IMG WIDTH=37 HEIGHT=24 ALIGN=BOTTOM ALT="next" SRC="../icons/next_motif.gif"></A> <A NAME="tex2html3441" HREF="page191.html"><IMG WIDTH=26 HEIGHT=24 ALIGN=BOTTOM ALT="up" SRC="../icons/up_motif.gif"></A> <A NAME="tex2html3435" HREF="page193.html"><IMG WIDTH=63 HEIGHT=24 ALIGN=BOTTOM ALT="previous" SRC="../icons/previous_motif.gif"></A> <A NAME="tex2html3445" HREF="page611.html"><IMG WIDTH=43 HEIGHT=24 ALIGN=BOTTOM ALT="index" SRC="../icons/index_motif.gif"></A> <P><ADDRESS><img src="../icons/bruno.gif" alt="Bruno" align=right><a href="../copyright.html">Copyright © 2003</a> by <a href="../signature.html">Bruno R. Preiss, P.Eng.</a> All rights reserved.</ADDRESS></BODY></HTML>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?