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
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# AlgorithmsAndDataStructures
Create ArrayList and SortedArrayList, implemented sorting: selection, inserts, bubble.
1. Создать массив большого размера (100000 элементов).
2. Заполнить массив случайными числами.
3. Написать методы, реализующие рассмотренные виды сортировок,
и проверить скорость выполнения каждой.
57 changes: 55 additions & 2 deletions src/algorithmsAndDataStructures/Main.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,61 @@
package algorithmsAndDataStructures;

import java.util.Random;

public class Main {

public static void main(String[] args) {
// write your code here
doTask();

long startTimeCreate = System.currentTimeMillis();
Random random = new Random();
MyArrayList<Integer> mal = new MyArrayList<>(100000);
for (int i = 0; i < 100000; i++) {
mal.add(random.nextInt(100000));
}
System.out.println(System.currentTimeMillis() - startTimeCreate);
selectionSort(mal);

}

public static void selectionSort(MyArrayList<Integer> mal) {
long startTimeSel = System.currentTimeMillis();
mal.selectionSort();
System.out.printf("selectionSort: %s ", System.currentTimeMillis() - startTimeSel);
}

public static void insertionSort(MyArrayList<Integer> mal) {
long startTimeIns = System.currentTimeMillis();
mal.insertionSort();
System.out.printf("insertionSort: %s ", System.currentTimeMillis() - startTimeIns);
}

public static void bubbleSort(MyArrayList<Integer> mal) {
long startTimeBub = System.currentTimeMillis();
mal.bubbleSort();
System.out.printf("bubbleSort: %s", System.currentTimeMillis() - startTimeBub);
}


public static void doTask() {
MyArrayList<Integer> 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(10, 66);
list.add(5, 66);
list.add(6, 66);
list.add(10, 66);
list.add(11, 66);
list.add(8, 66);
System.out.println(list);
}
}
}
154 changes: 154 additions & 0 deletions src/algorithmsAndDataStructures/MyArrayList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package algorithmsAndDataStructures;

import java.util.Arrays;
import java.util.NoSuchElementException;

public class MyArrayList<E extends Comparable<E>> {
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];
capacity = DEFAULT_CAPACITY;
}

public int size() {
return size;
}

public void add(E item) {
if (capacity > size) {
list[size] = item;
size++;
} else checkArraySize();
}

private void checkArraySize() {
capacity = capacity + DEFAULT_CAPACITY / 2 + 1;
E[] newList = (E[]) new Comparable[capacity];
System.arraycopy(list, 0, newList, 0, size);
list = newList;
}

public void add(int index, E item) {
if (index >= 0 & index <= size) {
if (capacity > size) {
for (int i = size; i > index; i--) {
list[i] = list[i - 1];
}
list[index] = item;
size++;
} else checkArraySize();
} else throw new ArrayIndexOutOfBoundsException("index" + index + "is not correct ");
}

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 NoSuchElementException("Array is empty");
}
if (index >= 0 & index <= size) {
for (int i = index; i < size; i++) {
list[i] = list[i + 1];
}
size--;
list[size] = null;
} else throw new ArrayIndexOutOfBoundsException("index" + index + "is not correct ");
}

public boolean isEmpty() {
return size == 0;
}

public E get(int index) {
if (index >= 0 & index < size) {
return list[index];
} else throw new ArrayIndexOutOfBoundsException("index" + index + "is not correct ");
}

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);
}
}
}
}
}
32 changes: 32 additions & 0 deletions src/algorithmsAndDataStructures/MyArraySortedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package algorithmsAndDataStructures;

public class MyArraySortedList<E extends Comparable<E>> extends MyArrayList<E> {
@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;
}
}