Skip to content

Instantly share code, notes, and snippets.

@aturan23
Created November 24, 2017 06:46
Show Gist options
  • Save aturan23/660a8f710763f6bd6a76717f34b7d95e to your computer and use it in GitHub Desktop.
Save aturan23/660a8f710763f6bd6a76717f34b7d95e to your computer and use it in GitHub Desktop.
Memory usage of sorts
package lect;
public class MemoryUsage {
private static final long MEGABYTE = 1024L * 1024L;
private static final long KILOBYTE = 1024L;
public static long bytesToKilobytes(long bytes) {
return bytes / KILOBYTE;
}
public static long bytesToMegabytes(long bytes) {
return bytes / MEGABYTE;
}
public static void main(String[] args) {
MyLinkedList<Integer> list = new MyLinkedList<Integer>();
for (int i = 0; i < 10000; i++) {
list.add((int)Math.random()*100);
}
selectionSort(list);
Runtime runtime = Runtime.getRuntime();
runtime.gc();
long memory = runtime.totalMemory() - runtime.freeMemory();
System.out.println("---My Linked List Selection Sort---");
System.out.println("Used memory is bytes: " + memory);
System.out.println("Used memory is megabytes: " + bytesToMegabytes(memory));
System.out.println("Used memory is kilobytes: " + bytesToKilobytes(memory));
MyArrayList<Integer> arraylist = new MyArrayList<Integer>();
for (int i = 0; i < 1000; i++) {
arraylist.add((int)Math.random()*100);
}
selectionSort(arraylist);
runtime = Runtime.getRuntime();
runtime.gc();
memory = runtime.totalMemory() - runtime.freeMemory();
System.out.println("---My Array List Selection Sort---");
System.out.println("Used memory is bytes: " + memory);
System.out.println("Used memory is megabytes: " + bytesToMegabytes(memory));
System.out.println("Used memory is kilobytes: " + bytesToKilobytes(memory));
MyLinkedList<Integer> arr = new MyLinkedList<Integer>();
for (int i = 0; i < 1000; i++) {
arr.add((int)Math.random()*100);
}
insertSort(arr);
runtime = Runtime.getRuntime();
runtime.gc();
memory = runtime.totalMemory() - runtime.freeMemory();
System.out.println("---My Linked List Insertion Sort---");
System.out.println("Used memory is bytes: " + memory);
System.out.println("Used memory is megabytes: " + bytesToMegabytes(memory));
System.out.println("Used memory is kilobytes: " + bytesToKilobytes(memory));
MyArrayList<Integer> array = new MyArrayList<Integer>();
for (int i = 0; i < 1000; i++) {
array.add((int)Math.random()*100);
}
insertSorta(array);
runtime = Runtime.getRuntime();
runtime.gc();
memory = runtime.totalMemory() - runtime.freeMemory();
System.out.println("---My Array List Insertion Sort---");
System.out.println("Used memory is bytes: " + memory);
System.out.println("Used memory is megabytes: " + bytesToMegabytes(memory));
System.out.println("Used memory is kilobytes: " + bytesToKilobytes(memory));
}
public static void selectionSort(MyAbstractList<Integer> list) {
int n = list.size;
Integer temp;
for (int index = 0; index < n; index++) {
int min = index;
for (int j = index+1; j < n; j++) {
if (list.get(j) < list.get(min)) {
min = j;
}
}
temp = list.get(index);
list.set(index, list.get(min));
list.set(min, temp);
}
}
public static void insertSort(MyLinkedList<Integer> list) {
for(int i = 0; i <= list.size(); i++) {
for(int lastIndex = i-1; lastIndex > 0; lastIndex--){
if(list.get(lastIndex) < list.get(lastIndex-1)) {
int value = list.get(lastIndex);
list.set(lastIndex, list.get(lastIndex-1));
list.set(lastIndex-1, value);
} else
break;
}
}
}
public static void insertSorta(MyArrayList<Integer> list) {
for(int index = 0; index <= list.size(); index++) {
for(int lastIndex = index-1; lastIndex > 0; lastIndex--){
if(list.get(lastIndex) < list.get(lastIndex-1)) {
int value = list.get(lastIndex);
list.set(lastIndex, list.get(lastIndex-1));
list.set(lastIndex-1, value);
} else
break;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment