Sorting needs to be mastered by having a bubbling sort, inserting sort and selecting sort.Bubble sort: The outer loop goes from back to front, the memory loops from the back to the outer loop, the adjacent array item 22 compares, and the larger value is moved back.Insert sort: Starts at the middle of the
1. Bubble sort1) Principle: iterate over the sequence to be sorted, a comparison of two elements at a time, assuming they are in the wrong order to exchange them. The work of visiting the series is repeated until there is no need to exchange, that is to say, the sequence is sorted out. 2) Code implementation:Package Com.test.sort;public class bubblesort{public static void sort (int[] data) {for
significantly lower than the element interchange) The index of the variable representing the minimum value is then compared to the index of the starting element, and if it is not the same, the starting element is interchanged with the minimum element (so that the order is completed by a smaller number of interchange cycles).1 for(inti = 0; i ) {2 intindex =i;3 intValue =Arr[i];4 //If there is a better element than the starting element value, assign t
Bubble sort: There are many kinds of enshin, some first determine the maximum value put to the back, some first determine the minimum value to the front, there is the reverse, first determine the minimum value of the position, but the essence is: constantly 22 comparison, exchange position ... The first trip to determine a maximum (minimum) value placed in front (behind), the second trip, the exclusion of t
>a;i--) {//Outputs 5 spaces for a loop, reducing this to 1;Systen.out.print (""); The loop output space is not bhakta;} Systen.out.println ("*"); The first line ends with a newline so use println;} else{for (int i = 5;i>a;i--) {//Outputs 5 spaces for a loop, reducing this to 1;Systen.out.print (""); The loop output space is not bhakta;Systen.out.println ("*"); Then the output space, plus *;if (a==5/2+5%2) {//is taken to the middle value;for (int b =1;bSysten.out.print (""); The loop output space
encoding table when storing Chinese characters (Chinese characters occupy four bytes in unicode/utf-8), char can be computed with int, prompt is int type, two bytes in memory.
Char value range is 0-65535, unsigned data type (cannot store negative numbers)
The value range for short is -32768~32767
public class Asciidemo{public static void Main (string[] args) {char c = ' a '; int i = c + 1; The char type is converted to type int System.out.println (i), int j = 90;char h = (char
of comparisons, and the 11th line for loop controls the number of I (minimum or maximum) and the remaining number of comparisons2. Bubble sortThe principle is that the adjacent number 22 is compared, in order from small to large or from large to small to exchange,After such a trip, the largest or smallest number was exchanged to the last,And then start from the beginning to the 22 comparison exchange, until the end of the second placecode example:1
For the improvement of bubble sort, thanks to Morewindows (Microsoft's most valuable expert), to provide me with such a good idea, I just wrote a full Java version.
/** * * @author Fly/public class Bubblesort {//No thought write exchange sort//not bubble
1 Packagelog;2 3 Public classTest4 {4 5 /**6 * Java algorithm---bubble sort7 * 8 * @paramargs9 */Ten Public Static voidMain (string[] args) { One //arrays that need to be sorted A intArr[] = {49, 20, 36, 51, 18, 94, 61, 31, 50 }; - //loop out the contents of the array -System.out.println ("Before sorting:"); the for(intA:arr) { -System.out.print (A
More search can refer to http://www.cnblogs.com/liuling/p/2013-7-24-01.html this is someone else's resources, feel write very full. You can study it carefully. /* Use binary to find the premise that the data is ordered (ascending). */class Halfsearchdemo {public static void main (string[] args) {int[] arr={1,2,3,6,2,3,5,1,8,9,3,5,2};sop ("keyword location:" + Halfsearch_2 (arr,6)); System.out.println ("Hello world!");} public static int halfsearch_2 (int[] arr,int key) {searchsort (arr); int Max
Java Algorithm interview questions: What are the methods for sorting? List. Use JAVA to implement a fast sorting. Select bubble quick set to sort by at least four methods, java Algorithm
Package com. swift; import java. util. arra
The basic concept of bubble sort (bubblesort) is to compare the number of adjacent two digits, place the decimal number in the front, and put the large numbers behind. On the first trip: first compare the 1th and 2nd numbers, put the decimal number before the big number put. Then compare the 2nd and 3rd numbers, put the decimal places before the large number, so continue until the last two, the number of de
finished.Third, the realization of ideas:With the double loop, the outer loop variable is set to I and the inner loop variable is set to J. If there are n number needs to be ordered, then the outer loop repeats n-1 times, the inner loop repeats n-1,n-2,...,1 times sequentially. Each comparison of the two elements are related to the inner loop j, they can be identified by a[j] and a[j+1], the value of I sequentially,..., n-1, the value of each i,j is 0,1,2,... n-i.Set the array length to N:1. Co
loop, because each bubble we start from the first element, slowly right to the last (here is not the end), a bubble is a number of 22 compare Exchange (11th to 14th line of code) implementation, so our code inside the loop variable j= 0, each time is from 0 subscript elements start bubbling, J change range of J Forget to say a bit, here jThe essence of bubble so
of arr[i], the condition in the inner loop is j>i, because Arr[i] is lined up in front of the order. Each time you compare from backward to arr[i] you don't have to go on.Of course, the core code above can also be written in the following form:1Private Static voidBubbleSortTest2 (intarr[]) { 2inttemp = 0; 3 for(inti = arr.length-1; i > 0; i++) { 4 for(intj = 0; J ) { 5if(Arr[j] ]) { 6 temp = arr[j+1]; 7 Arr[j+1] =Arr[j];8 Arr[j] =temp;9 }10 }11 }12}Here the thought is th
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 bubble sort algorithm works by starting with the fir
reached the maximum value:c=o (n2) m=o (n2)the worst time complexity for bubbling sorting is O (n2) . The overall average time complexity of the bubble sort is O (n2) . 1.2.2. Algorithm stabilityThe bubble sort is to move the small element forward or the large element back. The comparison is an adjacent two element co
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.