Datasets:
ArXiv:
License:
File size: 6,477 Bytes
c574d3a |
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 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
package rd222dv_assign4.time;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;
public class Exercise3 {
public static void main(String[] args) {
//timeInsertionSort();
// timeStringInsertionSort();
timeIntMergeSort();
}
public static void timeInsertionSort() {
int[] insertArr = randArray(20, 40);
int estimatedTime = 0;
while (estimatedTime < 1000) {
long before = System.currentTimeMillis();
insertionSort(insertArr);
long after = System.currentTimeMillis();
estimatedTime = (int) (after - before);
System.out.println("Int time: " + estimatedTime + " millisec. Total length: " + insertArr.length);
//increasing and decreasing the size of the array slowly so that the while loop does not stop
if (estimatedTime == 999 || estimatedTime == 1000 || estimatedTime == 1001) { //breaking it to get a result
break;
} else if (estimatedTime > 1001 && estimatedTime <= 1050) {
insertArr = randArray(insertArr.length - 1, insertArr.length * 2);
estimatedTime = 0;
} else if (estimatedTime > 1050) {
insertArr = randArray(insertArr.length - 500, insertArr.length * 2);
estimatedTime = 0;
} else if (estimatedTime <= 950) {
insertArr = randArray(insertArr.length + 100, insertArr.length * 2);
estimatedTime = 0;
} else {
insertArr = randArray(insertArr.length + 1, insertArr.length * 2);
}
}
}
public static void timeIntMergeSort() {
int[] insertArr = randArray(20, 40);
int estimatedTime = 0;
while (estimatedTime < 1000) {
long before = System.currentTimeMillis();
mergeSort(insertArr);
long after = System.currentTimeMillis();
estimatedTime = (int) (after - before);
System.out.println(
"Merge Int time: " + estimatedTime + " millisec. Total length: " + insertArr.length);
if (estimatedTime == 999 || estimatedTime == 1000 || estimatedTime == 1001) {
break;
} else if (estimatedTime > 1001 && estimatedTime <= 1050) {
insertArr = randArray(insertArr.length - 1, insertArr.length * 2);
estimatedTime = 0;
} else if (estimatedTime <= 950) {
insertArr = randArray(insertArr.length + 1000, insertArr.length * 2);
} else if (estimatedTime > 1050) {
insertArr = randArray(insertArr.length - 1000, insertArr.length * 2);
} else {
insertArr = randArray(insertArr.length + 1, insertArr.length * 2);
}
}
}
public static void timeStringInsertionSort() {
String[] insertionArr = randomStringArray(100, 300);
Comparator<String> c = null;
int estimatedTime = 0;
while (estimatedTime < 1000) {
long before = System.currentTimeMillis();
insertionSort(insertionArr, c);
long after = System.currentTimeMillis();
estimatedTime = (int) (after - before);
System.out.println(
"String time: " + estimatedTime + " millisec. Total length: " + insertionArr.length);
if (estimatedTime == 999 || estimatedTime == 1000 || estimatedTime == 1001) {
break;
} else if (estimatedTime > 1001 && estimatedTime <= 1050) {
insertionArr = randomStringArray(insertionArr.length - 1, insertionArr.length * 2);
estimatedTime = 0;
} else if (estimatedTime > 1050) {
insertionArr = randomStringArray(insertionArr.length - 250, insertionArr.length * 2);
estimatedTime = 0;
} else if (estimatedTime <= 950) {
insertionArr = randomStringArray(insertionArr.length + 500, insertionArr.length * 2);
} else {
insertionArr = randomStringArray(insertionArr.length + 1, insertionArr.length * 2);
}
}
}
/*the two random array methods*/
private static String[] randomStringArray(int length, int n) {
Random rand = new Random();
String[] arr = new String[length];
String[] letter = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r",
"s", "t", "u", "v", "w", "x", "y", "z" };
if (n >= letter.length || n <= 0)
n = letter.length;
StringBuilder sb;
for (int i = 0; i < arr.length; i++) {
sb = new StringBuilder();
int size = 0;
while (size != 10) {
sb.append(letter[rand.nextInt(n)]);
size++;
}
arr[i] = sb.toString();
}
return arr;
}
private static int[] randArray(int length, int n) {
Random rand = new Random();
int[] arr = new int[length];
for (int i = 0; i < arr.length; i++) {
arr[i] = rand.nextInt(n);
}
return arr;
}
/* all methods taken from assign3 */
public static int[] insertionSort(int[] sort) {
for (int i = 1; i < sort.length; i++) {
int temp = sort[i];
int j = i;
while (j > 0 && sort[j - 1] > temp) {
sort[j] = sort[j - 1];
j--;
}
sort[j] = temp;
}
return sort;
}
public static String[] insertionSort(String[] in, Comparator<String> c) { // inspired
// by
// http://algs4.cs.princeton.edu/21elementary/Insertion.java.html
String[] temp1 = in;
for (int i = 1; i < temp1.length; i++) {
String temp2 = temp1[i];
int j = i;
while ((j > 0) && c.compare(temp2, temp1[j - 1]) > 0) {
temp1[j] = temp1[j - 1];
j--;
}
temp1[j] = temp2;
}
return temp1;
}
public static int[] mergeSort(int[] in) {
int[] newArray = in;
if (newArray.length <= 1) {
return newArray;
}
int half = newArray.length / 2;
int[] arr1 = new int[half];
int[] arr2 = new int[newArray.length - arr1.length];
for (int i = 0; i < arr1.length; i++) {
arr1[i] = newArray[i];
}
for (int i = 0; i < arr2.length; i++) {
arr2[i] = newArray[half++];
}
arr1 = mergeSort(arr1); // recursively saving arr1 and arr2
arr2 = mergeSort(arr2);
int iarr1 = 0; // merge
int iarr2 = 0;
int j = 0;
while (iarr1 < arr1.length && iarr2 < arr2.length) {
if (arr1[iarr1] < arr2[iarr2]) {
newArray[j] = arr1[iarr1];
iarr1++;
} else {
newArray[j] = arr2[iarr2];
iarr2++;
}
j++;
}
while (iarr1 < arr1.length) {
newArray[j] = arr1[iarr1];
iarr1++;
j++;
}
while (iarr2 < arr2.length) {
newArray[j] = arr2[iarr2];
iarr2++;
j++;
}
return newArray;
}
}
/* used Resources:
*https://github.com/sarpreetsingh3131/1DV507-Programming-
* and-Data-Structures/blob/master/sb223ce_assign4/
* sb223ce_assign4/Exercise4/Experiment.java*/
|