From 39cca09108303cd8e597c6026d3993357b886300 Mon Sep 17 00:00:00 2001 From: sergeynikitin Date: Wed, 30 Jun 2021 18:37:42 +0300 Subject: [PATCH 1/7] Create MyArrayList and MyArraySortedList; --- src/algorithmsAndDataStructures/Main.java | 56 ++++++- .../MyArrayList.java | 153 ++++++++++++++++++ .../MyArraySortedList.java | 32 ++++ 3 files changed, 239 insertions(+), 2 deletions(-) create mode 100644 src/algorithmsAndDataStructures/MyArrayList.java create mode 100644 src/algorithmsAndDataStructures/MyArraySortedList.java diff --git a/src/algorithmsAndDataStructures/Main.java b/src/algorithmsAndDataStructures/Main.java index ba454e2..b839847 100644 --- a/src/algorithmsAndDataStructures/Main.java +++ b/src/algorithmsAndDataStructures/Main.java @@ -1,8 +1,60 @@ package algorithmsAndDataStructures; +import java.util.Random; + public class Main { public static void main(String[] args) { - // write your code here + long startTimeCreate = System.currentTimeMillis(); + Random random = new Random(); + MyArrayList mal = new MyArrayList<>(100000); + for (int i = 0; i < 100000; i++) { + mal.add(random.nextInt(100000)); + } + System.out.println(System.currentTimeMillis() - startTimeCreate); + + selectionSort(mal); + insertionSort(mal); + bubbleSort(mal); + + + } + + public static void selectionSort(MyArrayList mal) { + long startTimeSel = System.currentTimeMillis(); + mal.selectionSort(); + System.out.printf("selectionSort: %s ", System.currentTimeMillis() - startTimeSel); + } + + public static void insertionSort(MyArrayList mal) { + long startTimeIns = System.currentTimeMillis(); + mal.insertionSort(); + System.out.printf("insertionSort: %s ", System.currentTimeMillis() - startTimeIns); + } + + public static void bubbleSort(MyArrayList mal) { + long startTimeBub = System.currentTimeMillis(); + mal.bubbleSort(); + System.out.printf("bubbleSort: %s", System.currentTimeMillis() - startTimeBub); + } + + + public static void doTask() { + MyArrayList list = new MyArrayList<>(); + list.add(1); + list.add(2); + list.add(3); + list.add(4); + list.add(5); + list.add(6); + list.add(7); + list.add(8); + list.add(9); + list.add(10); + list.add(2, 66); + list.add(4, 66); + list.add(3, 66); + + System.out.println(list); } -} +} \ No newline at end of file diff --git a/src/algorithmsAndDataStructures/MyArrayList.java b/src/algorithmsAndDataStructures/MyArrayList.java new file mode 100644 index 0000000..a0c98b5 --- /dev/null +++ b/src/algorithmsAndDataStructures/MyArrayList.java @@ -0,0 +1,153 @@ +package algorithmsAndDataStructures; + +import java.util.Arrays; + +public class MyArrayList> { + private E[] list; + private int size; + private final int DEFAULT_CAPACITY = 10; + private int capacity; + + public MyArrayList(int capacity) { + this.capacity = capacity; + if (capacity <= 0) { + throw new IllegalArgumentException("capacity: " + capacity); + } else { + list = (E[]) new Comparable[capacity]; + } + } + + public MyArrayList() { + list = (E[]) new Comparable[DEFAULT_CAPACITY]; + } + + public int size() { + return size; + } + + public void add(E item) { + if (capacity < size) { + list[size] = item; + size++; + } else checkArray(item); + } + + private void checkArray(E item) { + capacity = capacity + DEFAULT_CAPACITY / 2 + 1; + E[] newList = (E[]) new Comparable[capacity]; + System.arraycopy(list, 0, newList, 0, size); + list = newList; + list[size] = item; + size++; + } + + // Проверка на переполненность не делается но массив как будто сам увеличивается не могу понять почему так) + public void add(int index, E item) { + if (index >= 0 & index <= size) { + for (int i = size; i > index; i--) { + list[i] = list[i - 1]; + } + list[index] = item; + size++; + } else throw new ArrayIndexOutOfBoundsException("index " + index); + } + + public boolean remove(E item) { + int i = index(item); + if (i == -1) { + return false; + } else removeIndex(i); + return true; + } + + public void removeIndex(int index) { + if (isEmpty()) { + throw new IndexOutOfBoundsException(); + } + if (index >= 0 & index <= size) { + for (int i = index; i < size; i++) { + list[i] = list[i + 1]; + } + size--; + list[size] = null; + } + } + + public boolean isEmpty() { + return size == 0; + } + + public E get(int index) { + if (index >= 0 & index < size) { + return list[index]; + } else throw new IndexOutOfBoundsException("no index"); + } + + public int indexOf(E item) { + return index(item); + } + + public boolean contains(E item) { + return index(item) > -1; + } + + private int index(E item) { + for (int i = 0; i < size; i++) { + if (list[i].equals(item)) { + return i; + } + } + return -1; + } + + @Override + public String toString() { + return Arrays.toString(Arrays.copyOf(list, size)); + } + + private boolean less(E item1, E item2) { + return (item1.compareTo(item2) < 0); + } + + private void swap(int index1, int index2) { + E temp = list[index1]; + list[index1] = list[index2]; + list[index2] = temp; + } + + public void selectionSort() { + int iMin; + for (int i = 0; i < size - 1; i++) { + iMin = i; + for (int j = i + 1; j < size; j++) { + if (less(list[j], list[iMin])) { + iMin = j; + } + } + swap(i, iMin); + } + } + + public void insertionSort() { + E key; + for (int i = 1; i < size; i++) { + int j = i; + key = list[i]; + while (j > 0 && less(key, list[j - 1])) { + list[j] = list[j - 1]; + j--; + } + list[j] = key; + } + } + + public void bubbleSort() { + for (int i = size - 1; i > 0; i--) { + for (int j = 0; j < i; j++) { + if (less(list[j + 1], list[j])) { + swap(j + 1, j); + } + } + } + } +} \ No newline at end of file diff --git a/src/algorithmsAndDataStructures/MyArraySortedList.java b/src/algorithmsAndDataStructures/MyArraySortedList.java new file mode 100644 index 0000000..764cb6c --- /dev/null +++ b/src/algorithmsAndDataStructures/MyArraySortedList.java @@ -0,0 +1,32 @@ +package algorithmsAndDataStructures; + +public class MyArraySortedList> extends MyArrayList { + @Override + public void add(E item) { + int i = 0; + while (i < size() && item.compareTo(get(i)) >= 0) { + i++; + } + super.add(i, item); + + } + + @Override + public void add(int index, E item) { + add(item); + } + + public int binaryFind(E item) { + int x = 0; + int y = size() - 1; + while (x <= y) { + int z = y + (x - y) / 2; + if (item.compareTo(get(z)) > 0) { + x = z + 1; + } else if (item.compareTo(get(z)) < 0) { + y = z - 1; + } else return z; + } + return -1; + } +} From ccc81e2c6749db9308a70b0945146cd8143c7b71 Mon Sep 17 00:00:00 2001 From: SerjNikitin <76908941+SerjNikitin@users.noreply.github.com> Date: Thu, 1 Jul 2021 10:37:31 +0300 Subject: [PATCH 2/7] Create README.md --- README.md | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..71abc2a --- /dev/null +++ b/README.md @@ -0,0 +1,2 @@ +# AlgorithmsAndDataStructures +Create ArrayList and SortedArrayList, implemented sorting: selection, inserts, bubble. From 226782f7e23de8823baea8a82bd3204febe360c0 Mon Sep 17 00:00:00 2001 From: sergeynikitin Date: Thu, 1 Jul 2021 10:51:47 +0300 Subject: [PATCH 3/7] Create MyArrayList and MyArraySortedList; --- src/algorithmsAndDataStructures/Main.java | 9 ++++----- src/algorithmsAndDataStructures/MyArrayList.java | 1 - 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/src/algorithmsAndDataStructures/Main.java b/src/algorithmsAndDataStructures/Main.java index b839847..6e42766 100644 --- a/src/algorithmsAndDataStructures/Main.java +++ b/src/algorithmsAndDataStructures/Main.java @@ -5,6 +5,8 @@ public class Main { public static void main(String[] args) { + doTask(); + long startTimeCreate = System.currentTimeMillis(); Random random = new Random(); MyArrayList mal = new MyArrayList<>(100000); @@ -12,12 +14,9 @@ public static void main(String[] args) { mal.add(random.nextInt(100000)); } System.out.println(System.currentTimeMillis() - startTimeCreate); - selectionSort(mal); insertionSort(mal); bubbleSort(mal); - - } public static void selectionSort(MyArrayList mal) { @@ -51,8 +50,8 @@ public static void doTask() { list.add(8); list.add(9); list.add(10); - list.add(2, 66); - list.add(4, 66); + list.add(10, 66); + list.add(11, 66); list.add(3, 66); System.out.println(list); diff --git a/src/algorithmsAndDataStructures/MyArrayList.java b/src/algorithmsAndDataStructures/MyArrayList.java index a0c98b5..52adcd4 100644 --- a/src/algorithmsAndDataStructures/MyArrayList.java +++ b/src/algorithmsAndDataStructures/MyArrayList.java @@ -41,7 +41,6 @@ private void checkArray(E item) { size++; } - // Проверка на переполненность не делается но массив как будто сам увеличивается не могу понять почему так) public void add(int index, E item) { if (index >= 0 & index <= size) { for (int i = size; i > index; i--) { From e4adcd40b4c53d56ecfcf5e32d1de66d7a4dbd12 Mon Sep 17 00:00:00 2001 From: sergeynikitin Date: Thu, 1 Jul 2021 12:24:00 +0300 Subject: [PATCH 4/7] Create MyArrayList and MyArraySortedList; --- src/algorithmsAndDataStructures/MyArrayList.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/algorithmsAndDataStructures/MyArrayList.java b/src/algorithmsAndDataStructures/MyArrayList.java index 52adcd4..bc905c6 100644 --- a/src/algorithmsAndDataStructures/MyArrayList.java +++ b/src/algorithmsAndDataStructures/MyArrayList.java @@ -1,6 +1,7 @@ package algorithmsAndDataStructures; import java.util.Arrays; +import java.util.NoSuchElementException; public class MyArrayList> { private E[] list; @@ -48,7 +49,7 @@ public void add(int index, E item) { } list[index] = item; size++; - } else throw new ArrayIndexOutOfBoundsException("index " + index); + } else throw new ArrayIndexOutOfBoundsException("index" + index + "is not correct "); } public boolean remove(E item) { @@ -61,7 +62,7 @@ public boolean remove(E item) { public void removeIndex(int index) { if (isEmpty()) { - throw new IndexOutOfBoundsException(); + throw new NoSuchElementException("Array is empty"); } if (index >= 0 & index <= size) { for (int i = index; i < size; i++) { @@ -69,7 +70,7 @@ public void removeIndex(int index) { } size--; list[size] = null; - } + } else throw new ArrayIndexOutOfBoundsException("index" + index + "is not correct "); } public boolean isEmpty() { @@ -79,7 +80,7 @@ public boolean isEmpty() { public E get(int index) { if (index >= 0 & index < size) { return list[index]; - } else throw new IndexOutOfBoundsException("no index"); + } else throw new ArrayIndexOutOfBoundsException("index" + index + "is not correct "); } public int indexOf(E item) { From e7589dba7fdd7268b0f6a5ea60665de5d1ae2260 Mon Sep 17 00:00:00 2001 From: sergeynikitin Date: Thu, 1 Jul 2021 14:42:41 +0300 Subject: [PATCH 5/7] Create MyArrayList and MyArraySortedList --- src/algorithmsAndDataStructures/Main.java | 7 +++++-- src/algorithmsAndDataStructures/MyArrayList.java | 16 ++++++++++------ 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/algorithmsAndDataStructures/Main.java b/src/algorithmsAndDataStructures/Main.java index 6e42766..bea3942 100644 --- a/src/algorithmsAndDataStructures/Main.java +++ b/src/algorithmsAndDataStructures/Main.java @@ -50,10 +50,13 @@ public static void doTask() { list.add(8); list.add(9); list.add(10); + + list.add(10, 66); + list.add(5, 66); + list.add(6, 66); list.add(10, 66); list.add(11, 66); - list.add(3, 66); - + list.add(8, 66); System.out.println(list); } } \ No newline at end of file diff --git a/src/algorithmsAndDataStructures/MyArrayList.java b/src/algorithmsAndDataStructures/MyArrayList.java index bc905c6..8e5f2fc 100644 --- a/src/algorithmsAndDataStructures/MyArrayList.java +++ b/src/algorithmsAndDataStructures/MyArrayList.java @@ -20,6 +20,7 @@ public MyArrayList(int capacity) { public MyArrayList() { list = (E[]) new Comparable[DEFAULT_CAPACITY]; + capacity = DEFAULT_CAPACITY; } public int size() { @@ -27,7 +28,8 @@ public int size() { } public void add(E item) { - if (capacity < size) { +// capacity = DEFAULT_CAPACITY; + if (capacity > size) { list[size] = item; size++; } else checkArray(item); @@ -44,11 +46,13 @@ private void checkArray(E item) { public void add(int index, E item) { if (index >= 0 & index <= size) { - for (int i = size; i > index; i--) { - list[i] = list[i - 1]; - } - list[index] = item; - size++; + if (capacity > size) { + for (int i = size; i > index; i--) { + list[i] = list[i - 1]; + } + list[index] = item; + size++; + } else checkArray(item); } else throw new ArrayIndexOutOfBoundsException("index" + index + "is not correct "); } From 19913f4cb868d296c9dde06615eb19836ed9ce24 Mon Sep 17 00:00:00 2001 From: sergeynikitin Date: Fri, 2 Jul 2021 11:52:19 +0300 Subject: [PATCH 6/7] new commit --- src/algorithmsAndDataStructures/Main.java | 3 +-- src/algorithmsAndDataStructures/MyArrayList.java | 9 +++------ 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/algorithmsAndDataStructures/Main.java b/src/algorithmsAndDataStructures/Main.java index bea3942..1df1fa0 100644 --- a/src/algorithmsAndDataStructures/Main.java +++ b/src/algorithmsAndDataStructures/Main.java @@ -15,8 +15,7 @@ public static void main(String[] args) { } System.out.println(System.currentTimeMillis() - startTimeCreate); selectionSort(mal); - insertionSort(mal); - bubbleSort(mal); + } public static void selectionSort(MyArrayList mal) { diff --git a/src/algorithmsAndDataStructures/MyArrayList.java b/src/algorithmsAndDataStructures/MyArrayList.java index 8e5f2fc..bc77b31 100644 --- a/src/algorithmsAndDataStructures/MyArrayList.java +++ b/src/algorithmsAndDataStructures/MyArrayList.java @@ -28,20 +28,17 @@ public int size() { } public void add(E item) { -// capacity = DEFAULT_CAPACITY; if (capacity > size) { list[size] = item; size++; - } else checkArray(item); + } else checkArraySize(); } - private void checkArray(E item) { + private void checkArraySize() { capacity = capacity + DEFAULT_CAPACITY / 2 + 1; E[] newList = (E[]) new Comparable[capacity]; System.arraycopy(list, 0, newList, 0, size); list = newList; - list[size] = item; - size++; } public void add(int index, E item) { @@ -52,7 +49,7 @@ public void add(int index, E item) { } list[index] = item; size++; - } else checkArray(item); + } else checkArraySize(); } else throw new ArrayIndexOutOfBoundsException("index" + index + "is not correct "); } From 8289a1cfc9b55be023ece2df8c6cadaf3051da08 Mon Sep 17 00:00:00 2001 From: sergeynikitin Date: Wed, 21 Jul 2021 17:15:11 +0300 Subject: [PATCH 7/7] Rename branch --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 71abc2a..3e9d785 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,6 @@ # AlgorithmsAndDataStructures Create ArrayList and SortedArrayList, implemented sorting: selection, inserts, bubble. +1. Создать массив большого размера (100000 элементов). +2. Заполнить массив случайными числами. +3. Написать методы, реализующие рассмотренные виды сортировок, + и проверить скорость выполнения каждой. \ No newline at end of file