Repository navigation
Expand file tree
/
Copy pathTestClass.java
More file actions
198 lines (139 loc) · 8.1 KB
/
Copy pathTestClass.java
File metadata and controls
198 lines (139 loc) · 8.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import java.util.* ;
public class TestClass {
//Auxilliary functions
// array generating functions
public static int[] randomArray(int upperBound , int numberOfElements){
int[] arrayToBeSorted = new int[numberOfElements] ;
for(int iterator = 0 ; iterator<numberOfElements; iterator++){
arrayToBeSorted[iterator] = (int) Math.floor((Math.random()*upperBound)) ;
System.out.print(arrayToBeSorted[iterator]+" ") ;
}
System.out.println() ;
System.out.println() ;
return arrayToBeSorted ;
}
// to be completed -
public static void printMessage(String name , int elements , long runTime){
System.out.println() ;
}
public static void main(String args[]){
// taking input of the size of the array
Scanner input = new Scanner(System.in) ;
int numberOfElements = -1 ;
int upperBound = 0 ;
System.out.println("enter the number of elements to be sorted") ;
// enter the number of elements to be generated in the array
if (input.hasNextInt()){numberOfElements = input.nextInt() ; }
else{ System.out.println("Not an integer") ; }
int[] arrayToBeSorted ;
// enter the upperBound of the randomly Generated Elements
System.out.println("Enter the upper bound of the elements to be sorted(integer)") ;
if(input.hasNextInt()){ upperBound = input.nextInt() ; }
else{ System.out.println("Enter a valid upperBound") ; }
// randomly generating elements in an array and putting it into an array
System.out.println() ;
System.out.println("Insertion Sort") ;
System.out.println("The array to be Sorted:") ;
// calling insertionSort on the randomly Generated Array of ints
arrayToBeSorted = TestClass.randomArray(upperBound,numberOfElements) ;
long startTimeInsertionSort = System.nanoTime() ;
SortingAlgorithms.insertionSort(arrayToBeSorted);
long endTimeInsertionSort= System.nanoTime() ;
long runTimeInsertionSort = (endTimeInsertionSort - startTimeInsertionSort);
System.out.println("The sorted elements are as follows: ") ;
for(int iterator= 0 ; iterator< arrayToBeSorted.length ; iterator++){System.out.print(arrayToBeSorted[iterator]+" ") ;}
System.out.println();
System.out.println("The runtime for Insertion Sort with "+numberOfElements+" elements is "+runTimeInsertionSort+ " nanoseconds" ) ;
System.out.println() ;
// calling recursive Insertion sort
System.out.println("Recursive Insertion Sort") ;
System.out.println("Array to be Sorted") ;
int[] arrayToBeSortedRecursiveInsertion = TestClass.randomArray(upperBound,numberOfElements) ;
long startTimeRecursiveInsertion = System.nanoTime() ;
// calling the recursive insertion Sort
SortingAlgorithms.recursiveInsertionSort(arrayToBeSortedRecursiveInsertion, arrayToBeSortedRecursiveInsertion.length);
long endTimeRecursiveInsertion = System.nanoTime() ;
long runTimeRecursiveInsertionSort = endTimeRecursiveInsertion - startTimeRecursiveInsertion ;
for(int counter: arrayToBeSortedRecursiveInsertion){System.out.print(counter+" ") ; }
System.out.println() ;
System.out.println("The runtime of Recursive Insertion Sort with "+numberOfElements+" elements is "+ runTimeRecursiveInsertionSort+" nanoSeconds" ) ;
System.out.println() ;
// running Selection Sort
System.out.println("SelectionSort ") ;
System.out.println("Array to be sorted : ") ;
int[] arrayToBeSortedSelection = TestClass.randomArray(upperBound,numberOfElements) ;
long startTimeSelectionSort = System.nanoTime() ;
SortingAlgorithms.selectionSort(arrayToBeSortedSelection);
long endTimeSelectionSort = System.nanoTime() ;
long runTimeSelectionSort = (endTimeSelectionSort - startTimeInsertionSort) ;
System.out.println("Result") ;
for(int iterator : arrayToBeSortedSelection){System.out.print(iterator+ " ") ; }
System.out.println() ;
System.out.println("The runtime of SELECTION SORT with " + numberOfElements+" elements is "+ runTimeSelectionSort+" nanoseconds") ;
// running BubbleSort
//initial array for bubbleSort
System.out.println() ;
System.out.println("Bubble Sort: ");
System.out.println("Array to be sorted") ;
int[] arrayToBeSortedBubble = TestClass.randomArray(upperBound,numberOfElements) ;
long startTimeBubbleSort = System.nanoTime() ;
SortingAlgorithms.bubbleSort(arrayToBeSortedBubble);
long endTimeBubbleSort = System.nanoTime() ;
long runTimeBubbleSort = (endTimeBubbleSort - startTimeBubbleSort);
System.out.println("Result") ;
for(int iterator : arrayToBeSortedBubble){System.out.print(iterator+ " ") ; }
System.out.println() ;
System.out.println("The runtime of BubbleSort with" + numberOfElements +" elements is "+ runTimeBubbleSort+" nanoseconds") ;
// calling MergeSort
System.out.println("Merge Sort") ;
System.out.println("Array to be sorted: ") ;
int [] arrayToBeSortedMerge = TestClass.randomArray(upperBound, numberOfElements);
long startTimeMergeSort = System.nanoTime() ;
SortingAlgorithms.mergeSort(arrayToBeSortedMerge,0,arrayToBeSortedMerge.length-1);
long endTimeMergeSort = System.nanoTime() ;
long runTimeMergeSort = (endTimeMergeSort - startTimeMergeSort) ;
for(int iterator : arrayToBeSortedMerge){ System.out.print(iterator+ " " ); } // print the elements after sorting
System.out.println() ;
System.out.println("The runtime of MergeSort with "+numberOfElements +" elements is "+runTimeMergeSort+" nanoseconds") ;
System.out.println() ;
// calling HEAP SORT
System.out.println("HEAP SORT ");
System.out.println("Array to be Sorted: ") ;
int[] arrayToBeSortedHeap = TestClass.randomArray(upperBound,numberOfElements) ;
long startTimeHeapSort = System.nanoTime() ;
SortingAlgorithms.heapSort(arrayToBeSortedHeap);
long endTimeHeapSort = System.nanoTime() ;
long runTimeHeapSort = endTimeHeapSort - startTimeHeapSort ;
for(int elements: arrayToBeSortedHeap){System.out.print(elements+" ") ; } // print the elements after sorting
System.out.println() ;
System.out.println("The runtime of HeapSort with"+ numberOfElements+" elements is "+ runTimeHeapSort+" nanoseconds") ;
System.out.println();
// calling Shell Sort
System.out.println("SHELL SORT :") ;
System.out.println("Array to be sorted :") ;
int[] arrayToBeSortedShell = TestClass.randomArray(upperBound,numberOfElements) ;
long startTimeShellSort = System.nanoTime() ;
SortingAlgorithms.shellSort(arrayToBeSortedShell);
long endTimeShellSort = System.nanoTime() ;
long runTimeShellSort = endTimeShellSort - startTimeShellSort ;
for(int element :arrayToBeSortedShell){
System.out.print(element+" ") ;
}
System.out.println("The runTime of Shell Sort with "+numberOfElements+" elements is"+runTimeShellSort+" nanoSeconds") ;
// calling Counting Sort
System.out.println("Counting Sort : ") ;
System.out.println("Array to be sorted: ") ;
int[] arrayToBeSortedCounting = TestClass.randomArray(upperBound, numberOfElements) ;
long startTimeCountingSort = System.nanoTime();
int[] resultArrayCounting = new int[arrayToBeSortedCounting.length] ;
SortingAlgorithms.countingSort(arrayToBeSortedCounting , resultArrayCounting);
long endTimeCountingSort = System.nanoTime() ;
long runTimeCountingSort = endTimeCountingSort - startTimeCountingSort ;
//for
for(int elements: resultArrayCounting){
System.out.println(elements+" ") ;
}
System.out.println("The runtime of Counting Sort :"+numberOfElements+" is :"+ runTimeCountingSort+" nanoseconds") ;
// calling radix sort
}
}