Common Java sorting algorithms

Source: Internet
Author: User

Common Java sorting algorithms

Bubble sort, select sort, quick sort

1  PackageCom.javaee.corejava;2 3  Public classDatasort {4 5      PublicDatasort () {6         //TODO auto-generated Constructor stub7     }8 9      Public Static voidMain (string[] args) {Ten         int[] p = {34, 21, 54, 18, 23, 76, 38, 98, 45, 33, 27, 51, 11, 20, 79,   One30, 89, 41 };  A    -         LongStart =System.currenttimemillis ();  -    theDatasort.bubblesort (P);//Bubble Sort -         //Datasort.selectsort1 (p);//Select sort 1 -         //Datasort.selectsort2 (p);//Select sort 2 -         //Datasort.quicksort (p, 0, p.length-1);//Quick Sort +    -System.out.println ("Time used:" + (System.currenttimemillis ()-start));  +          for(inti = 0; i < p.length; i++) {   ASystem.out.print (P[i] + "");  at         }   -  -     } -     //Bubble Sort -      Public Static voidBubblesort (int[] data) { -          for(inti=0;i<data.length-1;i++){ in              for(intj=0;j<data.length-i-1;j++){ -                 if(data[j]>data[j+1]){ to                     inttemp=Data[j]; +Data[j]=data[j+1]; -data[j+1]=temp; the                 } *             } $         }Panax Notoginseng     } -     //Select sort 1 the      Public Static voidSelectSort1 (int[] data) { +         if(data==NULL|| Data.length==0){ A             return; the         } +          for(inti=0;i<data.length-1;i++){ -             intmin=i; $              for(intj=i+1;j<data.length;j++){ $                 if(data[j]<Data[min]) { -min=J; -                 } the             } -             if(i!=min) {Wuyi                 inttemp=Data[i]; thedata[i]=Data[min]; -data[min]=temp; Wu             } -         } About     } $     //Select sort 2 -      Public Static voidSelectSort2 (int[] data) { -          for(inti=0;i<data.length-1;i++){ -              for(intj=i+1;j<data.length;j++){ A                 if(data[i]>Data[j]) { +                     inttemp=Data[i]; thedata[i]=Data[j]; -data[j]=temp; $                 } the             } the         } the     } the     //Quick Sort -      Public Static voidQuickSort (int[] Data,intStartintend) { in         intkey=Data[start]; the         intI=start; the         intj=end; About          while(i<j) { the              while(data[j]>key&&i<j) { thej--; the             } +data[i]=Data[j]; -              while(data[i]<key&&i<j) { thei++;Bayi             } thedata[j]=Data[i]; the         } -data[i]=key; -         if(i-1>start) { theQuickSort (data,start,i-1); the         } the         if(i+1<end) { theQuickSort (data,i+1, end); -         } the     } the  the}

Common Java sorting algorithms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.