adding linked list version of insertion sort
This commit is contained in:
@@ -84,7 +84,7 @@ public class AlgorithmTester{
|
|||||||
int input;
|
int input;
|
||||||
boolean ready=false;
|
boolean ready=false;
|
||||||
boolean print=false;
|
boolean print=false;
|
||||||
String[] sortingAlgoNames = {"bubble sort","selection sort","insertion sort","counting sort","quick sort","quick sort (3-way partition)","merge sort"};
|
String[] sortingAlgoNames = {"bubble sort","selection sort","insertion sort","insertion sort (linked list)","counting sort","quick sort","quick sort (3-way partition)","merge sort"};
|
||||||
boolean[] sortingAlgoChoices = new boolean[sortingAlgoNames.length];
|
boolean[] sortingAlgoChoices = new boolean[sortingAlgoNames.length];
|
||||||
System.out.print("\nPlease enter file name: ");
|
System.out.print("\nPlease enter file name: ");
|
||||||
filename = sc.next();
|
filename = sc.next();
|
||||||
@@ -133,10 +133,11 @@ public class AlgorithmTester{
|
|||||||
boolean bubble=sortingAlgoChoices[0];
|
boolean bubble=sortingAlgoChoices[0];
|
||||||
boolean selection=sortingAlgoChoices[1];
|
boolean selection=sortingAlgoChoices[1];
|
||||||
boolean insertion=sortingAlgoChoices[2];
|
boolean insertion=sortingAlgoChoices[2];
|
||||||
boolean counting=sortingAlgoChoices[3];
|
boolean insertionll=sortingAlgoChoices[3];
|
||||||
boolean quick=sortingAlgoChoices[4];
|
boolean counting=sortingAlgoChoices[4];
|
||||||
boolean quickTWP=sortingAlgoChoices[5];
|
boolean quick=sortingAlgoChoices[5];
|
||||||
boolean merge=sortingAlgoChoices[6];
|
boolean quickTWP=sortingAlgoChoices[6];
|
||||||
|
boolean merge=sortingAlgoChoices[7];
|
||||||
ArrayList<SortResult> results = new ArrayList<SortResult>();
|
ArrayList<SortResult> results = new ArrayList<SortResult>();
|
||||||
RandomNumberFileReader reader = new RandomNumberFileReader(filename);
|
RandomNumberFileReader reader = new RandomNumberFileReader(filename);
|
||||||
if(bubble){
|
if(bubble){
|
||||||
@@ -151,6 +152,10 @@ public class AlgorithmTester{
|
|||||||
InsertionSorter inSorter = new InsertionSorter(reader);
|
InsertionSorter inSorter = new InsertionSorter(reader);
|
||||||
results.add(inSorter.measuredSort());
|
results.add(inSorter.measuredSort());
|
||||||
}
|
}
|
||||||
|
if(insertionll){
|
||||||
|
InsertionSorterLinkedList inSorterLL = new InsertionSorterLinkedList(reader);
|
||||||
|
results.add(inSorterLL.measuredSort());
|
||||||
|
}
|
||||||
if(counting){
|
if(counting){
|
||||||
CountingSorter countSorter = new CountingSorter(reader);
|
CountingSorter countSorter = new CountingSorter(reader);
|
||||||
results.add(countSorter.measuredSort());
|
results.add(countSorter.measuredSort());
|
||||||
|
|||||||
30
Sorting/InsertionSorterLinkedList.java
Normal file
30
Sorting/InsertionSorterLinkedList.java
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package Sorting;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
|
||||||
|
public class InsertionSorterLinkedList extends Sorter{
|
||||||
|
|
||||||
|
public InsertionSorterLinkedList(RandomNumberFileReader reader){
|
||||||
|
super("insertion sort (linked list)", reader);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void sort(){
|
||||||
|
LinkedList<Integer> numbersll = new LinkedList<Integer>(Arrays.asList(numbers));//make a linked list from numbers array
|
||||||
|
for(int i=1;i<numbersll.size();i++){
|
||||||
|
int value = numbersll.get(i);
|
||||||
|
int j=0;
|
||||||
|
while(j>=0 && compare(numbers.get(j),numbersll.get(i))<0){
|
||||||
|
j--;
|
||||||
|
}
|
||||||
|
numbersll.add(j+1, numbersll.remove(i));
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
for(int i=0;i<numbers.length;i++){
|
||||||
|
writeToArray(numbers, i, numbersll.removeFirst());//write sorted linked list back into array
|
||||||
|
}*/
|
||||||
|
numbersll.toArray(numbers);
|
||||||
|
writesUsed += numbers.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -34,7 +34,7 @@ abstract class Sorter {
|
|||||||
* @param a the index of the first element
|
* @param a the index of the first element
|
||||||
* @param b the index of the second element
|
* @param b the index of the second element
|
||||||
*/
|
*/
|
||||||
void swap(int a, int b){
|
protected void swap(int a, int b){
|
||||||
int tmp = numbers[a];
|
int tmp = numbers[a];
|
||||||
numbers[a] = numbers[b];
|
numbers[a] = numbers[b];
|
||||||
numbers[b] = tmp;
|
numbers[b] = tmp;
|
||||||
@@ -47,7 +47,7 @@ abstract class Sorter {
|
|||||||
* @param b the second integer to be compared
|
* @param b the second integer to be compared
|
||||||
* @return a negative number if the first integer is smaller than the second, 0 if they are equal, and a positive number if the first integer is greater than the second.
|
* @return a negative number if the first integer is smaller than the second, 0 if they are equal, and a positive number if the first integer is greater than the second.
|
||||||
*/
|
*/
|
||||||
int compare(int a, int b){
|
protected int compare(int a, int b){
|
||||||
comparisonsUsed++;
|
comparisonsUsed++;
|
||||||
return Integer.compare(a,b);
|
return Integer.compare(a,b);
|
||||||
}
|
}
|
||||||
@@ -58,7 +58,7 @@ abstract class Sorter {
|
|||||||
* @param index the index to be written to
|
* @param index the index to be written to
|
||||||
* @param value the value to write
|
* @param value the value to write
|
||||||
*/
|
*/
|
||||||
void writeToArray(Integer[] array, int index, int value){
|
protected void writeToArray(Integer[] array, int index, int value){
|
||||||
array[index]=value;
|
array[index]=value;
|
||||||
writesUsed++;
|
writesUsed++;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user