java--type conversion, bubble sort, select sort, binary lookup, array rollover

Source: Internet
Author: User

one, type conversion

American Standard Code for information interchange , the American Standard Information Interchange Code .

In a computer, all data is stored and computed using a binary number representation, a、b、C、DSuch a theLetters (including uppercase), and0、1There are some common symbols for numbers., In the computer is also used to store the binary number to represent, and the specific use of which binary numbers to indicate which symbol, of course, everyone can contract their own set (this is called coding), and if you want to communicate with each other without causing confusion, then everyone must use the same coding rules, So the United States-related standardization organizations have introducedASCIICode, which specifies which binary numbers are used to represent the above-mentioned symbols.

1. Conversions of type int and char type

Char two bytes, int four bytes

When Char is converted to an int type, the type is automatically promoted, the char data type is queried, the encoded table is obtained, an integer is cast, and an int is converted to a char type.

Char Queries Unicode 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) j; System.out.println (h);  System.out.println ((char) 6); Use (char) numbers to convert int to char type char k = ' you '; System.out.println (k); char m =-1;}}

second, sorting algorithm1. Bubble sort

Principle: Adjacent element comparison, exchange position

Code implementation:

public class Bubblesort {public    static void Main (string[] args) {        int arr[] = {11,2,88,6,5};        Sort (arr);        Xunhuan (arr);    }    public static void sort (int[] arr) {for        (int. i=0;i<arr.length-1;i++) {for            (int j=0;j<arr.length-i-1;j++) {                if (arr[j]>arr[j+1]) {                    int temp = arr[j];                    ARR[J] = arr[j+1];                    ARR[J+1] = temp;    }}}} public static void Xunhuan (int[] arr) {for        (int i=0;i<arr.length;i++) {            System.out.println (arr[i]);        }    }}
2. Select Sort

Principle: Compare between each element, swap position

Code implementation:

public class Selectsort {public    static void Main (string[] args) {        int arr[] = {11,2,88,6,5};        Sort (arr);        Xunhuan (arr);    }    public static void sort (int[] arr) {for        (int. i=0;i<arr.length-1;i++) {for            (int j=i+1;j<arr.length;j++) {                if (Arr[i]>arr[j]) {                    int temp = arr[i];                    Arr[i] = arr[j];                    ARR[J] = temp;    }}}} public static void Xunhuan (int[] arr) {for        (int i=0;i<arr.length;i++) {            System.out.println (arr[i]);        }    }}
three or two points find
public class Binsearch {public    static void Main (string[] args) {        int[] arr = {1,2,3,4,5,6,7,8,9,10};        Search (arr,5);    }    public static int Search (int[] arr,int val) {    //void (no type), commonly used in methods with no return value        int low = 0;        int high = arr.length-1;        int mid = 0;        while (Low
Four, the inverted array
public class Reversearray {public    static void Main (string[] args) {        int[] arr = {1,2,3,4,5};        Reverse (arr);        Xunhuan (arr);    }    public static void reverse (int[] arr) {for        (int min=0,max=arr.length-1;max>min;max--, min++) {            int temp = arr[ MIN];            Arr[min] = Arr[max];            Arr[max] = temp;        }    }    public static void Xunhuan (int[] arr) {for        (int i=0;i<arr.length;i++) {            System.out.println (arr[i]);        }    }}

java--type conversion, bubble sort, select sort, binary lookup, array rollover

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.