5,5 is greater than 3, do not need
to Exchange 3 5 2 6
to continue comparing 2 and 2,5 greater than 5, Exchange position 2 3 2 + 5
continue to compare 6 and 6,6 greater than 2, do not need to Exchange
5 5
3 2 5 Continue to compare 6 and 2,6 greater than 2, swap position
3 2 5 2 6 6
sink to the last, two 2 are all up (fore).
Second round
Compare 3 and 2, 3 is greater than 2, Exchange position 2 3 5 2 6
Compare 3 and 5, 5 is greater than 3, do not need to Exchange 2 3 5 2 6
compare
the current unordered interval score[0......length-i-1] (the range of J is critical, the range is gradually shrinking)6if(Score[j] //swap the small value to the back7inttemp =Score[j];8 Score[j] = score[j + 1]; 9 Score[j + 1] =temp;10 }11 } System.out.print ("No." + (i + 1) + "Order results:");13 for(intA = 0; A ){System.out.print (Score[a] + "\ T");15 }System.out.println ("");17 }System.out.print ("Final
First, insert sort algorithm to implement Java version
public static int[] Insert_sort (int[] a)
{to
(int i = 0; i
Second, the choice of sorting algorithm to implement the Java version
public static int[] Select_sort (int[] a)
{for
(int i = 0; i for (int j=i+1;j
Three, bubble
Bubble sort principle, below in ascending order for example
Compares the adjacent elements. If the first one is bigger than the second one, swap them both.
For each pair of adjacent elements to do the same work, from the beginning of the first pair to the end of the last pair, at this time, the final element is the largest.
Repeat the above steps for all elements, except for the last one.
Repeat t
Bubble sort (Bubble sort) is a simpler sort algorithm in the field of computer science.It repeatedly visited the sequence to sort, comparing two elements at a time, and swapping them out if they were wrong in the order. The work o
Bubble sort (Bubble sort) is a simple sort algorithm. It repeatedly visited the sequence to sort, comparing two elements at a time, and swapping them out if they were wrong in the order. The work of the sequence of visits is repea
, 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 SearchWhen we look for an ordered linear table, and
First, the principle of the algorithm.The Bubble sorting algorithm works as follows:
Compares the adjacent elements. If the first one is bigger than the second one, swap them both.
Do the same work 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
Bubble Sort
Bubble sort (Bubble sort), see this algorithm, I think of a word "decimal floating, large number of sinks," through the comparison of layers to make the decimal surface, and the large number of "stone sink bottom." So
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 element, compare every two adjacent elements, if
Sort-bubble sortBasic idea: start at one end, compare the adjacent two elements, and find that the reverse is exchange. Here, you'll compare adjacent elements one by one, from backward forward (top to bottom).Average Time: O (N2)Best case: O (N)Worst case scenario: O (n2) (reverse)Auxiliary Space: O (1)Stability: Stableapplicable scenario:N Compare hoursJava implementations:1 Public Static voidBubbleso
Bubble sortBasic idea: In the group of numbers to be sorted, the total number of unsorted in the current range, the top-down pair of adjacent two numbers sequentiallyCompare and adjust, let the larger number go down, the smaller go up. Public classbubblesort{ Public Static voidMain (string[] args) {int[] a={1,5,6,5,4,6,4,8}; inttemp = 0;//define where temporary variables are used to exchange data for(inti=0; I {//Outer loop indicates the number of whe
Pick up all the previous studies.Directly on the code:public class Bubble{public static void Main (String [] args) {int[] arr = new int[]{2,3,1,4,5,4,2,6,0,7,21};int[] Arrafterso RT = Sort (arr,arr.length), for (int i = 0;i Bubble sort Java
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.