Write in front: This essay mainly records the situation of virtual machine stack overflow caused by recursive call and configures the virtual machine stack size by parameters so that recursive calls can be executed smoothly. There is no detailed explanation of some of the concepts involved (because I am not particularly clear about these concepts myself at the moment) and can be used for reference keywords:
Keywords: Java Virtual machine stack, stack overflow, stack frame
Today, when comparing quick and bubbling sorting, the simplest quick sort and bubble sort (from small to large) are implemented by code, with the following code:
1 /**2 * Bubble Sort3 */4 5 Public classBubblesortdemo {6 Public Static voidMain (string[] args) {7 int[] Nums =New int[100000];8 for(inti = nums.length-1, j = 0; I >= 0; I--, J + +) {9NUMS[J] =i;Ten } One LongStart =System.currenttimemillis (); A Bubblesort (nums); - LongEnd =System.currenttimemillis (); -System.out.println ((End-start) + "MS"); the } - - Public Static voidBubblesort (int[] nums) { - intSwapcount =-1; + while(Swapcount! = 0) { -Swapcount = 0; + for(inti = 0; i < nums.length-1; i++) { A intIItem =Nums[i]; at intJitem = nums[i + 1]; - if(IItem >Jitem) { -Swap (Nums, I, i + 1); -swapcount++; - } - } in } - } to + /** - * Swap array nums[] Two elements in the position I and J the */ * Public Static voidSwapint[] Nums,intIintj) { $ inttemp =Nums[i];Panax NotoginsengNums[i] =Nums[j]; -NUMS[J] =temp; the } +}
1 /**2 * Quick Sort3 */4 5 Public classQuicksortdemo {6 Public Static voidQuickSort (int[] Nums,intLeftindex,intRightindex) {7 if(nums.length <= 1 | | Leftindex >=Rightindex) {8 return;9 }Ten One intPivot =Nums[leftindex]; A intleft =Leftindex; - intright =Rightindex; - the while(Left <Right ) { - while(Left < right && Nums[right] >=pivot) { -Right--; - } + if(Left <Right ) { -Nums[left] =Nums[right]; +Left + +; A } at while(Left < right && Nums[left] <=pivot) { -Left + +; - } - if(Left <Right ) { -Nums[right] =Nums[left]; -Right--; in } - } toNums[left] =pivot; +QuickSort (Nums, Leftindex, left-1); -QuickSort (Nums, left + 1, Rightindex); the } * $ Public Static voidQuickSort (int[] nums) {Panax Notoginseng if(Nums! =NULL) { -QuickSort (nums, 0, Nums.length-1); the } + } A the Public Static voidMain (string[] args) { + int[] Nums =New int[100000]; - for(inti = nums.length-1, j = 0; I >= 0; I--, J + +) { $NUMS[J] =i; $ } - LongStart =System.currenttimemillis (); - QuickSort (nums); the LongEnd =System.currenttimemillis (); -System.out.println ((End-start) + "MS");Wuyi } the -}
In their main function, I generated an ordered array of length=100000 from large to small in order to test two sorting algorithms and observe their execution time.
For bubble sorting, it takes 12833ms to sort 100,000 ordered arrays.
For fast sorting, the ordering of 100,000 ordered arrays, because the fast sort takes the recursive implementation, the program throws the Stackoverflowerror exception, which was supposed to be the cause of the death cycle, and then after the investigation, in fact, because the virtual machine stack Overflow, The function call is too deep. By setting the virtual machine parameter-xss10m, the virtual machine stack is allocated 10M of memory, so that the algorithm can execute normally. Finally, the quick sort took 4830ms.
Stackoverflowerror exceptions caused by fast Java ordering