adding linked list version of insertion sort

This commit is contained in:
2020-06-16 13:44:08 -05:00
parent 72895d3cfd
commit 9910ec23b2
3 changed files with 43 additions and 8 deletions

View File

@@ -84,7 +84,7 @@ public class AlgorithmTester{
int input;
boolean ready=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];
System.out.print("\nPlease enter file name: ");
filename = sc.next();
@@ -133,10 +133,11 @@ public class AlgorithmTester{
boolean bubble=sortingAlgoChoices[0];
boolean selection=sortingAlgoChoices[1];
boolean insertion=sortingAlgoChoices[2];
boolean counting=sortingAlgoChoices[3];
boolean quick=sortingAlgoChoices[4];
boolean quickTWP=sortingAlgoChoices[5];
boolean merge=sortingAlgoChoices[6];
boolean insertionll=sortingAlgoChoices[3];
boolean counting=sortingAlgoChoices[4];
boolean quick=sortingAlgoChoices[5];
boolean quickTWP=sortingAlgoChoices[6];
boolean merge=sortingAlgoChoices[7];
ArrayList<SortResult> results = new ArrayList<SortResult>();
RandomNumberFileReader reader = new RandomNumberFileReader(filename);
if(bubble){
@@ -151,6 +152,10 @@ public class AlgorithmTester{
InsertionSorter inSorter = new InsertionSorter(reader);
results.add(inSorter.measuredSort());
}
if(insertionll){
InsertionSorterLinkedList inSorterLL = new InsertionSorterLinkedList(reader);
results.add(inSorterLL.measuredSort());
}
if(counting){
CountingSorter countSorter = new CountingSorter(reader);
results.add(countSorter.measuredSort());

View 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;
}
}

View File

@@ -34,7 +34,7 @@ abstract class Sorter {
* @param a the index of the first 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];
numbers[a] = numbers[b];
numbers[b] = tmp;
@@ -47,7 +47,7 @@ abstract class Sorter {
* @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.
*/
int compare(int a, int b){
protected int compare(int a, int b){
comparisonsUsed++;
return Integer.compare(a,b);
}
@@ -58,7 +58,7 @@ abstract class Sorter {
* @param index the index to be written to
* @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;
writesUsed++;
}