diff --git a/BinarySearch/.idea/description.html b/BinarySearch/.idea/description.html new file mode 100644 index 0000000..db5f129 --- /dev/null +++ b/BinarySearch/.idea/description.html @@ -0,0 +1 @@ +Simple Java application that includes a class with main() method \ No newline at end of file diff --git a/BinarySearch/.idea/misc.xml b/BinarySearch/.idea/misc.xml new file mode 100644 index 0000000..5d75313 --- /dev/null +++ b/BinarySearch/.idea/misc.xml @@ -0,0 +1,12 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/BinarySearch/.idea/modules.xml b/BinarySearch/.idea/modules.xml new file mode 100644 index 0000000..ee45988 --- /dev/null +++ b/BinarySearch/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/BinarySearch/.idea/project-template.xml b/BinarySearch/.idea/project-template.xml new file mode 100644 index 0000000..1f08b88 --- /dev/null +++ b/BinarySearch/.idea/project-template.xml @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/BinarySearch/.idea/workspace.xml b/BinarySearch/.idea/workspace.xml new file mode 100644 index 0000000..d8fb887 --- /dev/null +++ b/BinarySearch/.idea/workspace.xml @@ -0,0 +1,205 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1550163941126 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No facets are configured + + + + + + + + + + + + + + + 1.8 + + + + + + + + Java4L2HW + + + + + + + + 1.8 + + + + + + + + Maven: junit:junit:4.12 + + + + + + + + \ No newline at end of file diff --git a/Java4L2HW/Java4L2HW.iml b/Java4L2HW/Java4L2HW.iml new file mode 100644 index 0000000..78b2cc5 --- /dev/null +++ b/Java4L2HW/Java4L2HW.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/Java4L2HW/pom.xml b/Java4L2HW/pom.xml new file mode 100644 index 0000000..7bc5854 --- /dev/null +++ b/Java4L2HW/pom.xml @@ -0,0 +1,34 @@ + + + 4.0.0 + + alexrm84 + Java4L2HW + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 6 + 6 + + + + + + + + + junit + junit + 4.12 + test + + + + + \ No newline at end of file diff --git a/Java4L2HW/src/main/java/Array.java b/Java4L2HW/src/main/java/Array.java new file mode 100644 index 0000000..cbcb232 --- /dev/null +++ b/Java4L2HW/src/main/java/Array.java @@ -0,0 +1,22 @@ +public interface Array { + + + void add(E value); + + E get(int index); + + int getSize(); + + boolean contains(E value); + + int indexOf(E value); + + boolean remove(E value); + void remove(int index); + + boolean isAmpty(); + + void sortBubble(); + void sortSelect(); + void sortInsert(); +} diff --git a/Java4L2HW/src/main/java/ArrayImplement.java b/Java4L2HW/src/main/java/ArrayImplement.java new file mode 100644 index 0000000..2899ec5 --- /dev/null +++ b/Java4L2HW/src/main/java/ArrayImplement.java @@ -0,0 +1,124 @@ +import java.util.Arrays; + +public class ArrayImplement> implements Array { + + protected static final int INVALID_INDEX = -1; + private static final int DEFAULT_CAPACITY = 16; + + protected int size; + protected E[] data; + + public ArrayImplement(){ + this(DEFAULT_CAPACITY); + } + + public ArrayImplement(int initialSize){ + this.data = (E[]) new Object[initialSize]; + } + + + @Override + public void add(E value) { + checkSize(); + data[size++]=value; + } + + protected void checkSize(){ + if (size==data.length) + data = Arrays.copyOf(data,data.length*2); + } + + @Override + public E get(int index) { + return data[index]; + } + + @Override + public int getSize() { + return size; + } + + @Override + public boolean contains(E value) { + return indexOf(value) != -1 ? true : false; + } + + @Override + public int indexOf(E value) { + for (int i = 0; i < size; i++) { + if (data[i].equals(value)) + return i; + } + return -1; + } + + @Override + public boolean remove(E value) { + return removeByIndex(indexOf(value)); + } + + @Override + public void remove(int index) { + if (!removeByIndex(index)) + throw new ArrayIndexOutOfBoundsException(index); + } + + private boolean removeByIndex(int index){ + if (index==INVALID_INDEX || index<0 || index>=size){ + return false; + } + + for (int i = index; i < size-1; i++) { + data[i]=data[i+1]; + } + data[size-1]=null; + size--; + return true; + } + + @Override + public boolean isAmpty() { + return size==0; + } + + @Override + public void sortBubble() { + for (int i = 0; i < size-1; i++) { + for (int j = 0; j < size-1; j++) { + if (data[j].compareTo(data[j+1])>0) + change(j,j+1); + } + } + } + + @Override + public void sortSelect() { + for (int i = 0; i < size-1; i++) { + int min=i; + for (int j = i+1; j < size; j++) { + if (data[j].compareTo(data[min])<0) + min=j; + } + change(min,i); + } + } + + @Override + public void sortInsert() { + for (int i = 1; i 0 && data[j-1].compareTo(temp)>=0){ + data[j]=data[j-1]; + j--; + } + data[j]=temp; + } + } + + void change(int index1,int index2){ + E temp=data[index1]; + data[index1]=data[index2]; + data[index2]=temp; + } +} diff --git a/Java4L2HW/src/main/java/SortedArray.java b/Java4L2HW/src/main/java/SortedArray.java new file mode 100644 index 0000000..2607eff --- /dev/null +++ b/Java4L2HW/src/main/java/SortedArray.java @@ -0,0 +1,46 @@ +public class SortedArray> extends ArrayImplement { + + @Override + public void add(E value) { + checkSize(); + int index; + for (index = 0; index < size; index++) { + if (data[index].compareTo(value)>0) + break; + } + for (int i = size; i > index; i--) { + data[i]=data[i-1]; + } + data[index]=value; + size++; + } + + @Override + public int indexOf(E value) { + int min=0; + int max=size-1; + while (min<=max){ + int mid=(min+max)/2; + if (data[mid].equals(value)) + return mid; + if (data[mid].compareTo(value)>0) + max=mid-1; + else + min=mid+1; + } + return INVALID_INDEX; + } + + public int indexOfRec(E value, int min, int max){ + int mid=(min+max)/2; + if (data[mid].equals(value)) + return mid; + if (min>max) return INVALID_INDEX; + if (data[mid].compareTo(value)>0) { + return indexOfRec(value,min,mid-1); + } else { + return indexOfRec(value, mid+1, max); + } + + } +} diff --git a/Java4L2HW/src/test/java/ArrayTests.java b/Java4L2HW/src/test/java/ArrayTests.java new file mode 100644 index 0000000..263859f --- /dev/null +++ b/Java4L2HW/src/test/java/ArrayTests.java @@ -0,0 +1,231 @@ +import org.hamcrest.core.Is; +import org.junit.Assert; +import org.junit.Test; + + +public class ArrayTests { + public static final int INVALID_VALUE = 777; + private Array array1 = new ArrayImplement(); + private Array array2 = new ArrayImplement(); + private Array array3 = new ArrayImplement(); + private Array array4 = new SortedArray(); + private Array array5 = new SortedArray(); + + + public ArrayTests(){ + + for (int i = 0; i < 100000; i++) { + int temp =(int) (Math.random()*1000); + array1.add(temp); + array2.add(temp); + array3.add(temp); +// array4.add(i); +// array5.add(i); + } + } + + @Test + public void test_runtime(){ + long start, finish; + start = System.currentTimeMillis(); + array1.sortBubble(); + finish = System.currentTimeMillis(); + System.out.println("Пузырьковая сортировка занимает "+(finish - start)+" милисекунд"); + + start = System.currentTimeMillis(); + array2.sortSelect(); + finish = System.currentTimeMillis(); + System.out.println("Сортировка выборкой занимает "+(finish - start)+" милисекунд"); + + start = System.currentTimeMillis(); + array3.sortInsert(); + finish = System.currentTimeMillis(); + System.out.println("Сортировка вставкой занимает "+(finish - start)+" милисекунд"); + } + +/* Что то не смог понять почему методы из ArrayImplement нормально видны а методы из SortedArray (в частности indexOfRec) не виден. + @Test + public void test_runtime_binary_search(){ + long start, finish; + start = System.currentTimeMillis(); + array4.indexOf(1); + finish = System.currentTimeMillis(); + System.out.println("Бинарный поиск с массивом занимает "+(finish - start)+" милисекунд"); + + start = System.currentTimeMillis(); + array5.inde(); + finish = System.currentTimeMillis(); + System.out.println("Бинарный поиск с рекурсией занимает "+(finish - start)+" милисекунд"); + } +*/ + +/* + @Test + public void test_init_array() { + Array array = new ArrayImplement(); + array.add(1); + array.add(2); + Assert.assertThat(array.getSize(), Is.is(2)); + Assert.assertThat(array.get(0), Is.is(1)); + Assert.assertThat(array.get(1), Is.is(2)); + + } + + @Test + public void test_init_array_fixed_size() { + Array array = new ArrayImplement(2); + array.add(1); + array.add(2); + array.add(3); + Assert.assertThat(array.getSize(), Is.is(3)); + Assert.assertThat(array.get(0), Is.is(1)); + Assert.assertThat(array.get(1), Is.is(2)); + Assert.assertThat(array.get(2), Is.is(3)); + } + + @Test + public void test_search() { + Array array = new ArrayImplement(); + array.add(10); + array.add(20); + array.add(70); + + Assert.assertTrue(array.contains(10)); + Assert.assertTrue(array.contains(20)); + Assert.assertTrue(array.contains(70)); + + Assert.assertFalse(array.contains(INVALID_VALUE)); + + Assert.assertThat(array.indexOf(10), Is.is(0)); + Assert.assertThat(array.indexOf(20), Is.is(1)); + Assert.assertThat(array.indexOf(70), Is.is(2)); + + Assert.assertThat(array.indexOf(INVALID_VALUE), Is.is(-1)); + } + + @Test + public void test_remove_by_index() { + Array array = new ArrayImplement(); + array.add(10); + array.add(20); + array.add(70); + + array.remove(0); + Assert.assertThat(array.getSize(), Is.is(2)); + Assert.assertThat(array.get(0), Is.is(20)); + Assert.assertThat(array.get(1), Is.is(70)); + } + + @Test(expected = ArrayIndexOutOfBoundsException.class) + public void test_remove_by_invalid_index() { + Array array = new ArrayImplement(); + array.add(10); + array.add(20); + array.add(70); + + array.remove(50); + } + + @Test + public void test_remove_by_value() { + Array array = new ArrayImplement(); + array.add(10); + array.add(20); + array.add(70); + + Assert.assertFalse(array.remove(Integer.valueOf(INVALID_VALUE))); + Assert.assertTrue(array.remove(Integer.valueOf(20))); + + Assert.assertThat(array.getSize(), Is.is(2)); + Assert.assertThat(array.get(0), Is.is(10)); + Assert.assertThat(array.get(1), Is.is(70)); + } + + @Test + public void test_sorted_array() { + Array array = new SortedArray(); + array.add(20); + array.add(10); + array.add(70); + + Assert.assertThat(array.getSize(), Is.is(3)); + Assert.assertThat(array.get(0), Is.is(10)); + Assert.assertThat(array.get(1), Is.is(20)); + Assert.assertThat(array.get(2), Is.is(70)); + } + + + @Test + public void test_binary_search() { + Array array = new SortedArray(); + array.add(20); + array.add(10); + array.add(70); + + Assert.assertTrue(array.contains(10)); + Assert.assertTrue(array.contains(20)); + Assert.assertTrue(array.contains(70)); + + Assert.assertFalse(array.contains(INVALID_VALUE)); + + Assert.assertThat(array.indexOf(10), Is.is(0)); + Assert.assertThat(array.indexOf(20), Is.is(1)); + Assert.assertThat(array.indexOf(70), Is.is(2)); + + Assert.assertThat(array.indexOf(INVALID_VALUE), Is.is(-1)); + } + + @Test + public void test_sort_bubble() { + ArrayImplement array = new ArrayImplement(); + array.add(20); + array.add(10); + array.add(70); + array.add(50); + array.add(7); + + array.sortBubble(); + Assert.assertThat(array.get(0), Is.is(7)); + Assert.assertThat(array.get(1), Is.is(10)); + Assert.assertThat(array.get(2), Is.is(20)); + Assert.assertThat(array.get(3), Is.is(50)); + Assert.assertThat(array.get(4), Is.is(70)); + } + + + @Test + public void test_sort_select() { + ArrayImplement array = new ArrayImplement(); + array.add(20); + array.add(10); + array.add(70); + array.add(50); + array.add(7); + + array.sortSelect(); + Assert.assertThat(array.get(0), Is.is(7)); + Assert.assertThat(array.get(1), Is.is(10)); + Assert.assertThat(array.get(2), Is.is(20)); + Assert.assertThat(array.get(3), Is.is(50)); + Assert.assertThat(array.get(4), Is.is(70)); + } + + + @Test + public void test_sort_insert() { + ArrayImplement array = new ArrayImplement(); + array.add(20); + array.add(10); + array.add(70); + array.add(50); + array.add(7); + + array.sortInsert(); + Assert.assertThat(array.get(0), Is.is(7)); + Assert.assertThat(array.get(1), Is.is(10)); + Assert.assertThat(array.get(2), Is.is(20)); + Assert.assertThat(array.get(3), Is.is(50)); + Assert.assertThat(array.get(4), Is.is(70)); + } + */ +} diff --git a/Java4L2HW/target/classes/Array.class b/Java4L2HW/target/classes/Array.class new file mode 100644 index 0000000..9c40ad4 Binary files /dev/null and b/Java4L2HW/target/classes/Array.class differ diff --git a/Java4L2HW/target/classes/ArrayImplement.class b/Java4L2HW/target/classes/ArrayImplement.class new file mode 100644 index 0000000..53464b3 Binary files /dev/null and b/Java4L2HW/target/classes/ArrayImplement.class differ diff --git a/Java4L2HW/target/classes/SortedArray.class b/Java4L2HW/target/classes/SortedArray.class new file mode 100644 index 0000000..40c7452 Binary files /dev/null and b/Java4L2HW/target/classes/SortedArray.class differ diff --git a/Java4L2HW/target/test-classes/ArrayTests.class b/Java4L2HW/target/test-classes/ArrayTests.class new file mode 100644 index 0000000..27e7691 Binary files /dev/null and b/Java4L2HW/target/test-classes/ArrayTests.class differ