diff --git a/Lesson3/Algorithms/sort/insertion/.gitignore b/Lesson3/Algorithms/sort/insertion/.gitignore new file mode 100644 index 0000000..8281022 --- /dev/null +++ b/Lesson3/Algorithms/sort/insertion/.gitignore @@ -0,0 +1,76 @@ +############################## +## Java +############################## +.mtj.tmp/ +*.class +*.jar +*.war +*.ear +*.nar +hs_err_pid* + +############################## +## Maven +############################## +target/ +pom.xml.tag +pom.xml.releaseBackup +pom.xml.versionsBackup +pom.xml.next +pom.xml.bak +release.properties +dependency-reduced-pom.xml +buildNumber.properties +.mvn/timing.properties +.mvn/wrapper/maven-wrapper.jar + +############################## +## Gradle +############################## +bin/ +build/ +.gradle +.gradletasknamecache +gradle-app.setting +!gradle-wrapper.jar + +############################## +## IntelliJ +############################## +out/ +.idea/ +.idea_modules/ +*.iml +*.ipr +*.iws + +############################## +## Eclipse +############################## +.settings/ +tmp/ +.metadata +.classpath +.project +*.tmp +*.bak +*.swp +*~.nib +local.properties +.loadpath +.factorypath + +############################## +## NetBeans +############################## +nbproject/private/ +nbbuild/ +dist/ +nbdist/ +nbactions.xml +nb-configuration.xml + +############################## +## OS X +############################## +.DS_Store diff --git a/Lesson3/Algorithms/sort/insertion/src/algorithms/eratosfen/Eratosfen.java b/Lesson3/Algorithms/sort/insertion/src/algorithms/eratosfen/Eratosfen.java new file mode 100644 index 0000000..4fa0831 --- /dev/null +++ b/Lesson3/Algorithms/sort/insertion/src/algorithms/eratosfen/Eratosfen.java @@ -0,0 +1,36 @@ +package algorithms.eratosfen; + +import java.util.Arrays; + +public class Eratosfen { + + public boolean[] primes; + + public Eratosfen(int range) { + primes = new boolean[range + 1]; + } + + public void fillSieve() { + Arrays.fill(primes, true); + + primes[0] = false; + primes[1] = false; + + for (int i = 2; i < primes.length; ++i) { + if (primes[i]) { + for (int j = 2; i * j < primes.length; ++j) { + primes[i * j] = false; + } + } + } + } + + public void displayAllNumbers() { + fillSieve(); + for (int i = 0; i < primes.length; i++) { + if (primes[i]) { + System.out.print(i + " "); + } + } + } +} diff --git a/Lesson3/Algorithms/sort/insertion/src/algorithms/fibonacci/FibonacciSequence.java b/Lesson3/Algorithms/sort/insertion/src/algorithms/fibonacci/FibonacciSequence.java new file mode 100644 index 0000000..57fd016 --- /dev/null +++ b/Lesson3/Algorithms/sort/insertion/src/algorithms/fibonacci/FibonacciSequence.java @@ -0,0 +1,14 @@ +package algorithms.fibonacci; + +public class FibonacciSequence { + + public int[] calculatingFibonacciNumbers(int quantity) { + int[] fib = new int[quantity]; + fib[0] = 0; + fib[1] = 1; + for (int i = 2; i < quantity; ++i) { + fib[i] = fib[i - 1] + fib[i - 2]; + } + return fib; + } +} diff --git a/Lesson3/Algorithms/sort/insertion/src/algorithms/main.java b/Lesson3/Algorithms/sort/insertion/src/algorithms/main.java new file mode 100644 index 0000000..53e8522 --- /dev/null +++ b/Lesson3/Algorithms/sort/insertion/src/algorithms/main.java @@ -0,0 +1,78 @@ +package algorithms; + +import algorithms.eratosfen.Eratosfen; +import algorithms.fibonacci.FibonacciSequence; +import algorithms.mylinkedlist.twowaylinkedlist.MyTwoWayLinkedList; +import algorithms.mylinkedlist.twowaylinkedlist.MyTwoWayLinkedListImpl; +import algorithms.sort.Sort; + +public class main { + public static void main(String[] args) { + System.out.println("Сортировка методом вставки:"); + checkSortAlgorithm(); + + System.out.println("Последовательность фибоначи:"); + checkFibonacciSequenceAlgorithm(); + + TwoSideMyLinkedListImplTest(); + + Eratosfen eratosfen = new Eratosfen(100); + eratosfen.displayAllNumbers(); + + } + + private static void TwoSideMyLinkedListImplTest() { + MyTwoWayLinkedList myLinkedList = new MyTwoWayLinkedListImpl<>(); + + myLinkedList.insertFirst(18); + myLinkedList.insertFirst(457); + myLinkedList.insertFirst(12); + myLinkedList.insertFirst(54); + myLinkedList.insertLast(6); + + myLinkedList.insertLast(584); + + myLinkedList.display(); + + System.out.println("Contains 18: " + myLinkedList.contains(18)); + System.out.println("Contains 7: " + myLinkedList.contains(7)); + System.out.println("Contains 457: " + myLinkedList.contains(457)); + System.out.println("Contains 24: " + myLinkedList.contains(24)); + System.out.println("Contains 584: " + myLinkedList.contains(584)); + + myLinkedList.display(); + + myLinkedList.removeFirst(); + myLinkedList.remove(6); + + myLinkedList.display(); + } + + private static void checkFibonacciSequenceAlgorithm() { + FibonacciSequence fibonacci = new FibonacciSequence(); + printArray(fibonacci.calculatingFibonacciNumbers(20)); + } + + private static void checkSortAlgorithm() { + int[] array = new int[10]; + Sort sort = new Sort(); + fillArray(array); + printArray(array); + + sort.insertionSort(array); + printArray(array); + } + + private static void fillArray(int[] array) { + for (int i = 0; i < array.length; i++) { + array[i] = (int) Math.round(Math.random() * 100); + } + } + + private static void printArray(int[] array) { + for (int i = 0; i < array.length; i++) { + System.out.print(array[i] + " "); + } + System.out.println(); + } +} diff --git a/Lesson3/Algorithms/sort/insertion/src/algorithms/mylinkedlist/MyIterator.java b/Lesson3/Algorithms/sort/insertion/src/algorithms/mylinkedlist/MyIterator.java new file mode 100644 index 0000000..71b0663 --- /dev/null +++ b/Lesson3/Algorithms/sort/insertion/src/algorithms/mylinkedlist/MyIterator.java @@ -0,0 +1,9 @@ +package algorithms.mylinkedlist; + +import java.util.Iterator; + +public interface MyIterator extends Iterator { + void reset(); + void insertBefore(E value); + void insertAfter(E value); +} diff --git a/Lesson3/Algorithms/sort/insertion/src/algorithms/mylinkedlist/MyLinkedList.java b/Lesson3/Algorithms/sort/insertion/src/algorithms/mylinkedlist/MyLinkedList.java new file mode 100644 index 0000000..e7b19b4 --- /dev/null +++ b/Lesson3/Algorithms/sort/insertion/src/algorithms/mylinkedlist/MyLinkedList.java @@ -0,0 +1,21 @@ +package algorithms.mylinkedlist; + + +public interface MyLinkedList extends Iterable { + void insertFirst(E value); + E removeFirst(); + boolean remove(E value); + boolean contains(E value); + boolean isEmpty(); + int size(); + void display(); + E getFirstElement(); + Entry getFirst(); + static class Entry { + public final E value; + public Entry next; + public Entry(E value) { + this.value = value; + } + } +} diff --git a/Lesson3/Algorithms/sort/insertion/src/algorithms/mylinkedlist/MySimpleLinkedListImpl.java b/Lesson3/Algorithms/sort/insertion/src/algorithms/mylinkedlist/MySimpleLinkedListImpl.java new file mode 100644 index 0000000..855a63e --- /dev/null +++ b/Lesson3/Algorithms/sort/insertion/src/algorithms/mylinkedlist/MySimpleLinkedListImpl.java @@ -0,0 +1,167 @@ +package algorithms.mylinkedlist; + +import java.util.Iterator; + +public class MySimpleLinkedListImpl implements MyLinkedList { + + protected Entry firstElement; + protected int size; + + @Override + public void insertFirst(E value) { + Entry entry = new Entry<>(value); + entry.next = firstElement; + firstElement = entry; + size++; + } + + @Override + public E removeFirst() { + if (isEmpty()){ return null; } + E removedElement = this.firstElement.value; + firstElement = firstElement.next; + size--; + return removedElement; + } + + @Override + public boolean remove(E value) { + Entry previous = null; + Entry current = firstElement; + while (current != null){ + if (current.value.equals(value)){ break; } + previous = current; + current = current.next; + } + if (current == null) { return false; } + if (current == firstElement){ + firstElement = firstElement.next; + } else { + previous.next = current.next; + } + size--; + return true; + } + + @Override + public boolean contains(E value) { + Entry current = firstElement; + while (current != null) { + if (current.value.equals(value)){ return true; } + current = current.next; + } + return false; + } + + @Override + public boolean isEmpty() { + return size == 0; + } + + @Override + public int size() { + return size; + } + + @Override + public void display() { + System.out.println("-----------"); + Entry current = firstElement; + while (current != null) { + System.out.println(current.value); + current = current.next; + } + System.out.println("-----------"); + } + + @Override + public E getFirstElement() { + return firstElement.value; + } + + @Override + public Entry getFirst() { + return firstElement; + } + + @Override + public Iterator iterator() { + return new LinkedListIterator(this); + } + + @SuppressWarnings("unchecked") + private static class LinkedListIterator implements MyIterator { + + private MySimpleLinkedListImpl list; + + private Entry current; + private Entry previous; + + public LinkedListIterator(MySimpleLinkedListImpl list) { + this.list = list; + reset(); + } + + @Override + public boolean hasNext() { + return current != null; + } + + @Override + public E next() { + E nextValue = current.value; + previous = current; + current = current.next; + return nextValue; + } + + @Override + public void remove() { + if (previous == null){ + list.firstElement = current.next; + reset(); + } else { + previous.next = current.next; + if ( !hasNext() ) { + reset(); + } else { + current = current.next; + } + } + } + + @Override + public void reset() { + current = list.firstElement; + previous = null; + } + + @Override + public void insertBefore(E value) { + Entry newItem = new Entry(value); + if(previous == null) { + newItem.next = list.firstElement; + list.firstElement = newItem; + reset(); + } + else { + newItem.next = previous.next; + previous.next = newItem; + current = newItem; + } + + } + @Override + public void insertAfter(E value) { + Entry newItem = new Entry(value); + if (list.isEmpty()){ + list.firstElement = newItem; + current = newItem; + } else { + newItem.next = current.next; + current.next = newItem; + next(); + } + } + } +} diff --git a/Lesson3/Algorithms/sort/insertion/src/algorithms/mylinkedlist/twowaylinkedlist/MyTwoWayLinkedList.java b/Lesson3/Algorithms/sort/insertion/src/algorithms/mylinkedlist/twowaylinkedlist/MyTwoWayLinkedList.java new file mode 100644 index 0000000..2b60d90 --- /dev/null +++ b/Lesson3/Algorithms/sort/insertion/src/algorithms/mylinkedlist/twowaylinkedlist/MyTwoWayLinkedList.java @@ -0,0 +1,7 @@ +package algorithms.mylinkedlist.twowaylinkedlist; + +import algorithms.mylinkedlist.MyLinkedList; + +public interface MyTwoWayLinkedList extends MyLinkedList { + void insertLast(E value); +} diff --git a/Lesson3/Algorithms/sort/insertion/src/algorithms/mylinkedlist/twowaylinkedlist/MyTwoWayLinkedListImpl.java b/Lesson3/Algorithms/sort/insertion/src/algorithms/mylinkedlist/twowaylinkedlist/MyTwoWayLinkedListImpl.java new file mode 100644 index 0000000..17a20f8 --- /dev/null +++ b/Lesson3/Algorithms/sort/insertion/src/algorithms/mylinkedlist/twowaylinkedlist/MyTwoWayLinkedListImpl.java @@ -0,0 +1,53 @@ +package algorithms.mylinkedlist.twowaylinkedlist; + +import algorithms.mylinkedlist.MySimpleLinkedListImpl; + +public class MyTwoWayLinkedListImpl extends MySimpleLinkedListImpl implements MyTwoWayLinkedList { + + private Entry lastElement; + + @Override + public void insertLast(E value) { + Entry entry = new Entry<>(value); + if (isEmpty()) { firstElement = entry; + } else { lastElement.next = entry; } + lastElement = entry; + size++; + } + + @Override + public void insertFirst(E value) { + super.insertFirst(value); + if (size() == 1) { lastElement = firstElement; } + } + + @Override + public E removeFirst() { + E removedElement = super.removeFirst(); + if (isEmpty()) { lastElement = null; } + return removedElement; + } + + @Override + public boolean remove(E value) { + Entry previous = null; + Entry current = firstElement; + while (current != null){ + if (current.value.equals(value)){ break; } + previous = current; + current = current.next; + } + + if (current == null) { return false; } + if (current == firstElement){ + firstElement = firstElement.next; + } else if(current == lastElement) { + lastElement = previous; + previous.next = null; + } else { + previous.next = current.next; + } + size--; + return true; + } +} diff --git a/Lesson3/Algorithms/sort/insertion/src/algorithms/sort/Sort.java b/Lesson3/Algorithms/sort/insertion/src/algorithms/sort/Sort.java new file mode 100644 index 0000000..cf9cfb8 --- /dev/null +++ b/Lesson3/Algorithms/sort/insertion/src/algorithms/sort/Sort.java @@ -0,0 +1,15 @@ +package algorithms.sort; + +public class Sort { + + public void insertionSort(int[] arr) { + int j, temp; + for (int i = 0; i < arr.length; i++) { + temp = arr[i]; + for (j = i-1; j >= 0 && arr[j] > temp; j--) { + arr[j+1] = arr[j]; + } + arr[j+1] = temp; + } + } +}