The first is the Eightalgorithms.java file, the code is as follows:Import java.util.arrays;/* * Implements eight commonly used sorting algorithms: Insert sort, bubble sort, select sort, Hill sort * and quick sort, merge
Java Learning (7), array, search algorithm, binary search method, bubble sort, select sort, insert sort, java bubble
I. Common array search algorithms
Working principle: it is also call
follows :1 voidBubble_1 (intR[],intN)2 { 3 intI= n-1;//initially, the last position remains the same4 while(i> 0) { 5 intpos= 0;//no record exchange at the beginning of each trip6 for(intj= 0; j) 7 if(R[j]> r[j+1]) { 8Pos= J;//record the location of the interchange9 intTMP = R[j]; r[j]=r[j+1];r[j+1]=tmp; Ten } Onei= POS;//prepare for the next sequencing A } -}2. The traditional
two two sequential sequences in one-dimensional array into an ordered sequence. The merging algorithm can also be implemented by recursive algorithm, which is simpler in form, but has poor practicability.
The number of merged algorithm merges is a very important amount, according to the calculation when there are 3 to 4 elements in the array, the number of merges
is 2 times, when there are 5 to 8 elements, the number of merges is 3, when there are 9 to 16 elements, the number of merges is 4, ac
Bubble sort of classic algorithms in Java (Bubble sort)Principle: compare two adjacent elements and exchange large value elements to the right end.Idea: Compare adjacent two numbers in turn, place decimals in front, and large numbers on the back. That is, in the first trip:
First, overview: This paper mainly introduces the principle of several kinds of sorting algorithms and Java implementation, including: Bubble sort, select sort, insert sort, quick sort, merge
{ Public Static voidMain (string[] args) {int[] arr={6,3,8,2,9,1}; System.out.println ("Array before sorting is:"); for(intNum:arr) {System.out.print (num+" "); } for(inti=0;i//Outer loop control sequencing number of trips for(intj=0;j//Inner loop control how many times each trip is sorted if(arr[j]>arr[j+1]) {inttemp=Arr[j]; ARR[J]=arr[j+1]; Arr[j+1]=temp; }}} System.out.println (); System.out.println ("The sorted array is:"); for(intNum:arr) {System.out.print (num+" ");
second comparison, and the third comparison is You only need to compare the number other than the last two numbers, and so on ... That is, not a trip to compare, each trip less than once, to a certain extent, reduce the amount of the algorithm.Code implementation:public class Bubble { public static void Main (String [] args) { int [] arr={1,7,9,11,158,97,666}; System.out.print ("Array before sorting:"); for (int num:arr) {
Bubble Sort: is the two elements that are contiguous by index, and if greater than/less than (depending on whether ascending or descending order is required), replace, otherwise do not make changes this round down, compares the n-1 times, n equals the number of elements; n-2 , n-3 ... One to the last round, compared 1 times so the comparison is decremented: from n-1 to 1 The total number of comparisons
far left and still not find a smaller data than pivot while(Rightpoint>left array[--rightpoint]>pivot); //left and right pointers overlap or intersect if(Leftpoint >=rightpoint) { Break; }Else{ //swap big and right small data on the leftswap (Leftpoint,rightpoint); } } //returns the cutoff point, which is the leftmost point in the right sub-array returnLeftpoint; } /*** Exchange Data*/ Public voidSwapintL
}Description: The bubbling sort is a comparison between 22, such as: 4.2.5.1.7: The first internal loop, 4 and 2 than-->2,4, 4 and 5 than-->4,5, 5 and 1 than-->5,1, 5, and 7 than-->5,7.So the largest of the number to the end of the array, the second time to make a comparison as long as the array length-2 times, is 3. Insert Sort: public static void insertsort (int [] a) { for (int i = 1; i for (int
Exchange sort(1) Bubble sort1, the basic idea: in order to sort a group of numbers, the current is not yet ranked in the range of all the number, top-down to the adjacent two numbers in turn to compare and adjust, so that the larger number to sink, smaller to the top. That is, each time a comparison of two adjacent numbers finds that they are in the opposite orde
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.