Ext.: http://blog.csdn.net/spy19881201/article/details/5867721
Https://www.cnblogs.com/hexiaochun/archive/2012/09/03/2668324.html
First, bubble sort
[Java]View PlainCopy
- Package sort.bubble;
- Import Java.util.Random;
- /**
- * Compare adjacent two numbers in turn, place decimals in front, large numbers on the back
- * Bubble sort, with stability
- * Time complexity of O (n^2)
- * Less than heap sort, quick sort O (Nlogn, base 2)
- * @author Liangge
- *
- */
- Public class Main {
- public static void Main (string[] args) {
- Random ran = new Random ();
- int[] sort = new int[10];
- For (int i = 0; i < i++) {
- Sort[i] = Ran.nextint (50);
- }
- System.out.print ("Array before sorting is");
- For (int i:sort) {
- System.out.print (i+"");
- }
- Buddlesort (sort);
- System.out.println ();
- System.out.print ("sorted array is");
- For (int i:sort) {
- System.out.print (i+"");
- }
- }
- /**
- * Bubble Sort
- * @param sort
- */
- private static void Buddlesort (int[] sort) {
- For (int i=1;i<sort.length;i++) {
- For (int j=0;j<sort.length-i;j++) {
- if (sort[j]>sort[j+1]) {
- int temp = sort[j+1];
- sort[j+1] = sort[j];
- SORT[J] = temp;
- }
- }
- }
- }
- }
Second, choose the sort
[Java]View PlainCopy
- Package sort.select;
- Import Java.util.Random;
- /**
- * Select sort
- * Each trip selects the smallest (or largest) element from the data element to be sorted,
- * The order is placed at the end of the ordered sequence, until all the data elements are sorted out.
- * Select Sort is an unstable sort method.
- * @author Liangge
- *
- */
- Public class Main {
- public static void Main (string[] args) {
- Random ran = new Random ();
- int[] sort = new int[10];
- For (int i = 0; i < i++) {
- Sort[i] = Ran.nextint (50);
- }
- System.out.print ("Array before sorting is");
- For (int i:sort) {
- System.out.print (i + "");
- }
- Selectsort (sort);
- System.out.println ();
- System.out.print ("sorted array is");
- For (int i:sort) {
- System.out.print (i + "");
- }
- }
- /**
- * Select sort
- * @param sort
- */
- private static void Selectsort (int[] sort) {
- For (int i =0;i<sort.length-1;i++) {
- For (int j = i+1;j<sort.length;j++) {
- if (Sort[j]<sort[i]) {
- int temp = sort[j];
- SORT[J] = Sort[i];
- Sort[i] = temp;
- }
- }
- }
- }
- }
Three, quick sort
1 package Com.quick; 2 3 public class Quick {4 public void Quick_sort (int[] arrays, int lenght) {5 if (null = = Arrays | | lenght < 1) {6 System.out.println ("Input error!"); 7 return; 8} 9 _quick_sort (arrays , 0, lenght-1);}11 public void _quick_sort (int[] arrays, int start, int end) {if (start>=end) {return;15}16-int i = start;18 int j = end;19 int value = array S[I];20 Boolean flag = true;21 while (i! = j) {if (flag) {(Value > Arrays[j]) {Swap (arrays, I, j); flag=false;26} else {28 j--;29}30}else{31 if (Value<arrays[i]) {32 Swap (arrays, I, j); Flag=true;34}else{35 i++;36 }37}38}39 SNP (arrays); _quick_sort (arrays, start, j-1); _quick_so RT (Arrays, i+1, end),}44, public void SNP (int[] arrays) {$ = (int i = 0; i < arrays. Length i++) {System.out.print (Arrays[i] + "),}49 System.out.println (),}51 (Priva) te void Swap (int[] arrays, int i, int j) {int temp;54 temp = arrays[i];55 Arrays[i] = arrays[j] ; Arrays[j] = temp;57}58 public static void Main (String args[]) {Quick q = new quick (); 61 Int[] A = {$, 65,12,45,5,};62 q.quick_sort (a,6); 63} 64 65}
Quick Sort Explanation
The idea of the so-called quick sort is to first take out the first number of the array as a key, set a i,j as the identity before and after each, and then take the key to the array from behind, and j--, until we find the first number less than this key, and then exchange the two values, after the exchange is complete , we take this key to go backwards from I, and i++, loop until the end of the i=j, when this is over, we will find that the value greater than this key will go to the back of the key, not if you write the wrong, less than this key will run to the front of this value Then we can complete the ordering of the entire array by invoking the array recursively.
Represented by a graphical representation of the following:
This will be divided into two segments of the key, we will take these two paragraphs further to solve the problem.
Java Common algorithms