Sie sind auf Seite 1von 9

Assignment 2

Task 1

Test SelectionSort algorithm 100 data

Test SelectionSort algorithm 1000 data

1
Test SelectionSort algorithm 100000 data

2
Task 2

Implementation code of InsertionSorter

import tk3043.profiler.sorter.Sorter;
import tk3043.profiler.Profiler;

/**
*
* @author sufian
*/
public class InsertionSorter implements Sorter {

public void sort(Object[] items){


System.out.println("Sorting using InsertionSort...");
Profiler.startTiming(1);

int qty = items.length;


int in;

for (int out=1; out < qty; out++) {


Object temp = items[out];
in = out;
while(in>0 && (Integer)items[in-1]>=(Integer)temp)
{
items[in] = items[in-1];
--in;
}
items[in] = temp;
}

long time = Profiler.stopTiming(1);


System.out.println("Sorting done... Time: "+time+"ms");
}

public boolean isLessThan(Object item1, Object item2) {


return (Integer) item1 < (Integer) item2;
}

3
Test InsertionSort algorithm 100 data

Test InsertionSort algorithm 1000 data

4
Test InsertionSort algorithm 100000 data

5
Task 3

Implementation code of MergeSorter

import tk3043.profiler.sorter.Sorter;
import tk3043.profiler.Profiler;

/**
*
* @author sufian
*/
public class MergeSorter implements Sorter {

private Object[] numbers;


private int number;

public void sort(Object[] items) {


System.out.println("Sorting using Mergesort");
Profiler.startTiming(1);

this.numbers = items;
number = items.length;
mergesort(0, number-1);

long time = Profiler.stopTiming(1);


System.out.println("Sorting done... Time: "+time+"ms");
}

private void mergesort(int low, int high) {


if (low < high){
int middle = (low + high) / 2;
mergesort(low, middle);
mergesort(middle + 1, high);
merge(low, middle, high);
}
}

private void merge(int low, int middle, int high) {

Object[] helper = new Object[number];

for (int a = low; a <= high; a++) helper[a] = numbers[a];

int a = low;
int b = middle + 1;
int c = low;

while (a <= middle && b <= high) {


if ((Integer)helper[a] <= (Integer)helper[b]) numbers[c++] =
helper[i++];
else numbers[c++] = helper[b++];
}

while (a <= middle) numbers[c++] = helper[a++];


helper = null;
}

public boolean isLessThan(Object item1, Object item2) {


return (Integer) item1 < (Integer) item2;
}

6
Test MergeSort algorithm 100 data

Test MergeSort algorithm 1000 data

7
Test MergeSort algorithm 100000 data

8
Task 4

Compare the results produced

Selection Sort Insection Sort Merge Sort

10 0 ms 0 ms 0 ms

1000 0 ms 5 ms 3 ms

100000 27172 ms 24929 ms 10395 ms

Are they consistent with the results obtained from analyzing the algorithms
theoretically as discussed during the lectures?

Yes

Selection sort is a sorting algorithm, specifically an in-place comparison sort. It has


O(n2) complexity, making it inefficient on large lists, and generally performs worse
than the similar insertion sort. Selection sort is noted for its simplicity, and also has
performance advantages over more complicated algorithms in certain situations.

The algorithm finds the minimum value, swaps it with the value in the first position,
and repeats these steps for the remainder of the list. It does no more than n swaps,
and thus is useful where swapping is very expensive.

Insertion sort is a simple sorting algorithm that is relatively efficient for small lists
and mostly-sorted lists, and often is used as part of more sophisticated algorithms. It
works by taking elements from the list one by one and inserting them in their correct
position into a new sorted list. In arrays, the new list and the remaining elements
can share the array's space, but insertion is expensive, requiring shifting all following
elements over by one

Merge sort takes advantage of the ease of merging already sorted lists into a new
sorted list. It starts by comparing every two elements (i.e., 1 with 2, then 3 with
4...) and swapping them if the first should come after the second. It then merges
each of the resulting lists of two into lists of four, then merges those lists of four, and
so on; until at last two lists are merged into the final sorted list. Of the algorithms
described here, this is the first that scales well to very large lists, because its worst-
case running time is O(n log n).

Das könnte Ihnen auch gefallen