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
Binary file added result.xls
Binary file not shown.
50 changes: 44 additions & 6 deletions src/ru/mail/polis/bench/AverageTimeBench.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import ru.mail.polis.sort.BubbleSort;
import ru.mail.polis.sort.Helper;
import ru.mail.polis.sort.*;

/**
* Created by Nechaev Mikhail
Expand All @@ -41,10 +40,9 @@ public class AverageTimeBench {

@Setup(value = Level.Trial)
public void setUpTrial() {
data = new int[10][100];
data = new int[10][];
for (int i = 0; i < 10; i++) {
//define arrays here
data[i] = Helper.gen(100);
data[i] = Helper.genSortedDESC(1000);
}
}

Expand All @@ -54,11 +52,51 @@ public void setUpInvocation() {
index = (index + 1) % 10;
}

@Benchmark
/*@Benchmark
public void measureBubbleSort() {
BubbleSort.sort(curr);
}

@Benchmark
public void measureInsertionSort() {
InsertionSort.sort(curr);
}

@Benchmark
public void measureImprovedInsertionSort() {
ImprovedInsertionSort.sort(curr);
}

@Benchmark
public void measureShellSort() {
ShellSort.sort(curr);
}*/

@Benchmark
public void measureMergeSort() {
MergeSort.sort(curr);
}

@Benchmark
public void measureMemoryOptimisedMergeSort() {
MemoryOptimisedMergeSort.sort(curr);
}

@Benchmark
public void measureQuickSort() {
QuickSort.sort(curr);
}

@Benchmark
public void measureRandomPivot3PartQuickSort() {
MemoryOptimisedMergeSort.sort(curr);
}

@Benchmark
public void measureBinaryQuickSort() {
BinaryQuickSort.sort(curr);
}

public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(AverageTimeBench.class.getSimpleName())
Expand Down
49 changes: 49 additions & 0 deletions src/ru/mail/polis/bench/ImprovedInsertionSortBench.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package ru.mail.polis.bench;

import java.util.concurrent.TimeUnit;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import ru.mail.polis.sort.ImprovedInsertionSort;
import ru.mail.polis.sort.Helper;

@State(Scope.Thread)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class ImprovedInsertionSortBench {

private int[] a;

@Setup(value = Level.Invocation)
public void setUpInvocation() {
a = Helper.gen(1000);
}

@Benchmark
public void measureImprovedInsertionSort(Blackhole bh) {
bh.consume(ImprovedInsertionSort.sort(a));
}

public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(ImprovedInsertionSortBench.class.getSimpleName())
.warmupIterations(5)
.measurementIterations(5)
.forks(1)
.build();

new Runner(opt).run();
}
}
49 changes: 49 additions & 0 deletions src/ru/mail/polis/bench/InsertionSortBench.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package ru.mail.polis.bench;

import java.util.concurrent.TimeUnit;

import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Level;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.Setup;
import org.openjdk.jmh.annotations.State;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import ru.mail.polis.sort.InsertionSort;
import ru.mail.polis.sort.Helper;

@State(Scope.Thread)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class InsertionSortBench {

private int[] a;

@Setup(value = Level.Invocation)
public void setUpInvocation() {
a = Helper.gen(1000);
}

@Benchmark
public void measureInsertionSort(Blackhole bh) {
bh.consume(InsertionSort.sort(a));
}

public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(InsertionSortBench.class.getSimpleName())
.warmupIterations(5)
.measurementIterations(5)
.forks(1)
.build();

new Runner(opt).run();
}
}
46 changes: 46 additions & 0 deletions src/ru/mail/polis/sort/BinaryQuickSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package ru.mail.polis.sort;

