In this part we will apply what we have learned about algorithms using the example of searching within data structures and sorting lists.
import java.util.List;
// linear search over an unsorted list
List<Integer> l = List.of(1,5,43,23,3,6,13);
int e = 6;
for(Integer i: l)
if (e == i)
return true;
return false;
package lecture;
import java.util.List;
public class BinarySearch {
public static boolean search(List l, int e) {
System.out.println("\n\n----- search for " + e + " ------");
return binSearch(l, e, 0, l.size() - 1);
}
private static boolean binSearch(List<Integer> l, int e, int low, int high) {
int mid = (low + high) / 2;
System.out.printf("searching for %d [%d,%d,%d]\n", e, low, mid, high);
if (low > high)
return false;
if (e == l.get(mid))
return true;
else if (e < l.get(mid))
return binSearch(l, e, low, mid - 1);
else // (e > l.get(mid))
return binSearch(l, e, mid + 1, high);
}
}
import java.util.List;
import lecture.BinarySearch;
List<Integer> l = List.of(1,4,6,7,9,10,13,22,24); // list has to be sorted for binary search
System.out.println("\nIs 1 in list: " + BinarySearch.search(l, 1));
System.out.println("\nIs 4 in list: " + BinarySearch.search(l, 4));
System.out.println("\nIs 5 in list: " + BinarySearch.search(l, 5));
System.out.println("\nIs 7 in list: " + BinarySearch.search(l, 7));
System.out.println("\nIs 9 in list: " + BinarySearch.search(l, 9));
System.out.println("\nIs 10 in list: " + BinarySearch.search(l, 10));
System.out.println("\nIs 13 in list: " + BinarySearch.search(l, 13));
System.out.println("\nIs 22 in list: " + BinarySearch.search(l, 22));
System.out.println("\nIs 24 in list: " + BinarySearch.search(l, 24));
System.out.println("\nIs 0 in list: " + BinarySearch.search(l, 0));
System.out.println("\nIs 11 in list: " + BinarySearch.search(l, 11));
System.out.println("\nIs 50 in list: " + BinarySearch.search(l, 50));
package lecture;
public class QuickSort {
public static void sort(int[] l) {
qsort(l, 0, l.length - 1);
}
private static void qsort(int[] l, int low, int high) {
if (low < high) {
int mid = partition(l, low, high);
qsort(l, low, mid - 1);
qsort(l, mid + 1, high);
}
}
private static void swap(int[] l, int left, int right) {
int temp = l[left];
l[left] = l[right];
l[right] = temp;
}
private static int partition(int[] l, int low, int high) {
int p = l[high]; // choose last element as pivot
while(low < high) {
if (l[low] > p)
swap(l, low, high--);
else
low++;
}
return low;
}
}
package lecture;
public class BubbleSort {
public static void sort(int[] l) {
for(int i = 0; i < l.length - 1; i++) {
for(int j = i+1; j < l.length; j++) {
if (l[j-1] > l[j]) {
int temp = l[j-1];
l[j-1] = l[j];
l[j] = temp;
}
}
}
}
}
package lecture;
public class SelectionSort {
public static void sort(int[] l) {
for(int i = 0; i < l.length - 1; i++) {
int min = l[i];
int pos = i;
for(int j = i+1; j < l.length; j++) {
if(l[j] < min) {
min = l[j];
pos = j;
}
}
swap(l, i, pos);
}
}
private static void swap(int[] l, int left, int right) {
int temp = l[left];
l[left] = l[right];
l[right] = temp;
}
}
import java.util.List;
import java.util.Arrays;
import lecture.QuickSort;
int[] l = { 1,4,24, 3, 9, 7, 100, 65, 64 }; // list has to be sorted for binary search
QuickSort.sort(l);
return Arrays.toString(l);
import java.util.List;
import java.util.Arrays;
import lecture.BubbleSort;
int[] l = { 1,4,24, 3, 9, 7, 100, 65, 64 }; // list has to be sorted for binary search
BubbleSort.sort(l);
return Arrays.toString(l);
import java.util.List;
import java.util.Arrays;
import lecture.SelectionSort;
int[] l = { 1,4,24, 3, 9, 7, 100, 65, 64 }; // list has to be sorted for binary search
SelectionSort.sort(l);
return Arrays.toString(l);
import java.util.Arrays;
import java.util.Random;
import lecture.QuickSort;
import lecture.BubbleSort;
Random r = new Random();
int[] l = new int[100000];
for(int i = 0; i < l.length; i++)
l[i] = r.nextInt();
System.out.println("Quicksort start");
QuickSort.sort(l);
System.out.println("Quicksort end");
for(int i = 0; i < l.length; i++)
l[i] = r.nextInt();
System.out.println("Bubblesort start");
BubbleSort.sort(l);
System.out.println("Bubblesort end");
return null;