javase(eight)
-- arrays and sorting and lookups
First, the definition of the array
three methods of definition :
int b[]=new int[5];
Int []b=new int[5];
Int[] A=new int[5]; ( It is recommended to use this definition method )// The array length must be specified because the memory is allocated at compile time.
We can also initialize an array at the time of definition
Int[] a={1,2,3,3,5};
This defaults to the length of array A is 5.
Allocate memory details as follows:
open up a piece of memory with 5 small pieces,a pointing to the first address of the array.
Int[][] B=new int[5][]; determine at least the number of the first dimension ( that is, the number of array rows ) without prompting for errors. The number of the second dimension can be temporarily not fixed.
Int[][][] B=new int[5][][]; the three-dimensional array length also determines at least the number of the first dimension in order not to suggest errors. can also determine the number of one or two-dimensional,int[][][] b=new int[5][4][]; However, it is not possible to determine single-span dimensions, such as int[][][] b=new int[5][][5];
Second, the array of traversal.
① only the number of dimensions is determined to traverse an array to not error.
② array subscript from 0 start, to n-1 end (n ) int[] s=new int[3] . This array length is 3 s[0],s[1],s[2] is these three values, access s[3] will be an error, the group out of bounds exception java.lang.arrayindexoutofboundsexception
Why is 0 start? With foreigners to build a house habit, because foreigners build houses will build basement, basement is not 0 layer ( Ground floor is also called the floor 0
When ③ is not initialized, all element values ofint array are 0,float,double all element values are 0.0,String All element value is null. Char All elements are a single space ( not nothing ).
④char[] C=new Char[a]; A is a known constant
Traversal mode one ( for loop, control with int variable ) :
for (int i=0;i<a;i++) {
System.out.println (C[i]);
}
Traversal mode two (For Loop ) :
for (char p:c) {
SYSTEM.OUT.PRINTLN (P);
}
Char[][] C=new char[a][b]; A, b is known constant
Traversal mode one ( for loop, control with int variable ) :
for (int i=0;i<a;i++) {
for (int j=0;j<b;j++) {
System.out.println (C[i][j]);
}
}
Traversal mode two (For Loop ) :
for (Char i[]: c) {
for (char j:i) {
System.out.println (j);
}
}
Third, sort
1. Sorting method Type :
internal ordering : All data that needs to be processed is loaded into the internal memory for sorting. ( including commutative sorting method, selective sorting method, insertion sort method )
external sort : The amount of data is too large to load into memory and needs to be sorted with external storage. ( including merge sort method and Direct merge sort method )
The Exchange sort method, after the data comparison, according to the judgment rule to the data position exchange, in order to achieve the order.
① Bubble Sort Method (Bubble sort)
② Fast Sorting Method ( quick Sort)
The selective sorting method is to select an element from the data to be sorted according to the specified rules, and then, after the reorganization with other elements, the order is reached after the principle exchange position.
① Selection Sorting Method ( select sort)
② Heap Sorting Method ( heap sort)
The insertion sort method is the appropriate location for the element to be sorted in an inserted way, in order to achieve the purpose of sorting.
① Insert Sort Method (insertion sort)
② Hill Sort Method (Shell sort)
③ binary Tree Sorting method (Binary-tree sort)
Other sorting methods.
① sorting method of selecting piles
② Combined Sorting method
2. Bubble Sorting method
public static int[] Bubble (int[] array) {
int temp=0;
for (int i=0;i<array.length;i++) {// outer loop, decided to walk a few times
for (int j=0;j<array.length-1-i;j++) {// inner loop, begin to compare one by one and find the previous number larger than the next
if (Array[j]>array[j+1]) {
TEMP=ARRAY[J];
ARRAY[J]=ARRAY[J+1];
Array[j+1]=temp;
ARRAY[J]=ARRAY[J]+ARRAY[J+1];
ARRAY[J+1]=ARRAY[J]-ARRAY[J+1];
ARRAY[J]=ARRAY[J]-ARRAY[J+1];
}
}
}
return array;
}
3. Select the sorting method:
public static int[] Select (int[] array) {
int temp=0;
for (int j=0;j<array.length;j++) {
I think the first number is the smallest one.
int MIN=ARRAY[J];
Subscript for record minimum number
int minindex=j;
for (int k=j+1;k<array.length;k++) {
if (Min>array[k]) {
Modify Minimum
MIN=ARRAY[K];
Minindex=k;
}
}
TEMP=ARRAY[J];
Array[j]=array[minindex];
Array[minindex]=temp;
}
return array;
}
4. Insert Sort Method :
public static int[] Insert (int[] array) {
for (int i=1;i<array.length;i++) {
int insertval=array[i];
Insertval Preparation compared to the previous number
int index=i-1;
while (Index>=0&&insertval<array[index]) {
Move the Array[index] backwards
Array[index+1]=array[index];
let index move forward
index--;
}
Insert the insertval into the appropriate location
Array[index+1]=insertval;
}
return array;
}
5. Quick-Sort method ( Commutative sorting Method ):
public static int[] Quick (int[] array) {
int left=0;
int right=array.length-1;
return px (left, right, array);
}
public static int[] px (int left,int right,int[] array) {
int l=left,r=right;
int privot=array[(L+R)/2];
int temp=0;
while (L<r) {
while (Array[l]<privot) l++;
while (Array[r]>privot) r--;
if (l>=r) break;
TEMP=ARRAY[L];
ARRAY[L]=ARRAY[R];
Array[r]=temp;
if (Array[l]==privot)--r;
if (Array[r]==privot) ++l;
}
if (l==r) {
l++;
r--;
}
if (left<r) px (left,r,array);
if (right>l) PX (l, right, array);
return array;
}
5. comparison of the running times of several sequencing methods:
randomly generates a number of 100,000 1~10000 and puts it into the array.
Int[] Array=new int[100000];
for (int i=0;i<array.length;i++) {
array[i]= (int) (Math.random () *10000);
}
Print the system time before sorting, and print the system time after sorting.
Calendar calendar=calendar.getinstance ();
System.out.println (" before sorting :" +calendar.gettime ());
Bubble (array);
Calendar=calendar.getinstance ();
System.out.println (" after sorting :" +calendar.gettime ());
Discovery sort time is about a second
Change to select Sort method:
Calendar calendar=calendar.getinstance ();
System.out.println (" before sorting :" +calendar.gettime ());
Select (array);
Calendar=calendar.getinstance ();
System.out.println (" after sorting :" +calendar.gettime ());
about 4 seconds .
Change to insert Sort method:
Calendar calendar=calendar.getinstance ();
System.out.println (" before sorting :" +calendar.gettime ());
Insert (array);
Calendar=calendar.getinstance ();
System.out.println (" after sorting :" +calendar.gettime ());
about 3 seconds .
change to the Quick Sort method :
Calendar calendar=calendar.getinstance ();
System.out.println (" before sorting :" +calendar.gettime ());
Quick (array);
Calendar=calendar.getinstance ();
System.out.println (" after sorting :" +calendar.gettime ());
Almost no sorting time.
6. Quick sorting method with shallow measurement
Just tested 100,000 numbers found almost no sorting time.
I tested 1 million again and found that there was no sorting time.
until 10 million were tested :
only 1s of sorting time is found.
Is that the fast sort method that is the best?
No, the fast sorting method runs at a very large CPU and memory footprint, and other sorting methods do not occupy as much as fast sorting.
Four, find
There are two types of lookups we use in Java
① Sequential Lookup ( easiest , least efficient way to find one )
② binary Search ( First Order, then select to find, high efficiency )
public static void Find (int val,int[] array) {
int leftindex=0;
int rightindex=array.length-1;
F (Leftindex, Rightindex, Val, array);
}
public static void f (int leftindex,int rightindex,int val,int array[]) {
int midindex= (RIGHTINDEX+LEFTINDEX)/2;
int Midval=array[midindex];
if (Rightindex>=leftindex) {
if (midval>val) {
F (Leftindex, MidIndex-1, Val, array);
}else if (midval<val) {
F (midindex+1, Rightindex, Val, array);
}else if (midval==val) {
System.out.println (" find this number, subscript for " +midindex);
}
}
}
binary Lookup Requirements array is already sorted out ( from small to large ) array, so we usually sort the first and then use the binary lookup.
(eight) arrays and sorting and finding