Algorithmic problems in Android

Source: Internet
Author: User

Tag:print   nbsp   public    using    roi    tom    Cycle control     case    androi   

1: Bubbling sort: the idea of bubbling sort: The next two numbers are compared (1): First you need to connect A for loop (2): the outermost for loop controls the loop number of the innermost for Loop (3): Exchange by setting the intermediate parameters (4): The code is as follows://bubble sort Public void Bubblesort () {//Declaration array int a[] = {49, 38, 65, 97, 76, 13, 27, 49, 78, 34, 12, 64, 5, 4, 62, 99, 98, 54, 56, 17, 18, 23, 51};int temp = 0;//outermost is not used to control how many times inside the next need to compare for (int i = 0; i < a.length-1; i++) {//bubble sort, put the largest number to the back F or (int j = 0; J < a.length-1-I; j + +) {//if (A[j] > a[j + 1]) {temp = A[j];a[j] = a[j + 1];a[j + 1] = temp;}}} for (int i = 0; i < a.length; i++) System.out.println (A[i]);} Two numbers Direct interaction: Note: Exchange two numbers without introducing other variables, use the sum of two numbers to do/a = a+b; A save two number and//b = a A; Two numbers and-B, which is a//a = a-A; Two numbers and-B, at this point, B. has become a, so the equivalent of sum-a=b//System.out.println ("swapbyself first function:a=" +a+ ", b=" +b);///There is another way Use the difference of two numbers, that is, the distance between two//a = b-a; A= the difference between the two/b = b-a; b = The original two-digit distance = = The original a//a = a+b; The difference between the final a= and the original a== B 2:Quick Sort: (concrete implementation ideas are as follows) Select a Datum element , Usually select the first element or the last element , Through a scan, the waiting sequence is divided into two parts , part smaller than the benchmark element , part is greater than or equal to the Datum element , at this point the datum element is in the correct position after its order. Span style= "font-family:arial" >, public static void _choicesort (integer[] a) {if (a = = NULL | | a.length <= 0) {return;} for (int i = 0; i < a.length; i++) {int min = i;/* Defines the current subscript as the lowest value subscript */for (int j = i + 1; j < A.length; J + +) {if (a[m In] > A[j]) {/* If there is a keyword less than the current minimum value */min = j;/* Assigns the subscript of this keyword to min */}}if (i! = min) {/* if min is not equal to I, the description finds the minimum value, exchange */int TMP = A[mi N];a[min] = a[i];a[i] = tmp;}}} 3: Dichotomy: (mainly to find a specified number)This method is appropriate when the amount of data is large. when searching by binary method, the data need to be ordered and not duplicated. Basic idea: Assume that the data is sorted in ascending order, for a given value x, from the middle of the sequence of the comparison, if the current position value equals X, then the search succeeds; if x is less than the current position value, the first half of the sequence is searched, and if x is greater than the current position, the second half of the series is searched until Public ClassDichotomysearch {Public Static voidMain (string[] args) {Int[] arr =New Int[] {12, 23, 34, 45, 56, 67, 77, 89, 90}; System.out.println (Search (arr, 12)); System.out.println (Search (arr, 45)); System.out.println (Search (arr, 67)); System.out.println (Search (arr, 89)); System.out.println (Search (arr, 99)); }Public Static IntSearchInt[] arr,IntKey) {Intstart = 0;Intend = Arr.length-1;While(Start <= end) {IntMiddle = (start + end)/2;ifelse ifelse {return Middle; }} return-1; } }

Algorithmic problems in Android

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.