diff --git a/Sorting/AlgorithmTester.java b/Sorting/AlgorithmTester.java index 07b6e6f..3b9564e 100644 --- a/Sorting/AlgorithmTester.java +++ b/Sorting/AlgorithmTester.java @@ -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 results = new ArrayList(); 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()); diff --git a/Sorting/InsertionSorterLinkedList.java b/Sorting/InsertionSorterLinkedList.java new file mode 100644 index 0000000..2994aab --- /dev/null +++ b/Sorting/InsertionSorterLinkedList.java @@ -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 numbersll = new LinkedList(Arrays.asList(numbers));//make a linked list from numbers array + for(int i=1;i=0 && compare(numbers.get(j),numbersll.get(i))<0){ + j--; + } + numbersll.add(j+1, numbersll.remove(i)); + } + /* + for(int i=0;i