The Java implementation of the sorting algorithm full Raiders _java

Source: Internet
Author: User

Collections.sort ()

Java sorting can be implemented using the Collections.sort () sort function.
There are two ways to sort list by using the Collections.sort method:
The first is that the objects in the list implement the comparable interface, as follows:

/**
* Sort user by order *
/public class User implements comparable<user>{
  private String name;
  private Integer order;
  Public String GetName () {return
    name;
  }
  public void SetName (String name) {
    this.name = name;
  }
  Public Integer GetOrder () {return order
    ;
  }
  public void Setorder (Integer order) {
    this.order = order;
  }
  public int CompareTo (User arg0) {return
    This.getorder (). CompareTo (Arg0.getorder ());
  }
}

Test:

public class test{public

  static void Main (string[] args) {
    user User1 = new user ();
    User1.setname ("a");
    User1.setorder (1);
    User User2 = new user ();
    User2.setname ("B");
    User2.setorder (2);
    list<user> list = new arraylist<user> ();
    Here add user2 add user1
    list.add (user2);
    List.add (user1);
    Collections.sort (list);
    for (User u:list) {
      System.out.println (U.getname ());
    }
  }
}

The output is as follows

A
b

The second approach is based on the Collections.sort overload method, for example:

/**
* Sort User According to order * * The public
class User {//here does not need to implement the comparable interface
  private String name;
  private Integer order;
  Public String GetName () {return
    name;
  }
  public void SetName (String name) {
    this.name = name;
  }
  Public Integer GetOrder () {return order
    ;
  }
  public void Setorder (Integer order) {
    this.order = order;
  }
}

This can be written in the main class:

public class test{public
  static void Main (string[] args) {
    user User1 = new user ();
    User1.setname ("a");
    User1.setorder (1);
    User User2 = new user ();
    User2.setname ("B");
    User2.setorder (2);
    list<user> list = new arraylist<user> ();
    List.add (user2);
    List.add (user1);
    
    Collections.sort (list,new comparator<user> () {public
      int compare (user arg0, user arg1) {
        return Arg0.getorder (). CompareTo (Arg1.getorder ());
      }
    );
    for (User u:list) {
      System.out.println (U.getname ());
    }
  }
}

The output is as follows

A
b

The former code is simple, but can only be sorted according to the fixed attributes, the latter is flexible, you can temporarily specify the sort items, but the code is not concise

The preferred use.

Common sort Algorithms
Here are some Java code practices for the classic sort algorithms:

Bubble sort

 public static void Bubblesort (int a[], int n) { 
    int i, J; 
     
    for (i = 0; i < n-1 i + +) {for 
      (j = 0; J < n-i-1; j + +) { 
        if (A[j] > a[j + 1]) { 
          a[j] = a[j] ^ A[j + 1]; 
          A[j + 1] = A[j] ^ a[j + 1]; 
          A[J] = A[j] ^ a[j + 1];}}} 
   

Direct Insert Sort

public static void Insertsort (int a[], int n) { 
    int i, J, TMP; 
   
    for (i = 1; i < n; i++) { 
      tmp = a[i]; 
   
      for (j = i-1 J >= 0; j--) { 
        if (A[j] > tmp) { 
          a[j + 1] = A[j]; 
        } else {break 
          ; 
        } 
      } 
   
      a[j + 1] = tmp; 
    } 
  } 

Direct Selection Sort

public static void Selectsort (int a[], int n) { 
    int i, J, loc; 
   
    for (i = 0; i < n; i++) { 
      loc = i; 
   
      for (j = i + 1; j < N; j + +) { 
        if (A[j] < A[loc]) { 
          loc = j; 
        } 
      } 
   
      if (loc!= i) { 
        a[i] = a[i] ^ A[loc]; 
        A[loc] = A[i] ^ A[loc]; 
        A[i] = A[i] ^ a[loc];}} 
   

Heap Sort

  /** * Heap sort (small to large) * * @param A * @param n */public static void Heapsort (int a[], int n) {in 
   
    t tmp; 
   
    Constructing Dagen buildmaxheap (A, N); 
      for (int j = n-1 J >= 1; j--) {tmp = a[0]; 
      A[0] = A[j]; 
   
      A[J] = tmp; 
    Maxheapify (A, 0, J); }/** * Build Large heap * * @param A * @param n */private static void Buildmaxheap (int a[], int 
    N) {for (int i = (n-2)/2; I >= 0; i--) {maxheapify (A, I, N); }/** * maintains the maximum heap from subscript I (* * @param A * @param i * @param n * * * private static void Maxh 
   
    eapify (int a[], int i, int n) {int left, right, loc; 
      while (I < n) {left = 2 * i + 1; 
      right = 2 * i + 2; 
   
      loc = i; 
      if (Left < n && A[left] > A[i]) {i = left; 
      } if (right < n && A[right] > A[i]) {i = right; } if (loc!= i) {A[i] = A[loc] ^ a[i]; 
        A[loc] = A[loc] ^ a[i]; 
      A[i] = A[loc] ^ a[i]; 
      } else {break; 
 } 
    } 
  }

Quick Sort

  public static void QuickSort (int a[], int bt, int ed) {if (BT < ed) {int pivot = pivotpartition (A, BT, 
   
      ED); 
   
      QuickSort (A, BT, pivot-1); 
    QuickSort (A, Pivot + 1, ed); 
   
    } private static void Swapvar (int a[], int bt, int ed) {int mid = BT + (ED-BT)/2; 
      If (mid!= bt) {A[BT] = A[bt] ^ A[mid]; 
      A[mid] = a[bt] ^ A[mid]; 
    A[BT] = a[bt] ^ A[mid]; } private static int pivotpartition (int a[], int bt, int ed) {//Take intermediate values as stand to prevent array from appearing in order O (n^2) SWAPV 
   
    AR (A, BT, ed); 
   
    int stand = A[BT]; 
      while (BT < ed) {while (BT < Ed && A[ed] >= stand) {ed--; 
      } if (BT < ed) {a[bt++] = a[ed]; 
      while (BT < Ed && A[BT] <= stand) {bt++; 
      } if (BT < ed) {a[ed--] = A[BT]; 
   
    }} A[BT] = stand; 
  Return BT; 

 }

Merge sort

 public static void MergeSort (int a[], int bt, int ed) { 
    if (BT < ed) { 
      int mid = BT + (ED-BT)/2; 
   
      MergeSort (A, BT, mid); 
   
      MergeSort (A, Mid + 1, ed); 
   
      Mergearray (A, BT, Mid, ed); 
    } 
   
  private static void Mergearray (int a[], int bt, int mid, int ed) { 
    int i, J, k, len = ed-bt + 1; 
    int tmp[] = new Int[len]; 
   
    for (i = bt, j = mid + 1, k = 0; I <= mid && J <= Ed; k++) { 
      if (A[i] <= a[j]) { 
        Tmp[k] = a[i++ ]; 
      } else { 
        Tmp[k] = a[j++]; 
      } 
    } 
   
    while (I <= mid) { 
      tmp[k++] = a[i++]; 
    } 
   
    while (J <= ed) { 
      tmp[k++] = a[j++]; 
    } 
   
    for (i = 0; i < K; i++) { 
      A[BT + i] = Tmp[i]; 
    } 
   

Test program

  To sum up the above algorithms:

 Import Java.util.Scanner; 
   
      public class Javasort {public static void main (String args[]) {Scanner cin = new Scanner (system.in); 
   
      int a[], n; 
        while (Cin.hasnext ()) {n = cin.nextint (); 
   
        A = new Int[n]; 
        for (int i = 0; i < n; i++) {A[i] = Cin.nextint (); 
   
        }//Bubblesort (A, N); 
   
        Insertsort (A, N); 
   
        Selectsort (A, N); 
   
        Heapsort (A, N); 
   
        QuickSort (A, 0, n-1); 
   
        MergeSort (A, 0, n-1); 
      Printarr (A); }/** * Merge sort * * @param A * @param bt * @param ed/public Stati 
   
        c void MergeSort (int a[], int bt, int ed) {if (BT < ed) {int mid = BT + (ED-BT)/2; 
   
        MergeSort (A, BT, mid); 
   
        MergeSort (A, Mid + 1, ed); 
      Mergearray (A, BT, Mid, ed); }/** * Merging arrays * *@param A * @param bt * @param mid * @param ed/private static void Mergearray (int a[], int 
      BT, int mid, int ed) {int I, j, k, len = ed-bt + 1; 
   
      int tmp[] = new Int[len]; for (i = bt, j = mid + 1, k = 0; I <= mid && J <= Ed; k++) {if (A[i] <= a[j]) {tmp[ 
        K] = a[i++]; 
        else {Tmp[k] = a[j++]; 
      } while (I <= mid) {tmp[k++] = a[i++]; 
      while (J <= ed) {tmp[k++] = a[j++]; 
      for (i = 0; i < K; i++) {A[BT + i] = tmp[i]; }/** * Quick sort * * @param A * @param bt * @param ed/public Stati 
   
        c void QuickSort (int a[], int bt, int ed) {if (BT < ed) {int pivot = pivotpartition (A, BT, ed); 
   
        QuickSort (A, BT, pivot-1); 
      QuickSort (A, Pivot + 1, ed); }} Privatestatic void Swapvar (int a[], int bt, int ed) {int mid = BT + (ED-BT)/2; 
        If (mid!= bt) {A[BT] = A[bt] ^ A[mid]; 
        A[mid] = a[bt] ^ A[mid]; 
      A[BT] = a[bt] ^ A[mid]; 
     }/** * Quick-row Find datum point position * * @param A * @param bt * @param ed * @return * * private static int pivotpartition (int a[], int bt, int ed) {//Take intermediate value as stand to prevent array from appearing in order O (n^2) SWAPVA 
   
      R (A, BT, ed); 
   
      int stand = A[BT]; 
        while (BT < ed) {while (BT < Ed && A[ed] >= stand) {ed--; 
        } if (BT < ed) {a[bt++] = a[ed]; 
        while (BT < Ed && A[BT] <= stand) {bt++; 
        } if (BT < ed) {a[ed--] = A[BT]; 
   
      }} A[BT] = stand; 
    Return BT; 
    /** * Heap sort (from small to large) * * @param A * @param n * *public static void Heapsort (int a[], int n) {int tmp; 
   
      Constructing Dagen buildmaxheap (A, N); 
        for (int j = n-1 J >= 1; j--) {tmp = a[0]; 
        A[0] = A[j]; 
   
        A[J] = tmp; 
      Maxheapify (A, 0, J); }/** * Build Large heap * * @param A * @param n */private static void Buildmaxhe 
      AP (int a[], int n) {for (int i = (n-2)/2; I >= 0; i--) {maxheapify (A, I, N); }/** * maintains the maximum heap from subscript I (* * @param A * @param i * @param n * * privat 
   
      e static void maxheapify (int a[], int i, int n) {int left, right, loc; 
        while (I < n) {left = 2 * i + 1; 
        right = 2 * i + 2; 
   
        loc = i; 
        if (Left < n && A[left] > A[i]) {i = left; 
        } if (right < n && A[right] > A[i]) {i = right; 
   
        }if (loc!= i) {a[i] = A[loc] ^ a[i]; 
          A[loc] = A[loc] ^ a[i]; 
        A[i] = A[loc] ^ a[i]; 
        } else {break; }}/** * Direct Select sort * * @param A * @param n */public static void S 
   
      Electsort (int a[], int n) {int i, J, loc; 
   
        for (i = 0; i < n; i++) {loc = i; 
          for (j = i + 1; j < N; j + +) {if (A[j] < A[loc]) {loc = J; 
          } if (loc!= i) {a[i] = A[i] ^ A[loc]; 
          A[loc] = A[i] ^ A[loc]; 
        A[i] = A[i] ^ A[loc]; }}/** * Direct Insert Sort * * @param A * @param n */public static void I 
   
      Nsertsort (int a[], int n) {int I, J, TMP; 
   
        for (i = 1; i < n; i++) {tmp = A[i]; for (j = i-1 J >= 0; j--) {if (A[j] > tmp) {a[j + 1] = A[j];
          } else {break; 
      } a[j + 1] = tmp; }/** * Bubble sort * @param A * @param n */public static void Bubblesort (i 
   
      NT a[], int n) {int i, J; 
            for (i = 0; i < n-1. i++) {for (j = 0; J < n-i-1; j +) {if (A[j] > a[j + 1]) { 
            A[J] = A[j] ^ a[j + 1]; 
            A[j + 1] = A[j] ^ a[j + 1]; 
          A[J] = A[j] ^ a[j + 1]; /** * Print Array * * @param A/public static void Printarr ( int a[]) {for (int i = 0; i < a.length i++) {if (i = = a.length-1) {System.out.printf (" 
        %d\n ", A[i]); 
        else {System.out.printf ("%d", a[i]); 
 } 
      } 
    } 
  }

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.