public class BinaryQuickSort {

private static final int INT_BINARY_LENGTH = 31;

public static int[] sort(int[] a) {
if (a == null)
return new int[0];

if (a.length == 0 || a.length == 1)
return a;

performSort(a, 0, a.length - 1, INT_BINARY_LENGTH);

return a;
}

public static int[] performSort(int[] arr, int l, int r, int bit){
if (l < r && !(bit < 0)) {
int index = partition(arr, l, r, bit);

performSort(arr, l, index, bit - 1);
performSort(arr, index + 1, r, bit - 1);
}

return arr;
}

static int getBit(int n, int k) {
return (n >> k) & 1;
}

public static int partition(int[] a, int l, int r, int bit){
int i = l;
int j = r;

while (i <= j) {
while (i < a.length && getBit(a[i], bit) == 0) i++;
while (j > -1 && getBit(a[j], bit) == 1) j--;
if (i <= j) Helper.swap(a, i++, j--);
}

return j;
}
}
3 changes: 3 additions & 0 deletions src/ru/mail/polis/sort/BubbleSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
public class BubbleSort {

public static int[] sort(int a[]) {
if (a == null)
return new int[0];

boolean wasSwap = true;
int j = 0;
while (wasSwap) {
Expand Down
88 changes: 88 additions & 0 deletions src/ru/mail/polis/sort/Helper.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ru.mail.polis.sort;

import java.util.Random;
import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;

public class Helper {
Expand All @@ -24,4 +25,91 @@ public static int[] gen(int n) {
}
return a;
}

public static int[] genSortedASC(int n) {
int[] a = Helper.gen(n);
Arrays.sort(a);
return a;
}

public static int[] genSortedDESC(int n) {
int[] a = Helper.genSortedASC(n);

for (int i = 0; i < a.length / 2; i++) {
Helper.swap(a, i, a.length - i - 1);
}

return a;
}

public static int[] genWorstCaseForMergeSort(int n) {
int[] a = Helper.genSortedASC(n);
Helper.worseCaseForMergeSortSeparate(a);
return a;
}

public static void worseCaseForMergeSortSeparate(int[] a) {
if(a.length <= 1)
return;

if(a.length == 2)
{
int swap = a[0];
a[0] = a[1];
a[1] = swap;
return;
}

int i, j;
int m = (a.length + 1) / 2;
int left[] = new int[m];
int right[] = new int[a.length - m];

for(i = 0, j = 0 ;i < a.length; i = i + 2, j++)
left[j]=a[i];

for(i = 1, j = 0; i < a.length; i = i + 2, j++)
right[j] = a[i];

Helper.worseCaseForMergeSortSeparate(left);
Helper.worseCaseForMergeSortSeparate(right);
Helper.worseCaseForMergeSortMerge(a, left, right);
}

public static void worseCaseForMergeSortMerge(int[] a, int[] left, int[] right) {
int i, j;
for(i = 0; i < left.length; i++) {
a[i] = left[i];
}
for(j = 0; j < right.length; j++, i++) {
a[i] = right[j];
}
}

public static int[] genWorstCaseForQuickSort(int n){
int[] a = Helper.genSortedASC(n);
worstCaseForQuickSort(a);
return a;
}

public static void worstCaseForQuickSort(int[] a)
{
for (int i = 2; i < a.length;i++)
swap(a, i, i / 2);
}

public static int binarySearch(int[] a, int key, int right) {
int left = -1;
int mid;
while (left < right - 1) {
mid = left + (right - left) / 2;

if (a[mid] < key) {
left = mid;
} else {
right = mid;
}
}
return right;
}
}
17 changes: 17 additions & 0 deletions src/ru/mail/polis/sort/ImprovedInsertionSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package ru.mail.polis.sort;

public class ImprovedInsertionSort {

public static int[] sort(int a[]) {
if (a == null)
return new int[0];

for(int i = 0; i < a.length - 1; i++){
int p = Helper.binarySearch(a, a[i], i);
int aP = a[i];
System.arraycopy(a, p, a, p + 1, i - p);
a[p] = aP;
}
return a;
}
}
24 changes: 24 additions & 0 deletions src/ru/mail/polis/sort/InsertionSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ru.mail.polis.sort;

public class InsertionSort {
/*public static void main(String[] args) {
int [] a = {5, 6, 1, 7, 9};

InsertionSort.sort(a);
for(int i = 0; i < a.length; i++){
System.out.print(a[i] + " ");
}
}*/

public static int[] sort(int a[]) {
if (a == null)
return new int[0];

for(int i = 0; i < a.length; i++){
for(int j = i; j > 0 && a[j] < a[j - 1]; j--){
Helper.swap(a, j, j - 1);
}
}
return a;
}
}
Loading