Alibabacloud.com offers a wide variety of articles about bubble sort algorithm java, easily find your bubble sort algorithm java information here online.
Ideas:The bubbling sort compares the adjacent two elements each time, and if the former is larger than the latter, it is exchanged. Until all elements have been traversed, no elements can be exchanged.Original array: 4 7 9 3 5Inner Layer First Iteration: 4 7 3 5 9Inner Layer Second Iteration: 4 3 5 7 9Inner Layer Third iteration: 3 4 5 7 9Inner Layer Fourth Iteration: 3 4 5 7 9templateclassT >voidBubblesort (T data[],intilenght) { for(inti =0; I 1
Principle: It repeatedly visits the sequence to sort, compares two elements at a time, and swaps them if their order is wrong. The work of the sequence of visits is repeated until no more need to be exchanged, that is, the sequence is sorted. Packagecom.java1234.chap02.sec06; Public classDemo1 { Public Static voidMain (string[] args) {int[]arr={4,21,0,-12,-3}; inttemp; //number of cycles n-1 times for(inti=0;i){ //number of comparis
Bubble sort One, plot 1, used to use Excel to draw analysisSecond, the cycle processThree, the Circulation law The outer loop is array.length-1, because the last number does not need to be compared with the next one.The inner loop is array.length-1-I; Because each cycle is one, the largest one is placed at the bottom, so the book below does not need to be comparedFour, the code Public classSort { Public S
++) { if(Test[i] 1]) { inttemp = Test[i +1]; Test[i+1] =Test[i]; Test[i]=temp; } }}/*analysis, such as the above program, the outer loop of N-times of the inner layer program, and each time the internal program cycle and the outer layer has a linear relationship, such as J = 0 o'clock, the internal program loop test. Length-1 times, j = 1 o'clock, the internal program loops the test. Length-2 times, and when j = Test. Length-1, the internal program loops 0 times, w
PHP programmers who don't have algorithms are not good programmers ~Bubble sorting: There are n numbers, and the first sort will be the smallest (or largest) to the far right, by comparing the number 22 from the leftmost to the rightmost. Similarly, the second time the smallest (or largest) number of n-1 to the penultimate position. And so onKey points: 22 comparison of two adjacent numbersComplexity of Tim
Bubble sortFirst, step① compare adjacent elements in the list, if they are reversed, swap positions, make the next group, and after multiple comparisons, the largest element "sinks" to the end of the list② Compare the next round until after the n-1, the list order is completeSecond, JavaScript code implementation12345678 9Ten -Three, algorithm analysisFor arrays with input size n, the number of key valu
The Bubble sorting algorithm works as follows: (from back to forward)Compares the adjacent elements. If the first one is bigger than the second one, swap them both.Do the same for each pair of adjacent elements, starting with the last pair from the first pair to the end. At this point, the last element should be the maximum number.Repeat the above steps for all elements, except for the last one.Repeat the a
varNums = [];//define an empty array for(vari=0;i//to have the user enter five numbers, loop 5 timesNums[i] = parseint (Prompt (' Please enter Number '));//using a For loop, prompt prompts the user to enter a number} console.log (' The user entered is: '); Console.log (nums);//print out all the numbers entered by the user for(vari=0;i){ for(varj=0;j){ if(Nums[j]>nums[j+1]) {//A number with index 0 is compared to a number indexed to 1, and so on if the order is descen
Package Com.huan;import Java.util.arrays;import Java.util.random;public class Bubblesort {public static void main ( String[] args) {int[] data = new Int[10];for (int i = 0; i Algorithm: Bubble sort
Li = [33,2,1,10] for in range (Len (LI)-1)://Because range (4) is 0 1 2 3 (not including 4) when i= 3 o'clock Next_ Value does not exist if li[i] >li[i + 1] := li[i ]= li[i + 1] li[i +1] = tempprint (LI)//put the largest to the lastThe first time the operation. After each operation put Len (LI)-1-1Therefore, these operations can be replaced by a loop outside: for in range (1, Len (LI)): is in range (Len (LI)- j): if li[ I] > li[i+1]: = Li[i]
) -1-i):If A[J]GT;A[J+1]:A[J],A[J+1] = A[j+1],a[j]Print (a) summarize the process: The result of the first inner loop is to find the maximum valueThe result of the second inner loop is to find the second largest value, this time ignoring the comparison of the last elementThe result of the third inner loop is to find the third largest value, this time ignoring the second and last element of the comparison the fourth inner loop results in finding the fourth largest value, this time ignoring the th
1 Importjava.util.Arrays;2 3 Public classBubblesort {4 5 Private Static int[] Intarray = {100,-2, 56, 65, 46, 488, 145, 666, 555, 71, 0, 1, 65, 68 };6 7 Public Static voidBubblesort (int[] intarray) {8 for(inti = 0; i ) {9 for(intj = 0; J ) {The range of J values decreases as the I value increases. The outer layer of each cycle, the larger element floats before the top. Ten if(Intarray[j] > intarray[j + 1]) { One inttemp =Intarr
"number of times" for n-1 trip;3, each trip to compare the number of data is less than the previous trip, the first trip to compare N (that is, compare n-1 times);4, not once compared, if "left data" is found to be greater than "right data", the two exchange position.The code demonstrates the following:PHP$a=Array(9,3,5,8,2,7);//Subscript is 0,1,2,3,4,5Echo"Before sorting:";Print_r($a);$n=Count($a);//number for($i= 0;$i$n-1;++$i)//This is the n-1 trip .{ for($k= 0;$k$n-$i-1;++$k)//that's th
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.