0003_Third push added[several sortingalgorithms]
This commit is contained in:
26
Main.java
26
Main.java
@@ -1,10 +1,30 @@
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
//Test MyList
|
||||
MyList list = new MyList();
|
||||
list.add(new Pair(2,2));
|
||||
list.add(new MyPair(2,2));
|
||||
list.remove(1);
|
||||
list.insert(new Pair(3,3), 0);
|
||||
|
||||
//list.insert(new MyPair(3,3), 0);
|
||||
System.out.println(list.toString());
|
||||
|
||||
//Test BubbleSortInSitu
|
||||
MyPair[] arrPairs = {new MyPair(2,2), new MyPair(0,0), new MyPair(1,1), new MyPair(3,3)};
|
||||
sort_algorithms.myBubbleSortInSitu(arrPairs);
|
||||
System.out.println(arrPairs[0].toString() + arrPairs[1].toString() + arrPairs[2].toString() + arrPairs[3].toString());
|
||||
|
||||
//Test BubbleSortNotInSitu
|
||||
MyPair[] arrPairs2 = {new MyPair(2,2), new MyPair(0,0), new MyPair(1,1), new MyPair(3,3)};
|
||||
MyPair[] arrPairs3 = sort_algorithms.myBubbleSortNotInSitu(arrPairs2);
|
||||
System.out.println(arrPairs3[0].toString() + arrPairs3[1].toString() + arrPairs3[2].toString() + arrPairs3[3].toString());
|
||||
System.out.println(arrPairs2[0].toString() + arrPairs2[1].toString() + arrPairs2[2].toString() + arrPairs2[3].toString());
|
||||
|
||||
//Test SelectionSortInSitu
|
||||
MyPair[] arrPairs4 = {new MyPair(2,2), new MyPair(0,0), new MyPair(1,1), new MyPair(3,3)};
|
||||
sort_algorithms.mySelectionSortInSitu(arrPairs4);
|
||||
System.out.println(arrPairs4[0].toString() + arrPairs4[1].toString() + arrPairs4[2].toString() + arrPairs4[3].toString());
|
||||
|
||||
//
|
||||
sort_algorithms.myInsertionSortInSitu(arrPairs2);
|
||||
System.out.println(arrPairs2[0].toString() + arrPairs2[1].toString() + arrPairs2[2].toString() + arrPairs2[3].toString());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,7 +6,7 @@ public class MyList {
|
||||
}
|
||||
|
||||
//Adds elem to list.
|
||||
public void add(Pair pair) {
|
||||
public void add(MyPair pair) {
|
||||
MyListElement elem = new MyListElement(pair, null);
|
||||
//Case 1: List is empty
|
||||
if (this.first == null) {
|
||||
@@ -22,7 +22,7 @@ public class MyList {
|
||||
}
|
||||
|
||||
//Inserts elem at index.
|
||||
public void insert(Pair pair, int index) {
|
||||
public void insert(MyPair pair, int index) {
|
||||
MyListElement node = this.first;
|
||||
MyListElement pre = this.first;
|
||||
if (index == 0) {
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
public class MyListElement {
|
||||
public Pair data;
|
||||
public MyPair data;
|
||||
public MyListElement next;
|
||||
|
||||
public MyListElement() {
|
||||
@@ -7,7 +7,7 @@ public class MyListElement {
|
||||
next = null;
|
||||
}
|
||||
|
||||
public MyListElement(Pair data, MyListElement next) {
|
||||
public MyListElement(MyPair data, MyListElement next) {
|
||||
this.data = data;
|
||||
this.next = next;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
public class Pair {
|
||||
public class MyPair {
|
||||
public Object key;
|
||||
public Object data;
|
||||
|
||||
public Pair() {
|
||||
public MyPair() {
|
||||
key = null;
|
||||
data = null;
|
||||
}
|
||||
|
||||
public Pair(Object key, Object data) {
|
||||
public MyPair(Object key, Object data) {
|
||||
this.key = key;
|
||||
this.data = data;
|
||||
}
|
||||
@@ -16,4 +16,8 @@ public class Pair {
|
||||
public String toString() {
|
||||
return ("(" + this.key.toString() + "," + this.data.toString() + ")");
|
||||
}
|
||||
|
||||
public int getKeyAsInt() {
|
||||
return (int)this.key;
|
||||
}
|
||||
}
|
||||
@@ -3,8 +3,8 @@ import java.util.List;
|
||||
public class search_algorithms {
|
||||
|
||||
//Data doesnt have to be sorted.
|
||||
public static Pair sequentielSearch(Object oKey, List<Pair> oList) {
|
||||
Pair oData = null;
|
||||
public static MyPair sequentielSearch(Object oKey, List<MyPair> oList) {
|
||||
MyPair oData = null;
|
||||
for (int i = 0; i < oList.size(); i++) {
|
||||
if (oList.get(i).key == oKey) {
|
||||
oData = oList.get(i);
|
||||
@@ -15,14 +15,14 @@ public class search_algorithms {
|
||||
}
|
||||
|
||||
//Data has to be sorted by keys > < =.
|
||||
public static Pair binarySearch(Object oKey, Pair[] oArray) {
|
||||
Pair oData = null;
|
||||
public static MyPair binarySearch(Object oKey, MyPair[] oArray) {
|
||||
MyPair oData = null;
|
||||
oData = helpBinarySearch(oKey, 0, (oArray.length - 1), oArray);
|
||||
return oData;
|
||||
}
|
||||
|
||||
private static Pair helpBinarySearch(Object oKey, int iLeft, int iRight, Pair[] oArray) {
|
||||
Pair oData = null;
|
||||
private static MyPair helpBinarySearch(Object oKey, int iLeft, int iRight, MyPair[] oArray) {
|
||||
MyPair oData = null;
|
||||
int m = (int)(iLeft + iRight) / 2;
|
||||
if (m > (int)oKey) {
|
||||
oData = helpBinarySearch(oKey, iLeft, (m - 1), oArray);
|
||||
@@ -35,9 +35,9 @@ public class search_algorithms {
|
||||
}
|
||||
|
||||
//Data has to be sorted by keys > < =.
|
||||
public static Pair exponentialSearch(Object oKey, Pair[] oArray) {
|
||||
public static MyPair exponentialSearch(Object oKey, MyPair[] oArray) {
|
||||
boolean bSuccess = false;
|
||||
Pair oData = null;
|
||||
MyPair oData = null;
|
||||
int iL = 0;
|
||||
int iR = 0;
|
||||
for (int i = 0; i < oArray.length; i++) {
|
||||
|
||||
@@ -1,3 +1,135 @@
|
||||
public class sort_algorithms {
|
||||
|
||||
/**
|
||||
* BubbleSort in-situ
|
||||
* Bubbles with every loop the largest element to the end, area where elements are looked at decreases by 1 each time
|
||||
* so largest will not be looked at anymore.
|
||||
*
|
||||
* Example:
|
||||
* 0, 2, 4, 7, 3, 1
|
||||
* 0, 2, 4, 3, 1, 7
|
||||
* 0, 2, 3, 1, 4, 7
|
||||
* 0, 2, 1, 3, 4, 7
|
||||
* 0, 1, 2, 3, 4, 7
|
||||
*
|
||||
* @param arr
|
||||
*/
|
||||
public static void myBubbleSortInSitu(MyPair[] arr) {
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
for (int j = 0; (j + 1) < arr.length - i; j++) {
|
||||
if (arr[j].getKeyAsInt() > arr[j + 1].getKeyAsInt()) {
|
||||
MyPair store = arr[j];
|
||||
arr[j] = arr[j + 1];
|
||||
arr[j + 1] = store;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* BubbleSort not in-situ
|
||||
* @param arr
|
||||
*/
|
||||
public static MyPair[] myBubbleSortNotInSitu(MyPair[] arr) {
|
||||
//Has to create new array and copy data
|
||||
MyPair[] sortedSequence = new MyPair[arr.length];
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
sortedSequence[i] = arr[i];
|
||||
}
|
||||
//Sorting starts here
|
||||
for (int i = 0; i < sortedSequence.length; i++) {
|
||||
for (int j = 0; (j + 1) < sortedSequence.length - i; j++) {
|
||||
if (sortedSequence[j].getKeyAsInt() > sortedSequence[j + 1].getKeyAsInt()) {
|
||||
MyPair store = sortedSequence[j];
|
||||
sortedSequence[j] = sortedSequence[j + 1];
|
||||
sortedSequence[j + 1] = store;
|
||||
}
|
||||
}
|
||||
}
|
||||
return sortedSequence;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param arr
|
||||
*/
|
||||
public static void mySelectionSortInSitu(MyPair[] arr) {
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
int smallestIndex = arr.length - 1;
|
||||
int smallestKey = arr[arr.length - 1].getKeyAsInt();
|
||||
for (int j = (arr.length - 1); j >= i; j--) {
|
||||
if (arr[j].getKeyAsInt() < smallestKey) {
|
||||
smallestIndex = j;
|
||||
smallestKey = arr[j].getKeyAsInt();
|
||||
}
|
||||
}
|
||||
MyPair store = arr[i];
|
||||
arr[i] = arr[smallestIndex];
|
||||
arr[smallestIndex] = store;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param arr
|
||||
* @return
|
||||
*/
|
||||
public static MyPair[] mySelectionSortNotInSitu(MyPair[] arr) {
|
||||
//Has to create new array and copy data
|
||||
MyPair[] sortedSequence = new MyPair[arr.length];
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
sortedSequence[i] = arr[i];
|
||||
}
|
||||
//Sorting starts here
|
||||
for (int i = 0; i < sortedSequence.length; i++) {
|
||||
int smallestIndex = sortedSequence.length - 1;
|
||||
int smallestKey = sortedSequence[sortedSequence.length - 1].getKeyAsInt();
|
||||
for (int j = (sortedSequence.length - 1); j >= i; j--) {
|
||||
if (sortedSequence[j].getKeyAsInt() < smallestKey) {
|
||||
smallestIndex = j;
|
||||
smallestKey = sortedSequence[j].getKeyAsInt();
|
||||
}
|
||||
}
|
||||
MyPair store = sortedSequence[i];
|
||||
sortedSequence[i] = sortedSequence[smallestIndex];
|
||||
sortedSequence[smallestIndex] = store;
|
||||
}
|
||||
return sortedSequence;
|
||||
}
|
||||
|
||||
/**
|
||||
* 7, 4, 2, 0, 1, 3
|
||||
* 7 4, 2, 0, 1, 3; i = 0; j = 0
|
||||
* 4, 7 2, 0, 1, 3; i = 1; j = 0, j = 1;
|
||||
* 2, 4, 7 0, 1, 3
|
||||
* 0, 2, 4, 7 1, 3
|
||||
* 0, 1, 2, 4, 7 3
|
||||
* 0, 1, 2, 3, 4, 7
|
||||
* TODO
|
||||
* @param arr
|
||||
*/
|
||||
public static void myInsertionSortInSitu(MyPair[] arr) {
|
||||
for (int i = 0; i < arr.length; i++) {
|
||||
int index = 0;
|
||||
for (int j = 0; j <= i; j++) {
|
||||
if (arr[j].getKeyAsInt() >= arr[i].getKeyAsInt()) {
|
||||
index = j;
|
||||
}
|
||||
}
|
||||
MyPair store = arr[index];
|
||||
arr[index] = arr[i];
|
||||
arr[i] = store;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* TODO
|
||||
* @param arr
|
||||
* @return
|
||||
*/
|
||||
public static MyPair[] myInsertionSortNotInSitu(MyPair[] arr) {
|
||||
return new MyPair[2];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user