所以,我必须比较Selection Sort和Insertion Sort的效率。我为此做的大多数搜索都给出了与时间相关的答案。我的问题涉及两者之间的记忆复杂性。具体哪个使用更多内存。我的代码没有问题。我只是不明白结果。
import java.io.*;
import java.util.*;
public class ShresthaHW2Q2 {
public static void main(String[] args) throws IOException
{
ArrayList <Integer> numbersTenInsertion = new ArrayList <Integer>();
Scanner inputOne = new Scanner(new File("numbers10.txt"));
while (inputOne.hasNextInt())
{
numbersTenInsertion.add(new Integer(inputOne.nextInt()));
}
ArrayList <Integer> numbersTenSelection = new ArrayList <Integer>();
Scanner inputTwo = new Scanner(new File("numbers10.txt"));
while (inputTwo.hasNextInt())
{
numbersTenSelection.add(new Integer(inputTwo.nextInt()));
}
ArrayList <Integer> numbersThirtyInsertion = new ArrayList <Integer>();
Scanner inputThree = new Scanner(new File("numbers30.txt"));
while (inputThree.hasNextInt())
{
numbersThirtyInsertion.add(new Integer(inputThree.nextInt()));
}
ArrayList <Integer> numbersThirtySelection = new ArrayList <Integer>();
Scanner inputFour = new Scanner(new File("numbers30.txt"));
while (inputFour.hasNextInt())
{
numbersThirtySelection.add(new Integer(inputFour.nextInt()));
}
ArrayList <Integer> numbersFiftyInsertion = new ArrayList <Integer>();
Scanner inputFive = new Scanner(new File("numbers50.txt"));
while (inputFive.hasNextInt())
{
numbersFiftyInsertion.add(new Integer(inputFive.nextInt()));
}
ArrayList <Integer> numbersFiftySelection = new ArrayList <Integer>();
Scanner inputSix = new Scanner(new File("numbers50.txt"));
while (inputSix.hasNextInt())
{
numbersFiftySelection.add(new Integer(inputSix.nextInt()));
}
Runtime rt = Runtime.getRuntime();
System.gc();
long totalMemory = rt.totalMemory();
long currentMemoryOne;
selectionSortTen(numbersTenSelection);
currentMemoryOne = rt.freeMemory();
System.out.println("Memory used = " + (totalMemory - currentMemoryOne));
System.gc();
long currentMemoryTwo;
insertionSortTen(numbersTenInsertion);
currentMemoryTwo = rt.freeMemory();
System.out.println("Memory used = " + (totalMemory - currentMemoryTwo));
System.gc();
long currentMemoryThree;
selectionSortThirty(numbersThirtySelection);
currentMemoryThree = rt.freeMemory();
System.out.println("Memory used = " + (totalMemory - currentMemoryThree));
System.gc();
long currentMemoryFour;
insertionSortThirty(numbersThirtyInsertion);
currentMemoryFour = rt.freeMemory();
System.out.println("Memory used = " + (totalMemory - currentMemoryFour));
System.gc();
long currentMemoryFive;
selectionSortFifty(numbersFiftySelection);
currentMemoryFive = rt.freeMemory();
System.out.println("Memory used = " + (totalMemory - currentMemoryFive));
System.gc();
long currentMemorySix;
insertionSortFifty(numbersFiftyInsertion);
currentMemorySix = rt.freeMemory();
System.out.println("Memory used = " + (totalMemory - currentMemorySix));
System.gc();
}
private static void insertionSortTen (ArrayList<Integer> numbersTenInsertion)
{
int valueToSort; // holds the value being sorted at each iteration
int index; // the index
for( int k=1; k<numbersTenInsertion.size(); k++ )
{
valueToSort = numbersTenInsertion.get(k); // create copy of value being inserted
// prevents value from being lost
index = k;
while( index > 0 && numbersTenInsertion.get(index-1) > valueToSort )
{
numbersTenInsertion.set( index, numbersTenInsertion.get(index-1) );
index--;
}
numbersTenInsertion.set(index, valueToSort );
}
//System.out.println(numbersTenInsertion);
}
private static void selectionSortTen (ArrayList<Integer> numbersTenSelection){
int max, i ,j;
int valueToSortTwo;
for (i = 0; i < numbersTenSelection.size()-1; i++)
{
max = i;
for (j = i+1; j < numbersTenSelection.size(); j++)
{
if (numbersTenSelection.get(max).compareTo(numbersTenSelection.get(j)) > 0)
max = j;
}
valueToSortTwo = numbersTenSelection.get(i);
numbersTenSelection.set(i, numbersTenSelection.get(max));
numbersTenSelection.set(max, valueToSortTwo);
}
//System.out.println(numbersTenSelection);
}
private static void insertionSortThirty (ArrayList<Integer> numbersThirtyInsertion)
{
int valueToSort; // holds the value being sorted at each iteration
int index; // the index
for( int k=1; k<numbersThirtyInsertion.size(); k++ )
{
valueToSort = numbersThirtyInsertion.get(k); // create copy of value being inserted
// prevents value from being lost
index = k;
while( index > 0 && numbersThirtyInsertion.get(index-1) > valueToSort )
{
numbersThirtyInsertion.set( index, numbersThirtyInsertion.get(index-1) );
index--;
}
numbersThirtyInsertion.set(index, valueToSort );
}
//System.out.println(numbersThirtyInsertion);
}
private static void selectionSortThirty (ArrayList<Integer> numbersThirtySelection){
int max, i ,j;
int valueToSortTwo;
for (i = 0; i < numbersThirtySelection.size()-1; i++)
{
max = i;
for (j = i+1; j < numbersThirtySelection.size(); j++)
{
if (numbersThirtySelection.get(max).compareTo(numbersThirtySelection.get(j)) > 0)
max = j;
}
valueToSortTwo = numbersThirtySelection.get(i);
numbersThirtySelection.set(i, numbersThirtySelection.get(max));
numbersThirtySelection.set(max, valueToSortTwo);
}
//System.out.println(numbersThirtySelection);
}
private static void insertionSortFifty (ArrayList<Integer> numbersFiftyInsertion)
{
int valueToSort; // holds the value being sorted at each iteration
int index; // the index
for( int k=1; k<numbersFiftyInsertion.size(); k++ )
{
valueToSort = numbersFiftyInsertion.get(k); // create copy of value being inserted
// prevents value from being lost
index = k;
while( index > 0 && numbersFiftyInsertion.get(index-1) > valueToSort )
{
numbersFiftyInsertion.set( index, numbersFiftyInsertion.get(index-1) );
index--;
}
numbersFiftyInsertion.set(index, valueToSort );
}
//System.out.println(numbersFiftyInsertion);
}
private static void selectionSortFifty (ArrayList<Integer> numbersFiftySelection){
int max, i ,j;
int valueToSortTwo;
for (i = 0; i < numbersFiftySelection.size()-1; i++)
{
max = i;
for (j = i+1; j < numbersFiftySelection.size(); j++)
{
if (numbersFiftySelection.get(max).compareTo(numbersFiftySelection.get(j)) > 0)
max = j;
}
valueToSortTwo = numbersFiftySelection.get(i);
numbersFiftySelection.set(i, numbersFiftySelection.get(max));
numbersFiftySelection.set(max, valueToSortTwo);
}
//System.out.println(numbersFiftySelection);
}
}
我得到的结果是:
Memory used = 4906504 (selection sort of arraylist with 10000 integers)
Memory used = 4697664 (insertion sort of arraylist with 10000 integers)
Memory used = 4849512 (selection sort of arraylist with 30000 integers)
Memory used = 3693496 (insertion sort of arraylist with 30000 integers)
Memory used = 3360632 (selection sort of arraylist with 50000 integers)
Memory used = 2276056 (insertion sort of arraylist with 50000 integers)
我不明白,鉴于ArrayList
有更多整数,为什么使用的内存会减少?
答案 0 :(得分:0)
由于垃圾收集,很难测量Java中特定算法使用的内存。如果你调用一个方法并测量前后可用的空间,如果JVM在方法中间开始进行垃圾收集而不是等到方法完成垃圾收集之后,你可能会得到截然不同的数字。因此,这种测量内存的方法可能并不理想。
更直接的方法是查看代码并查看最终使用的内存量。正确实现的插入排序版本应该只需要O(1)辅助内存,并且正确版本的选择排序也应该只需要O(1)辅助内存 - 不涉及递归,不需要辅助数组,并且您需要的唯一信息是几个指数。