Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,12 @@

# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*

# Intellij
.idea/
*.iml
*.iml
*.iws

# Maven
log/
target/
20 changes: 20 additions & 0 deletions hw2analysis/src/Array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public interface Array<E> {

void add(E value);

E get(int index);

boolean remove(E value);

boolean contains(E value);
int indexOf(E value);

int getSize();
boolean isEmpty();

void sortBubble();

void sortSelect();

void sortInsert();
}
151 changes: 151 additions & 0 deletions hw2analysis/src/ArrayImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ArrayImpl<E extends Object & Comparable<? super E>> implements Array<E> {

private static final int INITIAL_CAPACITY = 16;

protected E[] data;
protected int currentSize;

public ArrayImpl() {
this(INITIAL_CAPACITY);
}

@SuppressWarnings("unchecked")
public ArrayImpl(int initialCapacity) {
this.data = (E[]) new Object[initialCapacity];
}

@Override//O(N)
public void add(E value) {
if (currentSize == data.length) {
data = grow();
}
data[currentSize++] = value;
}

protected E[] grow() {
return Arrays.copyOf(data, data.length * 2);
}

@Override//O(1)
public E get(int index) {
if (index < 0 || index >= currentSize) {
throw new IllegalArgumentException("Invalid index value");
}
return data[index];
}

@Override//O(N)
public boolean remove(E value) {
int index = indexOf(value);
if (index == -1) {
return false;
}

for (int i = index; i < currentSize - 1; i++) {
data[i] = data[i + 1];
}

data[currentSize - 1] = null;
currentSize--;

return true;
}

@Override
public boolean contains(E value) {
return indexOf(value) != -1;
}

@Override//O(N)
public int indexOf(E value) {
for (int i = 0; i < currentSize; i++) {
if (value.equals(data[i])) {
return i;
}
}

return -1;
}

@Override
public int getSize() {
return currentSize;
}

@Override
public boolean isEmpty() {
return currentSize == 0;
}

@Override
public void sortBubble() {
for (int i = 0; i < currentSize - 1; i++) {
for (int j = 0; j < currentSize - 1 - i; j++) {
if ( data[j].compareTo(data[j + 1]) > 0 ) {
swap(j, j + 1);
}
}
}
}

@Override
public void sortSelect() {
for (int i = 0; i < currentSize - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < currentSize; j++) {
if (data[j].compareTo(data[minIndex]) < 0) {
minIndex = j;
}
}
if (minIndex != i) {
swap(minIndex, i);
}
}
}

@Override
public void sortInsert() {
for (int i = 1; i < currentSize; i++) {
E temp = data[i];

int in = i;
while (in > 0 && data[in - 1].compareTo(temp) >= 0) {
data[in] = data[in -1];
in--;
}

data[in] = temp;
}
}

private void swap(int index1, int index2) {
E temp = data[index1];
data[index1] = data[index2];
data[index2] = temp;
}

@Override
public String toString() {
return Stream.of(data)
.limit(currentSize)
// .map(element -> String.valueOf(element))
.map(String::valueOf)
.collect(Collectors.joining(", ", "[", "]"));

// StringBuilder sb = new StringBuilder("[");
// for (int i = 0; i < currentSize; i++) {
// sb.append(data[i]);
// if (i < currentSize - 1) {
// sb.append(", ");
// }
// }
//
// sb.append("]");
//
// return sb.toString();
}
}
80 changes: 80 additions & 0 deletions hw2analysis/src/ConcurrencyMainDZ.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;
import java.util.function.Supplier;

