Java-using binary trees for fast sorting-zhudi tornado, java-Binary Tree
(-1) Preface
I was asked about quick sorting during an interview, and I went back to read a previous case. I am ashamed to say that it takes a long time to understand what my code means, the main reason is the lack of comments and illustrations. I have deep feelings and decided to record them.
The binary tree is used because the stack overflow error is reported when the data volume is too large using recursion. I tried someone else's computer. Of course, using a binary tree will also report insufficient memory because it is impossible to create such a long array and the heap memory overflow. I personally feel much better than recursion.
(0) algorithm details
The program randomly generates data and places it in an array.
A. Consider the data between the minimum index and the maximum index as A whole. The program has the minimum index represented by.
B. Change the position of A so that the left side of A is A number larger than that of A, and the right side of A is A number smaller than that of A. At this time, the index of A is called an intermediate index.
C. The minimum index to the middle index-1 is regarded as the left child, and the middle index + 1 to the maximum index is regarded as the right child.
The following figure shows the procedure:
Each node represents an array segment, and 1 is the root node, representing the entire array. Each segment must undergo operations a and B.
Operation 1, operation 2, operation 2, operation 4, operation 2, operation 2, operation 2, operation 5, operation 2, operation 1, Operation 3, creation 6, operation 6, return to 3, return to 1, and finish.
The following array is used as an example to describe the program process.
Int [] oop ={ 510,107,948,659,955,438,283,822 };
First time
Step a: Minimum index 0, maximum index 7 A-> 510
Step B [822,955,948,659,510,438,283,107]
Step c left child 0-3 right child 5-7
Second
Step a: Minimum index 0 and maximum index 3 A-> 822
Step B [948,955,822,659]
Step c left child 0-1 right child no
Third time
Step a: The minimum index is 0, and the maximum index is 1 A-> 948.
Step B [955,948]
Step c: no for the left and no for the right
Fourth
Step a: Minimum index 5 and maximum index 7 A-> 438
Step B [438,283,107]
Step c: No left child, 6-7 Right child
Fifth
Step a: Minimum index 6 and maximum index 7 A-> 283
Step B [283,107]
Step c: no for the left and no for the right
(2) Implementation
Class Test
{
Public static void main (String [] args)
{
Int len = 8000000;
Int [] oop = new int [len];
For (int I = 0; I <len; I ++)
Oop [I] = (int) (Math. random () * 1000 );
Calendar c1 = Calendar. getInstance ();
Sort. quick_sort (oop );
Calendar c2 = Calendar. getInstance ();
System. out. println (c2.getTimeInMillis ()-c1.getTimeInMillis ());
}
}
Class Binary
{
Private int left, // minimum Index
Right; // maximum Index
Private Binary beforeBinary, // parent node
RightBinary, // left child
LeftBinary; // right child
Public Binary (int left, int right)
{
This. left = left;
This. right = right;
}
Public void setLeft (int left)
{
This. left = left;
}
Public void setRight (int right)
{
This. right = right;
}
Public void setBefore (Binary beforeBinary)
{
This. beforeBinary = beforeBinary;
}
Public void setRightBinary (Binary rightBinary)
{
This. rightBinary = rightBinary;
}
Public void setLeftBinary (Binary leftBinary)
{
This. leftBinary = leftBinary;
}
Public int getLeft ()
{
Return this. left;
}
Public int getRight ()
{
Return this. right;
}
Public Binary getBeforeBinary ()
{
Return this. beforeBinary;
}
Public Binary getRightBinary ()
{
Return this. rightBinary;
}
Public Binary getLeftBinary ()
{
Return this. leftBinary;
}
}
Class Sort
{
Public static void quick_sort (int [] oop)
{
Binary headBinary = new Binary (0, oop. length-1 ),
TempBinary = headBinary;
Int right,
Left,
TempNumber;
Boolean flag = true;
HeadBinary. setBefore (null );
Do
{
Left = tempBinary. getLeft ();
Right = tempBinary. getRight ();
TempNumber = oop [left];
While (left <right) // after the end of the loop, the left side of tempNumber is larger than him, and the right side of tempNumber is smaller than him.
{
While (left <right & tempNumber> = oop [right]) // find a number larger than tempNumber on the right
Right --;
If (left <right)
{
Oop [left] = oop [right]; // assign the number on the right to
Left ++; // reduce the range
}
While (left <right & tempNumber <= oop [left]) // find a number smaller than tempNumber from the left
Left ++;
If (left <right)
{
Oop [right] = oop [left]; // assign the number on the left to the right
Right --; // reduce the range
}
}
// Left = right
Oop [left] = tempNumber;
If (right <tempBinary. getRight ()-1) // create the left child
{
Binary c1 = new Binary (right + 1, tempBinary. getRight ());
TempBinary. setRightBinary (c1 );
C1.setBefore (tempBinary );
}
If (left-1> tempBinary. getLeft () // create the right child
{
Binary c1 = new Binary (tempBinary. getLeft (), left-1 );
TempBinary. setLeftBinary (c1 );
C1.setBefore (tempBinary );
}
Flag = true;
Do // operation A: (Traverse left and right children, if left and right children are traversed, return to the last node) repeat operation A until it is traversed to the following Node
{
If (tempBinary. getLeftBinary ()! = Null) // If the left child is created, traverse the left child first
{
Binary c1 = tempBinary. getLeftBinary ();;
TempBinary. setLeftBinary (null); // Most importantly, as long as the left child to be traversed is set to null,
TempBinary = c1;
Flag = false;
}
Else if (tempBinary. getRightBinary ()! = Null) // The right child is always traversed only after the Left brother node is traversed.
{
Binary c1 = tempBinary. getRightBinary ();
TempBinary. setRightBinary (null );
TempBinary = c1;
Flag = false;
}
Else // left child. The right child is traversed and returned to the parent node.
{
If (tempBinary = headBinary) break;
TempBinary = tempBinary. getBeforeBinary ();
}
} While (flag );
} While (tempBinary! = HeadBinary); // It indicates that the sorting is complete when you trace back to the root node.
}
}
(3) Simple Test
80000000 memory overflow
8000000 66607 ms
800000 1027 ms