欢迎来到天天文库
浏览记录
ID:34754175
大小:47.00 KB
页数:4页
时间:2019-03-10
《基于java链表的冒泡排序》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、/**Tochangethistemplate,chooseTools
2、Templates*andopenthetemplateintheeditor.*//****@authorC.J.Ward*/classNode{intdate;Nodenext;publicNode(intdata,Nodenext){this.date=data;this.next=next;}publicNode(intdata){this.date=data;this.next=null;}@OverridepublicStringtoString(){retur
3、n"{"+date+"}";}}publicclasssort{Nodehead;publicvoidsinglglist(){this.head=null;}publicbooleanadd(intelement){Nodenode=newNode(element);if(this.head==null){this.head=node;returntrue;}if(this.head!=null){Nodep=this.head;while(p.next!=null){p=p.next;//System.out.print(p.date+",
4、");}p.next=node;}//returntrue;}publicbooleanadd(intindex,intelement)//����element���?����������Ϊindex{//�������ɹ�����true��O(n)if(this.head==null
5、
6、index<=0)//ͷ����this.head=newNode(element,this.head);else//�����?����index>=1{intj=0;Nodep=this.head;while(p.next!=null&&j7、-1)//Ѱ�Ҳ���λ��{j++;p=p.next;}p.next=newNode(element,p.next);//�м�/β����}returntrue;}@OverridepublicStringtoString(){Stringstr="";Nodep=this.head;while(p!=null){//str+=p.date;System.out.print(p.date+",");p=p.next;}returnstr;}publicbooleansort(){if(this.head==null){returnfalse8、;}if(this.head.next==null){returnfalse;}Nodef=null;if(this.head.date>this.head.next.date){f=this.head;this.head=this.head.next;add(1,f.date);}Nodeq=null,front=this.head,t=null;inti=1;while(front.next.next!=null){Nodep=front.next;if(p.date>p.next.date){front.next=front.next.n9、ext;add(i+1,p.date);//System.out.print(p.date+",");//p=p.next;}i++;front=front.next;}returntrue;//}//returntrue;}publicstaticvoidmain(Stringargs[]){sortsofe=newsort();intsort[]={4,5,8,1,2,7,3,6};System.out.print("关键字序列:");for(inti=0;i<=sort.length-1;i++){sofe.add(sort[i]);Sy10、stem.out.print(sort[i]+",");}System.out.println();//sofe.toString();for(inti=0;i
7、-1)//Ѱ�Ҳ���λ��{j++;p=p.next;}p.next=newNode(element,p.next);//�м�/β����}returntrue;}@OverridepublicStringtoString(){Stringstr="";Nodep=this.head;while(p!=null){//str+=p.date;System.out.print(p.date+",");p=p.next;}returnstr;}publicbooleansort(){if(this.head==null){returnfalse
8、;}if(this.head.next==null){returnfalse;}Nodef=null;if(this.head.date>this.head.next.date){f=this.head;this.head=this.head.next;add(1,f.date);}Nodeq=null,front=this.head,t=null;inti=1;while(front.next.next!=null){Nodep=front.next;if(p.date>p.next.date){front.next=front.next.n
9、ext;add(i+1,p.date);//System.out.print(p.date+",");//p=p.next;}i++;front=front.next;}returntrue;//}//returntrue;}publicstaticvoidmain(Stringargs[]){sortsofe=newsort();intsort[]={4,5,8,1,2,7,3,6};System.out.print("关键字序列:");for(inti=0;i<=sort.length-1;i++){sofe.add(sort[i]);Sy
10、stem.out.print(sort[i]+",");}System.out.println();//sofe.toString();for(inti=0;i
此文档下载收益归作者所有