Quick Sort VS Compare sort

Source: Internet
Author: User

For convenience, I wrote the Bean into an internal class

Test conclusion, quick sort amputated compare sort, self-test

1     //Test Call2      Public Static voidMain (string[] args) {3list<student> list =NewArraylist<student>();4         Newtest (). Add (list);5         //record old data to determine if two orders are using the same data6List<student> Stus =NewArraylist<student>();7Stus =list;8         9         LongAA =NewDate (). GetTime ();TenQuickSort (list, 0, List.size ()-1); OneSystem.out.println ("Quick sort" + (NewDate (). GetTime ()-aa)); A  - System.out.println (); -          theAA =NewDate (). GetTime (); -Collections.sort (Stus,Newindexcomparator ()); -System.out.println ("compare sort" + (NewDate (). GetTime ()-aa)); -}

1 // Create a 100w bar student 2      Public void Add (list<student> List) {3while          (list.size () < 1000000  {4             list.add (new Student (new Random (). Nextint (1000000) + 1), "")) ; 5         }6     }
1 //Quick Sort2      Public Static voidQuickSort (list<student> data,intLeftintRight ) {3Student temp =NewStudent ();4         intF, Rtemp, ltemp;5Ltemp =Left ;6Rtemp =Right ;7f = data.get (left + right)/2). GetCount ();8          while(Ltemp <rtemp) {9              while(Data.get (ltemp). GetCount () <f) {Ten++ltemp; One             } A              while(Data.get (rtemp). GetCount () >f) { ---rtemp; -             } the             if(Ltemp <=rtemp) { -temp =Data.get (ltemp); - Data.set (Ltemp, Data.get (rtemp)); - data.set (rtemp, temp); +--rtemp; -++ltemp; +             } A         } at         if(left = =rtemp) { -ltemp++; -         } -         if(Left <rtemp) { -QuickSort (data, rtemp + 1, right); -         } in         if(Ltemp <Right ) { -QuickSort (data, rtemp + 1, right); to         } +}
1     /**2 * Compare Sort3      */4     Static classIndexcomparatorImplementsComparator<student> {5 6 @Override7          Public intCompare (Student O1, Student O2) {8Integer Sort1 =O1.getcount ();9Integer Sort2 =O2.getcount ();Ten             returnSort1.compareto (SORT2); One         } A}

Entity Student

1 Static classStudent {2         Private intcount;3 4         PrivateString name;5 6          Public intGetCount () {7             returncount;8         }9 Ten          PublicStudent () { One             Super(); A             //TODO auto-generated Constructor stub -         } -  the          PublicStudent (intcount, String name) { -             Super(); -              This. Count =count; -              This. Name =name; +         } -  +          Public voidSetCount (intcount) { A              This. Count =count; at         } -  -          PublicString GetName () { -             returnname; -         } -  in          Public voidsetName (String name) { -              This. Name =name; to         } +}
View Code

Quick Sort VS Compare sort

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.