JAVA basic instance (iii) -- Sort
Bubble Sorting is a simple sorting algorithm. It repeatedly visits the series to be sorted, compares two elements at a time, and exchanges them if their order is wrong. The work of visiting a sequence is repeated until there is no need for exchange, that is, the sequence has been sorted. The name of this algorithm comes from because the smaller elements will slowly "float" to the top of the series through the exchange.
Public class BubbleSort {
Public static void main (String [] args ){
Int score [] = {67, 69, 75, 87, 89, 90,99,100 };
For (int I = 0; I <score. length-1; I ++) {// a maximum of N-1 sequential sorting can be performed.
For (int j = 0; j <score. length-I-1; j ++) {// score the current unordered range [0 ...... length-i-1] sorting (j range is critical, this range is gradually narrowing)
If (score [j] <score [j + 1]) {// swap a small value to the end
Int temp = score [j];
Score [j] = score [j + 1];
Score [j + 1] = temp;
}
}
System. out. print ("th" + (I + 1) + "secondary sorting result :");
For (int a = 0; a <score. length; a ++ ){
System. out. print (score [a] + "\ t ");
}
System. out. println ("");
}
System. out. print ("final sorting result :");
For (int a = 0; a <score. length; a ++ ){
System. out. print (score [a] + "\ t ");
}
}
}
Fast sorting is faster, because compared with Bubble sorting, each exchange is Skip. Set a benchmark for each sort. Place all the numbers smaller than or equal to the benchmark to the left of the benchmark, and all the numbers greater than or equal to the benchmark to the right of the benchmark. In this way, the exchange between adjacent numbers is not the same as the bubble sort. The exchange distance is much larger. Therefore, the total number of comparisons and exchanges is reduced, and the speed naturally increases. Of course, in the worst case, it is still possible that two adjacent numbers are exchanged.
Void quicksort (int left, int right)
{
Int I, j, t, temp;
If (left> right)
Return;
Temp = a [left]; // The reference number is stored in temp.
I = left;
J = right;
While (I! = J)
{
// The order is very important. You need to start from the right
While (a [j]> = temp & I
J --;
// Find the right
While (a [I] <= temp & I
I ++;
// Swap the positions of two numbers in the array
If (I
{
T = a [I];
A [I] = a [j];
A [j] = t;
}
}
// Final normalization of the base number
A [left] = a [I];
A [I] = temp;
Quicksort (left, I-1); // continue to process the left, here is a recursive process
Quicksort (I + 1, right); // continue to process the right side. Here is a recursive process.
}
Insert sorting means that each step inserts a data to be sorted to an appropriate position in the sorted data according to its size until all data is inserted.
Http://www.cnblogs.com/kkun/archive/2011/11/23/2260265.html
static void insertion_sort(int[] unsorted) { for (int i = 1; i < unsorted.Length; i++) { if (unsorted[i - 1] > unsorted[i]) { int temp = unsorted[i]; int j = i; while (j > 0 && unsorted[j - 1] > temp) { unsorted[j] = unsorted[j - 1]; j--; } unsorted[j] = temp; } } }