Alibabacloud.com offers a wide variety of articles about bubble sort algorithm java, easily find your bubble sort algorithm java information here online.
Bubble Sort Algorithm (JAVA)
I. Overview
The day before yesterday, someone asked the next sorting algorithm implementation, so draw time to write a bubble sorting algorithm. The
for the bubble sort is . Algorithm stabilityThe bubble sort is to move the small element forward or the large element back. The comparison is an adjacent two element comparison, and the interchange also occurs between these two elements. So, if the two elements are equal
[7]]) inner layer seventh trip: [(3rd [7]]) inner layer eighth trip: [[7] (2nd [7]]) inner layer nineth trip: [7, (+), (+), (1st) [7]]) ...In fact, the bubble sort is similar to the choice of sorting, the same number of times, is n * (n + 1)/2, but the bubble sort in the selection of the minimum value of the process w
Bubble sorting basic idea: In order to sort the number of the current is not yet ranked in the range of all the number, the top-down to the adjacent two numbers in turn to compare and adjust, so that the larger number to sink, smaller upward. That is, each time a comparison of two adjacent numbers finds that they are in the opposite order of order, they are interchanged.Borrow Hip-hop
comparisons of bubble sort
The method of J represents a bubble sort of 22 of the comparison, as for j
Otherwise, the array is out of bounds.*/public void Bubblesort () {for (int i = 0; i for (int j = 0; j }}
/**
The method is mainly an array of variables and displays*/public void Di
Principle
Bubble sort is probably the algorithm that all programmers use, and one of the most familiar algorithms.Its ideas are not complicated:Set now to sort the array arr[], it has n elements.1. If n=1: Obviously no platoon. (In fact, this discussion doesn't seem necessary)2. If n>1:(1) We start with the first elem
First, what is bubble sort: the bubbling sort is compared between two adjacent numbers, where the previous value is defined as before, and the following value is defined as after; when Before>after I, exchange their values, if beforeThe time complexity of bubble sort is indi
, the array for us relatively orderly, but for the bubbling algorithm of the program, There is no difference from disorder.After the repeat operation, 9 will be in the last one, and our first layer loop has been executed. The second time, another cycle, repeat the operation to the last, only one comparison is the first and second comparison,Above is the principle and thought of bubble sort.Two-point SearchW
Bubble Sort Algorithm Demo diagram:
public static void Bubblesort (int[] array) {
//The next two options can be
///mode 1 for
(int i = array.length-1; i > 0; i--) {
for (int j = 0; J
The above is the entire contents of this article, I hope to be able to understand the Java
Principle: Assigning large values of adjacent elements to the rightIdea: ①1. Compares the first element in the collection or array to the second element and assigns a larger value to the right;2. Compare the second element to the third one, and the larger value to the right;.......(N-1). Compares the N-1 element with the nth element and assigns a larger value to the right;From this, the maximum value of the element in the set is obtained and assigned to the element n;② Repeat Operation ① get the
1 Basic IdeasSet the sort table to n, from backward forward or backward 22 to compare the values of adjacent elements, if the relative order of the two are not (A[i-1] > A[i]), then exchange them, the result is to exchange the smallest element to the first position of the sequence to be sorted, we call it a trip bubbling. When the next trip bubbles, the smallest element determined by the previous trip no longer participates in the comparison, and the
[Basic idea]Bubble sort is a sort of exchange, and its basic idea is to compare the keywords of the adjacent records by 22, if the reverse order is exchanged until there are no reversed records.[Java Implementation]public class Bubblesort {public static void main (string[] args) {int[] arr = {49, 38, 65, 97, 76, 13, 27
Basic idea:In the set of numbers to be sorted, the total number in the range that is not currently in sequence, the top-down pairs of adjacent two numbers are compared and adjusted sequentially, so that the larger number to sink , smaller upward. That is, each time a comparison of two adjacent numbers finds that they are in the opposite order of order, they are interchanged. Algorithm implementation: (HDU 1040 pro-Test AC)#include using namespacestd;C
number of the second comparison, and the third time, You just need to compare the numbers except the last two, and so on ... In other words, without a comparison, each trip less than once, to a certain extent, reduce the amount of the algorithm.
In terms of time complexity:
1. If our data is in positive order, just take a trip to complete the sorting. The required number of comparisons and the number of records moved to a minimum, namely: cmin=n-1;
:
Package com.zc.manythread;
Import Java.util.Random;
/**
* Bidirectional bubble sort
* @author Me Yes
*
*/
public class Bbsort {
Bidirectional bubbling algorithm, which greatly reduces the number of cyclic sorting
Public int[] Sort (int[] a) throws exception{
Int J;
int limit=a.length;
int st=-1;
whi
is: [11, 25, 26, 12, 45, 78]. 2, the second round of sorting, starting from the head of the array to compare. The first is 11 and 25 comparison, because the front number is small, so the position does not move. Comparing 25 and 26, the position does not move because the number of the front is small. After the second round of sorting, the order is: [11, 25, 12, 26, 45, 78]. 3, the third round of sorting, starting from the head of the array to compare. The first is 11 and 25 comparison, because
1 /**2 * Bubble Sort (the current value is compared to the next value in turn)3 */4 Public classBubblesort5 {6 Public Static voidMain (string[] args)7 {8 sort ();9 }Ten Public Static voidsort () { One A int[] arr =New int[]{80,15,31,20,59}; - - for(inti=1;i){ the for(intj=0;j){ - if(arr[j]>arr[j
One: Recursive algorithmConcept: Call your own methodThe sample code is as follows:1 @Test2 /**3 * Recursive summation4 * 5+4+3+2+1=155 */6 Public voidgetsum () {7 Longsum = SUM (5);8System.out.println ("sum=" +sum);9 }Ten One Public Static LongSuminti) { A if(1 = =i) { - return1; -}Else { the returni + SUM (i-1); - } -}Explain:When I=5, return to 5+sum (4),When i=4, return to 4+sum (3),When I=3, return to 3+sum (2),When i=2
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.