- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用采用 float 的测试类来实现快速排序的版本。当我尝试生成大小为 10⁸ 的数组时,我在运行测试类时出现堆栈溢出。
我尝试使用 10⁷ 的数组大小,效果很好
在我的测试类中,我生成了两个完全相同的数组,一个是用我的算法排序的,一个是用javas Arrays.sort()
排序的。 .
这是我的测试类的样子。
package Quicksort;
import org.junit.Before;
import org.junit.Test;
import java.util.Arrays;
import static org.junit.Assert.*;
public class QuickSortTest {
private static float[] quickSort, javaSort;
private final static int SIZE = (int) 1e7;
@Before
public void init(){
System.gc();
quickSort = new float[SIZE];
javaSort = new float[SIZE];
for(int i = 0; i < SIZE; i++){
int temp = (int) (Math.random() * 1000) + 1;
quickSort[i] = temp;
}
javaSort = quickSort;
}
@Test
public void testQuickSort(){
QuickSort.sort(quickSort);
Arrays.sort(javaSort, 0, SIZE);
assertArrayEquals(quickSort, javaSort, .0f);
}
}
快速排序实现:
private static void quickSort(float[] table, int first, int last){
if(first < last){
int pivotIndex = partition(table, first, last);
quickSort(table, first, pivotIndex - 1);
quickSort(table, pivotIndex + 1, last);
}
}
public static int partition(float[] table, int first, int last){
sort3(table, first, last);
swap(table, first, (first + last) / 2);
float pivot = table[first];
int up = first;
int down = last;
do{
while((up < last) && table[up] <= pivot){
up++;
}
while(table[down] > pivot){
down--;
}
if(up < down){
swap(table, up, down);
}
}while(up < down);
swap(table, first, down);
return down;
}
最佳答案
StackOverflowError
通常是由错误的递归调用引起的。您的 QuickSort 类有一个递归函数,当您传入长度为 10^8 的数组时,该函数会在超出堆栈大小时不断调用自身。
解决此问题的一种方法是将您的实现切换为迭代方法而不是递归方法。
根据您的上次更新,似乎 partition()
方法递归地调用自身,超出了 Java 堆空间的限制。
在此post ,您可以找到迭代 partition()
实现。它稍微复杂一些,但能够处理数组的大小。
import java.util.Arrays;
import java.util.Random;
// Java program for implementation of QuickSort
class QuickSort
{
public static void main(String[] args) {
QuickSort sort=new QuickSort();
int[] randomArray = createRandomArray((int) Math.pow(2, 20));
sort.qSort(randomArray);
//System.out.println(Arrays.toString(randomArray));
}
private void qSort(int[] arr) {
this.qSort(arr, 0, arr.length-1);
}
/* This function takes last element as pivot,
places the pivot element at its correct
position in sorted array, and places all
smaller (smaller than pivot) to left of
pivot and all greater elements to right
of pivot */
int partition(int arr[], int low, int high)
{
int pivot = arr[high];
int i = (low-1); // index of smaller element
for (int j=low; j<=high-1; j++)
{
// If current element is smaller than or
// equal to pivot
if (arr[j] <= pivot)
{
i++;
// swap arr[i] and arr[j]
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
// swap arr[i+1] and arr[high] (or pivot)
int temp = arr[i+1];
arr[i+1] = arr[high];
arr[high] = temp;
return i+1;
}
/* The main function that implements QuickSort()
arr[] --> Array to be sorted,
low --> Starting index,
high --> Ending index */
void qSort(int arr[], int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[pi] is
now at right place */
int pi = partition(arr, low, high);
// Recursively sort elements before
// partition and after partition
qSort(arr, low, pi-1);
qSort(arr, pi+1, high);
}
}
private static int[] createRandomArray(int size){
Random randNumGenerator = new Random();
int[] arr = new int[size];
for (int i=0; i<arr.length; i++)
{
arr[i] = (randNumGenerator.nextInt(100)+1);
}
return arr;
}
}
您似乎想牢记以下内容;
关于java - 当数组变得太大时 Stackoverflow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48423647/
我在 android 代码中使用 asmack XMPP。我可以正常登录 XMPP 服务器,但是当我尝试创建新用户时出现问题。我想要实现的是: 以管理员身份登录。 创建一个新用户。 从管理员注销。 以
这是我的标记页面,其中有一个按钮可以从数据库中搜索数据并显示在网格中 这是我背后的代码 if (!IsPostBack) { LblInfo.Text = "Page Load
当我多次将相同的 float 值插入到我的集合中时,本应花费恒定时间的 x in s 检查变得非常慢。为什么? 时序x in s的输出: 0.06 microseconds 0.09 mi
我有一个小型聊天客户端,可以将所有历史记录存储在 sqlite 数据库中。当用户单击我的应用程序中的 history 选项卡时,我的应用程序会获取所有相关历史记录并将其显示在 QWebView 中。我
我是一名优秀的程序员,十分优秀!