




下载本文档
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、1 .大。表示法:粗略的量度方法即算法的速度是如何与数据项的个数相关的大O表示法表示的运行O(N)O(logN)O(1)O(N)O(N)O(N)O(1)是最优秀的,O(logN)良好,O(N)还可以,O(N2)稍差(在冒泡法中见到)2 .排序publicclassJWzw/插入排序publicvoidinsertArray(Integerin)inttem=0;intnum=0;intupnum=0;for(inti=0;i=0;j-)num+;if(inj+1inj)tem=inj+1;inj+1=inj;inj=tem;upnum+;elsebreak;for(inti=0;iin.len
2、gth;i+)System.out.print(ini);if(iin.length-1)System.out.print(,);算法时间线性查找二分查找无序数组的插入有序数组的插入无序数组的删除有序数组的删除/选择排序publicvoidchooseArray(Integerin)inttem=0;intnum=0;intupnum=0;for(inti=0;iin.length;i+)for(intj=i;jin.length-1;j+)num+;if(inj+1inj)tem=inj+1;inj+1=inj;inj=tem;upnum+;)for(inti=0;iin.length;i+
3、)System.out.print(ini);if(iin.length-1)System.out.print(,);)/冒泡排序publicvoidefferArray(Integerin)inttem=0;intnum=0;intupnum=0;)System.out .println();System.out.println(插入排序循环次数:+System.out .println(移动次数:+upnum);System.out .print(nnn);)num);System.out .println();System.out .println(选择排序循环次数:+System.ou
4、t .println(移动次数:+upnum);)System.out .print(nnn);num);for(inti=0;iin.for(intj=i;jin.(num+;if(inj+1ini)tem=inj+1;inj+1=ini;ini=tem;upnum+;System.out.print(ini);if (iin.length-1)System.out.print(,);System.out .println();System.out .println(冒泡排序循环次数:+num);System.out .println(移动次数:+upnum);System.out .pri
5、nt(nnn);/打印乘法口诀publicvoidprintMulti()for(intj=1;j10;j+)for(inti=1;i=j;i+)System.out.print(i+*+j+System.out.print(tn);System.out.print(nnn);/打印N*1+N*2+N*3=num的所有组合publicvoidprintNumAssemble(intnum)for(inti=0;inum+1;i+)for(intj=0;jnum/2+1;j+)for(intin=0;innum/3+1;in+)if(i*1+j*2+in*3=num)for(inti=0;i2)
6、;3 .优先级队列classPriorityQueueprivatelonga=null;privateintnItems=0;privateintmaxSize=0;publicPriorityQueue(intmaxSize)a=newlongmaxSize;this.maxSize=maxSize;nItems=0;publicvoidinsert(longl)/优先级队列的插入不是队尾,而是选择一个合适的按照某种顺序插入的+in);)System.out.println(小马+i+,t中马+j+,t大马*/的所有组合/当队列长度为0时,如下/不为0时,将所有比要插入的数小的数据后移,这
7、样大的数就在队列的头部了inti=0;if(nItems=0)a0=l;elsefor(i=nItems-1;i=0;i-)if(lai)ai+1=ai;elsebreak;ai+1=l;nItems+;publiclongremove()/移出的是数组最上端的数,这样减少数组元素的移动returna-nItems;publicbooleanisEmpty()return(nItems=0);publicbooleanisFull()return(nItems=maxSize);publicintsize()returnnItems;publicclassduilie/队列体类privated
8、uilies;privateStringdata;duilie(Stringdata)this.data=data;publicStringgetData()returndata;)publicvoidsetData(Stringdata)this.data=data;)publicduiliegetS()returns;)publicvoidsetS(duilies)this.s=s;)publicclassduiliecz/队列操作类/*paramargs*/privateinti=0;/队列长privateduilietop=newduilie();/队列头privateduilieen
9、d=newduilie();/队列尾publicvoidadd(Strings)/添加队列duiliem=newduilie(s);if(i!=0)m.setS(top.getS();top.setS(m);elsetop.setS(m);end.setS(m);i+;4 .队列publicvoiddel()/删除队尾if(i=0)return;elseif(i=1)top.setS(null);end.setS(null);elseduilietopi=newduilie();/top1.setS(top.getS();while(!top1.getS().getS().equals(end
10、.getS()top1.setS(top1.getS().getS();)end.setS(top1.getS();)i-;)publicstaticvoidmain(Stringargs)/TODOAuto-generatedmethodstubduilieczm=newduiliecz();m.add(1);m.add(2);m.add(3);m.add(4);for(intn=0;n4;n+)m.del();)publicintgetI()returni;)publicduiliegetEnd()returnend;)publicduiliegetTop()returntop;)5 .栈
11、publicclassStackintarr;intlen=0;publicStack()arrnewint100;队列底查找用缓存publicStack(intn)arr=newintn;)publicintsize()returnlen+1;)/扩大数组publicvoidresize()intb=newintarr.length*2;System.arraycopy(arr,0,b,0,arr.length);arr=b;)publicvoidshow()for(inti=0;i=arr.length)resize();arrlen=a;len+;)/出栈publicintpop()if
12、(len=0)System.out.println();System.out.println(return-1;)inta=arrlen-1;arrlen-1=0;len-;returna;stackisempty!);6 .链表classNodeObjectdata;Nodenext;publicNode(Objectdata)setData(data);publicvoidsetData(Objectdata)this.data=data;publicObjectgetData()returndata;classLinkNodehead;intsize=0;publicvoidadd(Ob
13、jectdata)Noden=newNode(data);if(head=null)head=n;elseNodecurrent=head;while(true)if(current.next=null)break;current=current.next;current.next=n;size+;publicvoidshow()Nodecurrent=head;if(current!=null)while(true)System.out.println(current);if(current=null)break;)current=current.next;)elseSystem.out.p
14、rintln(linkisemptypublicObjectget(intindex)/.publicintsize()returnsize;7 .单链表classNode/节点类,单链表上的节点Stringdata;/数据域,存放String类的数据Nodenext;/指向下一个节点Node(Stringdata)this.data=data;/构造函数Stringget()returndata;/返回数据classMyLinkList/链表类Nodefirst;/头节点intsize;/链表长度MyLinkList(Stringarg)/Nodefirst=newNode(head);/生
15、成头节点first=newNode(head);/J.F.这里不需要定义局部变量first/如果定义了局部变量,那成员变量first就一直没有用上/所以,它一直为空size=0;Nodep=first;for(inti=0;iarg.length;i+)/将arg数组中的元素分别放入链表中Nodeq=newNode(argi);q.next=p.next;/每一个节点存放一个arg数组中的元素p.next=q;p=p.next;size+;);MyLinkList()/无参数构造函数/Nodefirst=newNode(head);first=newNode(head);/J.F.这里犯了和上
16、一个构造方法同样的错误size=0;intsize()/返回链表长度returnsize;voidinsert(Nodea,intindex)/将节点a插入链表中的第index个位置Nodetemp=first;for(inti=0;iindex;i+)temp=temp.next;/找到插入节点的前一节点a.next=temp.next;/插入节点temp.next=a;size+;Nodedel(intindex)/删除第index个节点,并返回该值Nodetemp=first;for(inti=0;iindex;i+)temp=temp.next;/找到被删除节点的前一节点)Nodeno
17、de=temp.next;temp.next=node.next;size-;/删除该节点,链表长度减一returnnode;)voidprint()/在屏幕上输出该链表(这段程序总是出错,不知道错在哪里)(Nodetemp=first;for(inti=1;i);)voidreverse()/倒置该链表(for(inti=0;i=size)returnnull;)Nodetemp=first;for(inti=0;i10)System.out.println(长度超过限制或者缺少参数);elseMyLinkListll=newMyLinkList(arg);/创建一个链表ll.print()
18、;/先输出该链表(运行到这一步抛出异常)ll.reverse();/倒置该链表ll.print();/再输出倒置后的链表Stringdata=newString10;inti;for(i=0;ill.size();i+)datai=ll.get(i);/将链表中的数据放入数组)/sort(data);/按升序排列data中的数据(有没有现成的排序函数?)for(i=0;ill.size();i+)System.out.print(datai+;);/输出数组中元素)System.out.println();MyStacks=newMyStack();/创建堆栈实例sfor(i=0;ilast)
19、:);Linkcurrent=first;while(current!=null)current.display();current=current.next;)System.out.println();classFirstLastListAppstaticvoidmain(Stringargs)TODOAuto-generatedmethodstubnewFirstLastList();/insertatfront/displaythelist/deletefirsttwoitems/displayagain9 .有序链表packagearithmetic;classLinkpublicin
20、tiData=0;publicLinknext=nullpublicLink(intiData)this.iData=iData;publicvoiddisplay()System.out.print(iData+);classSortedListprivateLinkfirst=nullpublicSortedList()first=null;publicvoidinsert(intkey)public/FirstLastListtheList=theList.insertFirst(22);theList.insertFirst(44);theList.insertFirst(66);th
21、eList.insertLast(11);theList.insertLast(33);theList.insertLast(55);theList.displayList();theList.deleteFirst();theList.deleteFirst();theList.displayList();/insertatrearLinknewLink=newLink(key);Linkprevious=null;Linkcurrent=first;/while的第一个条件是没有到达链表的尾端,第二个是按顺序找到一个合适的位置while(current!=null&keycurre
22、nt.iData)previous=current;current=current.next;/如果是空表或者要插入的元素最小,则在表头插入keyif(current=first)first=newLink;elseprevious.next=newLink;newLink.next=current;/*删除表头的节点*return要删除的节点*/publicLinkremove()Linktemp=first;first=first.next;returntemp;publicbooleanisEmpty()return(first=null);publicvoiddisplayList()
23、System.out.print(List(first-last):)Linkcurrent=first;/startatbeginningoflistclassSortedListAppwhile(current!=current.display();current=current.System.out.println(null)/untilendoflist,/printdatanext;/movetonextlinknSortedListtheSortedList=theSortedList.insert(20);theSortedList.insert(40);theSortedLis
24、t.displayList();theSortedList.insert(10);theSortedList.insert(30);theSortedList.insert(50);theSortedList.displayList();theSortedList.remove();theSortedList.displayList();newSortedList();/insert2items/displaylist/insert3moreitems/displaylist/removeanitem/displaylist10 .双向链表classLink/双向链表,有两个指针,一个向前pu
25、blicvoiddisplay()System.out.print(iData+);classDoublyLinked/分别指向链表的表头和表尾privateLinkfirst=nullprivateLinklast=nullpublicbooleanisEmpty()returnfirst=null/*在表头插入数据param要插入的节点的数据*/publicvoidinsertFirst(intkey)LinknewLink=newLink(key);/如果开始链表为空,则插入第一个数据后,last也指向第一个数据if(this.isEmpty()last=newLink;else/表不为
26、空的情况first.previous=newLink;newLink.next=first;/无论怎publicstaticvoidmain(Stringargs)/createnewlistpublicpublicLinkpublicLinkpublicLink(thisintiData=0;previous=nullnext=null;intiData).iData=iData;样,插入后都的让first重新指向第一个节点first=newLink;publicvoidinsertLast(intkey)/在尾端插入数据,同上LinknewLink=newLink(key);if(this
27、.isEmpty()first=newLink;elselast.next=newLink;newLink.previous=last;last=newLink;/一二在指定的节点后插入数据/如果插入点在last的位置if(current=last)last=newLink;else/非last位置,交换各个next和previous的指针newLink.next=current.next;current.next.previous=newLink;current.next=newLink;newLink.previous=current;returntrue;/*删除表头的节点return*
28、/publicLinkdeleteFirst()Linktemp=first;/如果表中只有一个元素,删除后则为空表,所以last=nullparamkey指定的节点的值paramiData要插入的数据return是否插入成功*/publicbooleaninsertAfter(intkey,intiData)LinknewLink=newLink(key);Linkcurrent=first;/从first开始遍历,看能否找到以key为关键字的节点while(current.iData!=key)current=current.next;/若能找到就跳出循环,否则返回false,插入失败if
29、(current=null)returnfalseif(first.next=null)last=null;else/否则,让第二个元素的previous=nullfirst.next.previous=null;/删除头指针,则first指向原来的secondfirst=first.next;returntemp;publicLinkdeleteLast()/同上Linktemp=last;if(last.previous=null)first=null;elselast.previous.next=null;last=last.previous;returntemp;publicLinkd
30、eleteKey(intkey)Linkcurrent=first;/遍历整个链表查找对应的key,如果查到跳出循环,否则while(current.iData!=key)current=current.next;/.否则遍历到表尾,说明不存在此key,返回null,删除失败if(current=null)returnnull;)if(current=first)first=first.next;elsecurrent.previous.next=current.next;if(current=last)last=last.previous;elsecurrent.next.previous=
31、current.previous;returncurrent;)publicvoiddisplayForward()Linkcurrent=while(current!=current.display();System.out.println();)publicvoiddisplayBackward()DoublyLinkedtheList=theList.insertFirst(22);theList.insertFirst(44);newDoublyLinked();/insertatfronttheList.insertFirst(66);theList.insertLast(11);t
32、heList.insertLast(33);theList.insertLast(55);Linkcurrent=last;classwhile(current!=current.display();current=current.System.out.println();nullprevious;DoublyLinkedApppublicstaticvoidmain(Stringargs)/makeanewlistfirstnullcurrent=current.next;/insertatreartheList.displayForward();theList.displayBackwar
33、d();theList.deleteFirst();theList.deleteLast();theList.deleteKey(11);theList.displayForward();theList.insertAfter(22,77);theList.insertAfter(33,88);theList.displayForward();)11 .实现二叉树前序遍历迭代器classTreeNode这个类用来声明树的结点,其中有左子树、右子树和自身的内容。classMyTree这个类用来声明一棵树,传入根结点。这里设计的比较简单classTreeEum这个类是树的迭代器,通过MyTree类
34、的方法获取,这里主要就是设计它了。代码如下:/TreeNode类,使用了泛型,由于比较简单,考试.大提示不作解释classTreeNode(Enode;privateTreeNodeleft;privateTreeNoderight;publicTreeNode(Ee)(this(e,null,null);)publicTreeNode(Ee,TreeNodeleft,TreeNoderight)(this.node=e;this.left=left;this.right=right;)publicTreeNodeleft()(returnleft;)publicTreeNoderight()
35、(returnright;)/MyTree类,没什么功能,传入根结点构造,getEnumerator()方法获取迭代器。classMyTree(TreeNoderoot;publicMyTree(TreeNoderoot)(this.root=root;publicTreeEnumgetEnumerator()(returnnewTreeEnum(root);/这个类为迭代器,有详细解释,相信各位能看懂。在栈中用了两次泛型。importjava.util.Stack;publicclassTreeEnum(privateTreeNoderoot;privateStackTreeNodestor
36、e;/*保存遍历左子树但未遍历右子树的结点*/privateTreeNodenext;publicTreeEnum(TreeNoderoot)(this.root=root;store=newStackTreeNode();next=root;publicTreeNodenext()(TreeNodecurrent=next;if(next!=null)(/*如果当前结点的左子树不为空,则遍历左子树,并标记当前结点未遍历右子树*/if(next.left()!=null)(store.push(next);next=next.left();/如果当前结点的左子树为空,则遍历右子树elseif(
37、next.right()!=null)(next=next.right();/displaylistforward/displaylistbackward/deletefirstitem/deletelastitem/deleteitemwithkey11/displaylistforward/insert77after22/insert88after33/displaylistforward/*如果当前结点为叶子,则找未遍历右子树的结点并且遍历它的右子树*/else(if(!store.empty()/*判断是否还有结点的右子树未遍历*/(TreeNodetmp=store.pop();/*
38、如果有未遍历右子树的结点,但它的右子树为空,且还有结点的右子树未遍历,*/*则一直往上取,直到取到未遍历右子树且右子树不为空的结点,遍历它的右子树.*/while(tmp.right()=null)&!store.empty()(tmp=store.pop();next=tmp.right();else(/*如果没有哪个结点右子树未遍历,则表示没有下一个结点了,设置next为null*/next=null;returncurrent;publicbooleanhasMoreElement()(returnnext!=null;下面写个测试类,不作解释,相信大家看得懂publicclas
39、sTreeReaderpublicstaticvoidmain(Stringargs)TreeNoden1=newTreeNode(n1)TreeNoden2=newTreeNode(n2)TreeNoden3=newTreeNode(n3)TreeNoden4=newTreeNode(n4)TreeNoden5=newTreeNode(n5)TreeNoden6=newTreeNode(n6,null,n1);TreeNoden7=newTreeNode(n7,n2,null);TreeNoden8=newTreeNode(n8,n7,null);TreeNoden9=newTreeNode
40、(n9,null,n5);TreeNoden10=newTreeNode(n10,n4,n9);TreeNoden11=newTreeNode(n11,n6,n8);TreeNoden12=newTreeNode(n12,n3,n10);TreeNoderoot=newTreeNode(MyTreetree=newMyTree(root);root,n11,n12);TreeEnumeum=tree.getEnumerator();while(eum.hasMoreElement()(System.out.print(eum.next().node+)System.out.println(en
41、d);)12 .迭代器packageTreeIterator;publicinterfaceIteratorpublicbooleanhasNext();publicObjectnext();)这个接口我们有2个方法,hasNext()是否还有下一条数据,next返回具体的Object这里也就是树。我们先不必要忙着做他的实现类,我们现在要来做的是这个容器(不是JAVA中容器,与arraylist什么的无关),正所谓树的容器是什么,是山也!我们想想山应该具有什么呢!?首先它要有种植树的功能,这里可以看作添加树。我们可以想像山的功能是和树相互关联的,那么他们之间是什么关系呢,我们给他们一种聚合的关
42、系,聚合的关系大家可以参考UML图,我在这里给出它的一种程序表现形式。packageTreeIterator;publicclassHallTreetree;/这里可以看作是聚合关系privateintindex;/指向Tree口的标签publicHall(intmaxNumber)tree=newTreemaxNumber;index=0;)publicvoidadd(Treetree)this.treeindex=tree;index+;)publicIteratorconnectIterator();)这里我们定义的山可以抽象出Hall类来,Treetree可以看作是山和树之间的一种聚合
43、关系。add方法就是添加树。问题来了,山和树有了关系,那么山和迭代器有什么关系呢。它们之间肯定有一种关系。我们有了这个容器(山),就要把这个容器来实现迭代的方法:hasNext()和Next().恩这里我们可以看出,山和迭代器之间也是一种关联关系。我们就把它看成是一种聚合关系(TIP:聚合关系一种特殊的关联关系)。我们可以通过一个connectIterator方法来链接山和迭代器,接下来我们要去做一个具体的迭代类,这个具体的类中间有了hasNext()和Next()的具体实现方法packageTreeIterator;publicclassTreeIteratorimplementsItera
44、torprivateintlast=0;privateHallhall;publicTreeIterator(Hallhall)this.hall=hall;)publicbooleanhasNext()if(lasthall.tree.length)returntrue;elsereturnfalse;)publicTreenext()Treet=hall.treelast;last+;returnt;)这里Hallhall就可以看作是一种关联关系,我们要把山和迭代器联系起来就通过构造函数来实现,hasNext和next实现方法就体现出来了有了山,有了迭代器,可是树还没有定义,不过这个树的方
45、法还很好解决!树不关联其他的事务,我们returnnewTreeIterator(this);可以简单的这么写:packageTreeiterator;publicclassTreeprivateStringname;publicTree(Stringname)=name;)publicStringgetName();)好了似乎我们整个工程完工了,我们现在来模拟一下农民老大伯来种树撒肥的过程;packageTreeiterator;publicclassPrenpublicPren()voidmain(Stringargs)newHall(4);
46、newTree(苹果树);newTree(梨树);newTree(橘子树);newTree(凤梨树);for(Iteratori=hall.connectiterator();i.hasNext();)StringType=(Tree)(i.next().getName();publicstaticHallhall=hall.add(hall.add(hall.add(hall.add(if(Type=苹果树)System.out.println()if(Type=梨树)System.out.println()if(Type=橘子树)System.out.println(洒苹果树的农药,);“
47、洒梨树的农药);洒橘子树的农药,洒了也没用,还没到成熟日,现在没结果实);)if(Type=凤梨树)System.out.println(南风天,湿气大,让它烂在地里吧);)种4个树,山小而五脏俱全,更像一个土包,再要有树才行啊,所以4个树的实例出现。好了接下来种树,几毫秒解决!山了有我们就要把山放到迭代器中间去了。遍历这个山(容器)。联想我们看看arrayList中的迭代器模式是怎么实现的!ArrayLista=newArrayList();a.add(a1);a.add(a2);a.add(a3);a.add(a4);for(Iteratori=a.iterator();i.hasNext
48、();)System.out.println(i.next().toString();)13 .合并搜索算法publicclassMergeSortArrayprivatelongtheArray;privateintnElems;publicMergeSortArray(intmax)theArray=newlongmax;nElems=0;)publicvoidinsert(longvalue)theArraynElems=value;/insertitnElems+;/incrementsize)publicvoiddisplay()publicvoidmergeSort()for(in
49、tj=0;jSystem.out.print(System.out.println()nElems;j+)theArrayj+););longworkSpace=recMergeSort(workSpace,0,if(lowerBound=upperBound)return;/nousesortingelse/findmidpointintmid=(lowerBound+upperBound)/2;/sortlowhalfrecMergeSort(workSpace,lowerBound,mid);/sorthighhalfrecMergeSort(workSpace,mid+1,upperB
50、ound);/mergethemmerge(workSpace,lowerBound,mid+1,upperBound);while(lowPtr=mid&highPtr=upperBound)if(theArraylowPtrworkSpacej+=elsewhile(lowPtr=mid)workSpacej+=while(highPtr=upperBound)privatevoidrecMergeSort(longworkSpace,intlowerBound,intupperBound)privatevoidmerge(longworkSpace,intlowPtr,inthi
51、ghPtr,intupperBound)intj=0;/workspaceindexintintintlowerBound=lowPtr;mid=highPtr-1;n=upperBound-lowerBound+1;/#ofitemsworkSpacej+=theArrayhighPtr+;forworkSpacej+=theArrayhighPtr+;(j=0;j0)returnnum+RecursionNum(num-1);ElseMergeSortArrayarr=newMergeSortArray(maxSize);/createthearrayreturn0;Recursionre
52、=newRecursion();15.归并排序/*归并排序,要求待排序的数组必须实现Comparable接口*/publicclassMergeSortimplementsSortstrategyprivateComparablebridge;/*利用归并排序算法对数组obj进行排序*/publicvoidsort(Comparableobj)if(obj=null)thrownewNullPointerException(Theparamcannotbenull!)bridge=newComparableobj.length;/初始化中间数组mergeSort(obj,0,obj.lengt
53、h-1);/归并排序bridge=null;)/*paramobj*要排序的数组的句柄*paramleft*要排序的数组的第一个元素下标*paramright*要排序的数组的最后一个元素的下标*/privatevoidmergeSort(Comparableobj,if(leftright)intcenter=(left+right)/2;mergeSort(obj,left,center);mergeSort(obj,center+1,right);merge(obj,left,center,right);)/*将两个对象数组进行归并,并使归并后为升序。归并前两个数组分别有序将下标从left
54、到right的数组进行归并排序);intleft,intright)*paramobj*对象数组的句柄*paramleft*左数组的第一个元素的下标*paramcenter*左数组的最后一个元素的下标*paramright*右数组的最后一个元素的下标*/privatevoidmerge(Comparableobj,intmid=center+1;intthird=left;inttmp=left;while(left=center&mid=right)组if(pareTo(objmid)=0)bridgethird+=objleft+;elsebridgethird+=objmid+;
55、/剩余部分依次置入中间数组while(mid=right)bridgethird+=objmid+;while(left=center)bridgethird+=objleft+;/将中间数组的内容拷贝回原数组copy(obj,tmp,right);intleft,intcenter,intright)/从两个数组中取出小的放入中间数/*将中间数组bridge中的内容拷贝到原数组中*paramobj*原数组的句柄*paramleft*要拷贝的第一个元素的下标*paramright*要拷贝的最后一个元素的下标*/privatevoidcopy(Comparableobj,while(left=r
56、ight)objleft=bridgeleft;intleft,intright)left+;16.希尔排序间隔序列:h=3*h+1,h=(h-1)/3publicclassShellSort/*paramargs*/publicstaticvoidmain(String口args)/TODOAuto-generatedmethodstubShellSortss=newShellSort();intnum口=546,87,21,3124,65,2,9,3,213,54,98,23,6,4,7,8,123,872,61,5,8954);ss.shellArray(num);for(inti=0;
57、inum.length;i+)System.out.println(numi);)publicvoidshellArray(int口num)inti=1;inttem,in;for(;i=1;)for(intj=i;ji-1&numin-i=tem)numin=numin-i;in=in-i;)numin=tem;)i=(i-1)/3;17.快速排序classQuicksortprivateintdata;QuickSort(intdata)this.data=data;publicvoidquickSort()recQuickSort(data,0,data.length-1);pr
58、ivatevoidrecQuickSort(intdata,intlow,inthigh)/设置两个滑标intlowCursor=low+1;inthighCursor=high;/交换时的临时变量inttemp=0;/比较枢值,设为数组的第一个值intmedi=datalow;while(true)/从低端开始查找,确定大于数datalow所在的位置while(lowCursorhigh&datalowCursor=判断确定小于值while(highCursorlow&datahighCursor=medi)highCursor-;/两游标位置出现越界,退出循环if(lowC
59、ursor=highCursor)break;/交换datahighCursor和datalowCursor位置数据temp=datahighCursor;datahighCursor=datalowCursor;datalowCursor=temp;/由while循环退出条件可知:lowCursorhighCursor/当前lowCursor指向右侧大于datalow的第一个位置;/而highCursor指向左侧小于datalow的第一个位置,所以需要交换和datalow/datahighCursor的值datalow=datahighCursor;datahighCursor=medi;/
60、递归运算左半部分if(lowhighCursor)recQuickSort(data,low,highCursor);)/递归运算右半部分if(lowCursorhigh)recQuickSort(data,lowCursor,high);)publicvoiddisplay()for(inti=0;idata.length;i+)System.out.print(datai+);)System.out.println();)publicstaticvoidmain(Stringargs)intdata=newint43,12,32,55,33,67,54,65,43,22,66,98,74);Quicksortsort=newQuickSort(data)
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年上海第二工业大学单招职业技能测试题库及答案1套
- 2026年厦门城市职业学院单招职业技能考试必刷测试卷及答案1套
- 碳纤维生产线项目建议书
- 2026年台州学院单招综合素质考试题库及答案1套
- 2026年兰州外语职业学院单招职业倾向性考试必刷测试卷新版
- 定向投资协议书
- 离职赔偿协议书
- 煤电项目申请报告
- 城区污水管网改造工程规划设计方案
- 使用权车库转让协议书
- 2025至2030年政府办公化系统软件项目商业计划书
- 干眼门诊创建培训课件
- 2024年苏州市市属事业单位招聘工作人员笔试真题
- GB/T 45309-2025企业采购物资分类编码指南
- 手足口病完整课件
- 消防设施设备培训课件
- 碳酸钙在生物医药中的应用-洞察分析
- 南通市2025届高三第一次调研测试(一模)历史试卷(含答案 )
- GB/T 44871-2024纺织品二异氰酸酯类化合物的测定
- 2025中级消防设施操作员作业考试题及答案(1000题)
- 《小学劳动教育研究的文献综述》3800字
评论
0/150
提交评论