1. Sort by bubbling
/** Bubble Sort: In the set of numbers to be sorted, the total number of the ranges that are not currently in sequence, the exchange of two numbers adjacent to the left and right pairs of adjacent two numbers **/ Public voidBubblesort (int[] num) { inttemp = 0; for(inti=0;i<num.length-1;i++){ for(intj=0;j<num.length-1-i;j++){ if(num[j]>num[j+1]) {temp=Num[j]; NUM[J]=num[j+1]; Num[j+1]=Temp } } } }
Call:
int[] num = new int[] {2, 4, 5, 1, 6, 8};
Shuzu sz1 = new Shuzu ();
Sz1.bubblesort (num);
for (int i = 0; i < num.length; i++) {
System.out.println ("bubble method sort =" + Num[i] + "");
}
2. Quick Sort Method: The largest number on the last side
//Sort by: Find the largest number in sequence and then release Public voidSort (int[] nums) { intMaxpost, temp;//loop inside try not to define variables for(intk = 0; K < nums.length-1; k++)//Number of executions{maxpost= 0;//assuming that the first number is the largest, the loop will find a large number once, so it is-K for(inti = 1; i < nums.length-k; i++) { if(Nums[maxpost] <Nums[i]) {Maxpost=i; }} Temp= Nums[nums.length-k-1];//each time the exchange is the last numberNums[nums.length-k-1] =Nums[maxpost]; Nums[maxpost]=temp; } }
Call:int[] num = new int[] {2, 4, 5, 1, 6, 8};
Shuzu sz = new Shuzu ();
Sz.selectsort (num);
for (int i = 0; i < num.length; i++)
{
System.out.println (Num[i] + "");
}
Array sorting, bubbling method, fast sorting method