Java (ii)

Source: Internet
Author: User

A two-dimensional array is an array of one-dimensional arrays
Select Sort principle
Compares each element in the array to the first element, and if the element is less than the first element, the two elements are exchanged
Loop the first rule, find the smallest element, put it in the first position
Finish sorting by n-1 wheel comparison

Bubble Sort Principle
Compares the two adjacent elements in the array one after the other, and exchanges the order of the elements if they are smaller than the preceding elements.
After a round of comparisons, there must be one of the biggest rows in the final position.
Each time the remaining elements are compared, the sorting is done by n-1 wheel comparison

Insert Sort principle
Divides the array into two parts, comparing the first one of the latter with each of the previous parts, and moving the element if the current element is small. Find a reasonable place to insert

1. Concept: A two-dimensional array is an array of one-dimensional arrays


2. Syntax: A.int [] [] arr=new int [2][];
Defines the length of a two-dimensional array, but one-dimensional array length is undefined and one-dimensional array does not request a memory space null
B.int [] [] arr=new int [2][3];
Defines the length of one-dimensional and two-dimensional arrays, one-dimensional arrays are allocated memory space, the length of one-dimensional arrays is 3, and the default value for one-dimensional arrays is 0;
C. Two-d arrays also support static initialization
String [] []arr={{0,1,5,},{6,5,2}}

Array Classic Sort

1. Select the sorting principle
A. Compares each element in the array to the first element, and if the element is less than the first element, the position of the two elements is exchanged
B. Loop the first rule, find the smallest element and prevent it from the first position
C. After N-1 round comparison complete sorting


Package Paixu;

/**
* Select sort
*/
Import Java.util.Scanner;

public class Zy2 {
public static void Main (string[] args) {
Scanner input = new Scanner (system.in);
System.out.print ("Please enter the number of digits of the input number:");
int c = Input.nextint ();
Int[] A = new int[c];
for (int i = 0; i < a.length; i++) {
System.out.print ("Please input" + (i + 1) + "bit number:");
A[i] = Input.nextint ();
}
int temp = 0;
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < A.length; J + +) {
if (A[i] > A[j]) {
temp = A[i];
A[i] = A[j];
A[J] = temp;
}
}
}
for (int i = 0; i < a.length; i++)
System.out.print (A[i] + "");
}
}


2. Bubble Sorting principle
A. Compare each of the two adjacent elements in the array, if the subsequent elements are smaller than the preceding elements
B. After a round of comparisons, there must be one of the largest in the final position
C. Each time the remaining elements are compared, the sorting can be achieved by N-1 comparison

1 package Paixu;
2
3/**
4 * Bubble sort
5 */
6
7 public class Zy1 {
8 public static void Main (string[] args) {
9 int[] Sz = {8, 6, 5, 4, 3, 1, 2};
int bl = 0;
one for (int i = 0; i < sz.length-1; i++) {
(int j = 0; J < sz.length-1-I; j + +) {
if (Sz[j] > sz[j + 1]) {
BL = sz[j];
SZ[J] = sz[j + 1];
Sz[j + 1] = BL;
17}
18}
19}
(int i = 0; i < sz.length; i++) {
System.out.print (Sz[i] + "\ t");
22}
23}
24
25}

3. Insertion sorting principle
A. Divide the array into two parts, comparing the first one of the latter part to each of the previous ones, and if the current element is small, move the element being compared
B. Find a reasonable place to insert

1 package Paixu;
2
3/**
4 * Insert Sort
5 */
6 public class Zy3 {
7 public static void Main (string[] args) {
8 int[] Sz = {1, 9, 5, 2, 7, 4};
9 for (int i = 1; i < sz.length; i++) {
temp = Sz[i];
Int J;
(j = i-1; J >= 0; j--) {
if (temp < sz[j]) {
Sz[j + 1] = sz[j];//element moves backwards
-} else {
break;
17}
18}
Sz[j + 1] = temp;//Find the appropriate bit value insert element
20}
for (int i = 0; i < sz.length; i++) {
System.out.print (Sz[i] + "");
23}
24}
25}

Java (ii)

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.