[02] Java basis and sorting an array

LInk: [01] The basic structure of a Java program, expressions, statements and operators -> https://www.cnblogs.com/discoverspace/p/11509577.html

 

 1. Array concept

1.1 array with a subscript to indicate the identifier and the subscript in the array can distinguish between different elements. If an array is only one index, called the one-dimensional array. There are two subscripts, compared with two-dimensional arrays. Generally uses a three-dimensional array, one-dimensional and two-dimensional arrays more commonly used.

Array is a reference type, in memory array element and array variables are stored separately, wherein the reference variable is stored in a memory stack (Stack), the array elements is in the heap (heap) in .

An array is a basic data structure for sequentially storing a set of values of the same type.

1.2 to create and initialize an array

Name and type declaration of the array, create an array, initialize the array elements. Complete wording slightly.

Simplify the wording:

1 int a[]=new int [100];

 

1.3 Array alias

Array name represents the entire array, if the variable is given an array of other variables, the two variables will point to the same array.

1 int a[]=new int[100];
2 a[1]=102;
3 int b[]=a;
4 b[1]=2;

 

At this time, the value of a [1] also becomes 2.

2. Sort the array

2.1 bubble sort: a set of records for a set of data contains n, the worst case should be the n-1 order.

 Every sort will put out on the last fixed, the number of fixed will no longer participate in the next sort the greatest number.

 1 import java.util.Scanner;
 2 public class 冒泡排序 {
 3     public static void bubbleSort(int []data,int len)
 4     {
 5         for(int i=0;i<len-1;i++)
 6         {
 7             boolean flag=false;
 8             for(int j=0;j<len-1-i;j++)
 9             {
10                 if(data[j]>data[j+1])
11                 {
12                     int TEMP = Data [J];
 13 is                      Data [J] = Data [J +. 1 ];
 14                      Data [J +. 1] = TEMP;
 15                      In Flag = to true ;
 16                  }
 . 17              }
 18 is              IF (! In Flag) // not occurred exchange, it has been ordered 
. 19                  BREAK ;
 20 is          }
 21 is      }
 22 is      public  static  void main (String [] args) {
 23 is          Scanner CIN = new new Scanner (the System.in);
 24          the while (cin.hasNext()) {
25             int n = cin.nextInt();
26             int a[] = new int[n];
27             for (int i = 0; i < n; i++) {
28                 a[i] = cin.nextInt();
29             }
30             bubbleSort(a,n);
31             for(int i:a){ //for-each循环
32                 System.out.println(i);
33             }
34         }
35     }
36 }

 

 

 2.2 quicksort ( important! ) : Dichotomous thinking, take a data (such as the first) as the cutoff value in a set of data, a small number of all score cutoffs into the left large cutoff score on the right to obtain two sequences, two sequences are then sorted, up until about only a few.

Complete code:

Import java.util.Scanner;
 public  class quicksort {
     public  static  void QUICKSORT ( int [] Data, int Start, int End) 
    { 
        int I = Start; int J = End;
         IF (I> = J) // exit condition 
            return ;
         Boolean In Flag = to true ; // to true indicates searches from right to left, from left to right to false indicates a search 
        the while (I = J!) // until stop met, means that an exchange is completed 
        {
             IF (Data [I] > Data [J]) 
            {
                int the TEMP = the Data [i]; 
                the Data [i] = the Data [J]; 
                the Data [J] = the TEMP; 
                Flag = Flag;! // have exchanged the change flag, who once ij change change under this decision (to which the search direction to go) 
            }
             IF (In Flag) 
                J - ;
             the else 
                I ++ ; 
        } 
        I -; J ++; // recursive, s division, each of the left and right sides ij repeated once 
        QUICKSORT (Data, Start, I); 
        QUICKSORT ( Data, J, End); 
    } 
    public  static  void main (String [] args) { 
        Scanner CIN =new Scanner(System.in);
        while (cin.hasNext()) {
            int n = cin.nextInt();
            int a[] = new int[n];
            for (int i = 0; i < n; i++) {
                a[i] = cin.nextInt();
            }
            quickSort(a,0,n-1);
            for(int i:a){ //for-each循环
                System.out.println(i);
            }
        }
    }
}

 

Guess you like

Origin www.cnblogs.com/discoverspace/p/11518069.html