public class ConcurrencyMainDZ {

private static final int ARRAY_CAPACITY = 1_000_000;


public static final int MAX_VALUE = 10_000;

public static void main(String[] args) throws InterruptedException, TimeoutException, ExecutionException {
Supplier<Array> constructor = ArrayImpl::new;
// Supplier<Array> constructor = SortedArrayImpl::new;

Array arr1 = createArray(constructor);
Array arr2 = createArray(constructor);
Array arr3 = createArray(constructor);

randomInitialize(arr1, arr2, arr3);

ExecutorService executorService = Executors.newFixedThreadPool(3);

// List<Runnable> tasks = List.of(
// measureTime(arr1::sortBubble, "Sort Bubble"),
// measureTime(arr2::sortSelect, "Sort Select"),
// measureTime(arr3::sortInsert, "Sort Insert")
// );

List tasks = new ArrayList<Runnable>();
tasks.add(measureTime(arr1::sortBubble, "Sort Bubble"));
tasks.add(measureTime(arr1::sortSelect, "Sort Bubble"));
tasks.add(measureTime(arr1::sortInsert, "Sort Bubble"));

// tasks.forEach(executorService::execute);

// for (Runnable task : tasks) {
// executorService.execute(task);
// }

for (Object task : tasks) {
executorService.execute((Runnable) task);
}

executorService.shutdown();
executorService.awaitTermination(1, TimeUnit.MINUTES);
}

private static void randomInitialize(Array... arrays) {
Random random = new Random();

for (int i = 0; i < ARRAY_CAPACITY; i++) {
int value = random.nextInt(MAX_VALUE);
for (Array array : arrays) {
array.add(value);
}
}
}

private static Runnable measureTime(Runnable action, String actionName) {
return () -> {
long startTime = System.nanoTime();
action.run();
long finishTime = System.nanoTime();
long duration = finishTime - startTime;

System.out.println(String.format("%s took time: %d ms.",
actionName,
TimeUnit.NANOSECONDS.toMicros(duration)));
};

}

private static Array createArray(Supplier<Array> factory) {
return factory.get();
}

}
66 changes: 66 additions & 0 deletions hw2analysis/src/Main2.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import java.util.concurrent.TimeUnit;

public class Main2 {

public static void main(String[] args) {
// testJdkArray();
// testArray();

Array<Integer> array = new ArrayImpl<>();
array.add(2);
array.add(1);
array.add(4);
array.add(5);
array.add(3);

System.out.println(array);
// array.sortBubble();
// array.sortSelect();
array.sortInsert();
// System.nanoTime();
// TimeUnit.NANOSECONDS.toMillis();
System.out.println(array);

}

private static void testArray() {
// Array<Integer> array = new ArrayImpl<>();
Array<Integer> array = new SortedArrayImpl<>();
System.out.println(array);
array.add(2);
array.add(1);
array.add(4);
array.add(5);
array.add(3);

System.out.println(array);

System.out.println("Find 5 = " + array.contains(5));
System.out.println("Index of 3 = " + array.indexOf(3));
System.out.println("Index of 33 = " + array.indexOf(33));

System.out.println("Remove 3: " + array.remove(3));
System.out.println("Remove 33: " + array.remove(33));
System.out.println("Index of 3 = " + array.indexOf(3));

System.out.println(array);
System.out.println("Size is " + array.getSize());

System.out.println("----------");
for (int i = 0; i < array.getSize(); i++) {
System.out.println(array.get(i));
}
}

private static void testJdkArray() {
// Integer[] array = new Integer[] {1, 2, 3, 4, 5};
Integer[] array = {1, 2, 3, 4, 5};


// array[0] = 5;
// array[2] = 48;
for (Integer value : array) {
System.out.println(value);
}
}
}
43 changes: 43 additions & 0 deletions hw2analysis/src/SortedArrayImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
public class SortedArrayImpl<E extends Object & Comparable<? super E>> extends ArrayImpl<E> {

@Override//O(logN)
public int indexOf(E value) {
int low = 0;
int high = currentSize - 1;
while (low <= high) {
int mid = (low + high) / 2;
if ( data[mid].equals(value) ) {
return mid;
}
else if ( value.compareTo(data[mid]) > 0 ) {
low = mid + 1;
}
else {
high = mid - 1;
}

}

return -1;
}

@Override//O(N)
public void add(E value) {
if (currentSize == data.length) {
data = grow();
}

int index;
for (index = 0; index < currentSize; index++) {
if (data[index].compareTo(value) > 0)
break;
}

for (int i = currentSize; i > index; i--) {
data[i] = data[i - 1];
}

data[index] = value;
currentSize++;
}
}
Loading