Bubble + fast + heap Sorting

Source: Internet
Author: User

 Packagexie.struct;Importjava.util.ArrayList; Public classSort<textendsComparable<t>> {     Public Static Final intQuickSort = 1;  Public Static Final intSelectmin = 4;  Public Static Final intMaopao = 3;  Public Static Final intHeapsort = 2; PrivateArraylist<t> data;//Data Storage     PublicSort () {} Public voidRunsort (intmode) {        Switch(mode) { Case1: System.out.println ("Quick Sort output:");  This. QuickSort (data, 0, Data.size ()-1);  Break;  Case2: System.out.println ("Heap Sort output:");  This. Heapsort ();  Break;  Case3: System.out.println ("Bubbling sort output:");  This.            Maopao ();  Break;  Case4:             Break; default:             Break; }    }     Public voidSetData (arraylist<t>a) { This. data =A; }     Public voidaddData (T data) { This. Data.add (data); }     PublicString toString () { for(inti = 0; I < data.size (); i++) System.out.print (Data.get (i)+ "/"); return NULL; }         Public voidMaopao () {inti,j; intn= This. Data.size ();  for(i=0;i<n;i++)               for(j=1;j<n-i;j++){                if( This. Data.get (J-1). CompareTo ( This. Data.get (j)) >0) {T swap= This. Data.get (j);  This. Data.set (J, This. Data.get (j-1));  This. Data.set (j-1, swap); }            }    }            /** Quick Sort Code*/    Private voidQuickSort (Arraylist<t> A,intLeftintRight ) {        if(Left <Right ) {            intLow =Left ; intHigh =Right ; T Key=a.get (Low);  while(Low <High ) {                 while(Low < High && a.get (high) CompareTo (key) >= 0) High--;                A.set (Low, A.get (high));  while(Low < High && a.get (Low). CompareTo (key) < 0) Low++;            A.set (High, A.get (low));            } a.set (Low, key); QuickSort (A, left, low-1); QuickSort (A, low+ 1, right); }    }    /** * Heap sort function Code*/    Private voidbuildheap () {intLen = This. Data.size (); inti;  for(i = LEN/2-1; I >= 0; i--)             This. Adjustheap (i); }    Private voidAdjustheap (intindex) {        intLen = This. Data.size (); intleft = index * 2 + 1; intright = Index * 2 + 2; intsmaller =Left ;        T swap; if(Left > Len-1)            return; if(Right <Len&& This. Data.get (left). CompareTo ( This. Data.get (right)) > 0) {Smaller=Right ; }        if(Smaller <Len&& This. Data.get (Index). CompareTo ( This. Data.get (smaller)) > 0) {Swap= This. Data.get (smaller);  This. Data.set (Smaller, This. Data.get (index));  This. Data.set (index, swap);  This. Adjustheap (smaller); }    }    Private voidHeapsort () { This. Buildheap (); intLen = This. Data.size ();  while(Len > 0) {System.out.print ( This. Data.get (0) + "/");  This. Data.set (0, This. Data.get (len-1));  This. Data.remove (len-1); Len--;  This. adjustheap (0); }    }    /** * Test Code*/     Public Static voidMain (String args[]) {ArrayList<String> array =NewArraylist<string>(); Array.add (A); Array.add (D); Array.add (G); Array.add (H); Array.add (S); Array.add (J); Array.add (B); Sort<String> sort =NewSort<string>();        Sort.setdata (array);        Sort.runsort (Sort.quicksort);                Sort.tostring ();        Sort.setdata (array);        Sort.runsort (SORT.MAOPAO);                Sort.tostring ();        Sort.setdata (array);    Sort.runsort (Sort.heapsort); }}

Quick Sort Output:
a/b/d/g/h/j/s/

Bubble sort Output:
a/b/d/g/h/j/s/

Heap Sort output:
a/b/d/g/h/j/s/

Bubble + fast + heap Sorting

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.