diff --git a/algorithms.html b/algorithms.html index df20a02..536fb35 100644 --- a/algorithms.html +++ b/algorithms.html @@ -3,7 +3,7 @@
public class SelectionSort { - + public static void selectionSort(Comparable[] array) { - + for (int i = 0; i < array.length; i++) { int min = i; for (int j = i + 1; j < array.length; j++) { @@ -190,13 +190,13 @@ SELECTION SORT int comparison = j.compareTo(min); return comparison< 0; } - + private static void swap(Comparable[] array, int i, int j) { Comparable temp = array[i]; array[i] = array[j]; array[j] = temp; } - + public static <E> void printArray(E[] array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); @@ -212,30 +212,30 @@ SELECTION SORT } return true; } - + public static void main(String[] args) { Integer[] intArray = { 34, 17, 23, 35, 45, 9, 1 }; System.out.println("Unsorted Array: "); printArray(intArray); System.out.println("\nIs intArray sorted? " + isSorted(intArray)); - + selectionSort(intArray); System.out.println("\nSelection sort:"); printArray(intArray); System.out.println("\nIs intArray sorted? " + isSorted(intArray)); - + String[] stringArray = { "z", "g", "c", "o", "a", "@", "b", "A", "0", "." }; System.out.println("\n\nUnsorted Array: "); printArray(stringArray); - + System.out.println("\n\nSelection sort:"); selectionSort(stringArray); printArray(stringArray); } - } + }
Output is:
- - Unsorted Array: - 34 17 23 35 45 9 1 + + Unsorted Array: + 34 17 23 35 45 9 1 Is intArray sorted? false - + Selection sort: - 1 9 17 23 34 35 45 + 1 9 17 23 34 35 45 Is intArray sorted? true - - - Unsorted Array: - z g c o a @ b A 0 . - + + + Unsorted Array: + z g c o a @ b A 0 . + Selection sort: - . 0 @ A a b c g o z + . 0 @ A a b c g o z
- Unsorted Array: - 34 17 23 35 45 9 1 + + Unsorted Array: + 34 17 23 35 45 9 1 Is intArray sorted? false - + Selection sort: - 1 9 17 23 34 35 45 + 1 9 17 23 34 35 45 Is intArray sorted? true - - - Unsorted Array: - z g c o a @ b A 0 . - + + + Unsorted Array: + z g c o a @ b A 0 . + Selection sort: - . 0 @ A a b c g o z + . 0 @ A a b c g o z
+ Unsorted Array: + 34 17 23 35 45 9 1 Is intArray sorted? false - + Selection sort: - 1 9 17 23 34 35 45 + 1 9 17 23 34 35 45 Is intArray sorted? true - - - Unsorted Array: - z g c o a @ b A 0 . - + + + Unsorted Array: + z g c o a @ b A 0 . + Selection sort: - . 0 @ A a b c g o z + . 0 @ A a b c g o z
public class ShellSort { - + public static void shellSort(Comparable[] array) { int N = array.length; int h = 1; @@ -293,31 +293,31 @@ SHELLSORT // returns true if Comparable j is less than min private static boolean isLess(Comparable j, Comparable min) { - + int comparison = j.compareTo(min); return comparison< 0; - + } - + private static void swap(Comparable[] array, int i, int j) { - + Comparable temp = array[i]; array[i] = array[j]; array[j] = temp; - + } - + public static <E> void printArray(E[] array) { - + for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } - + } // Check if array is sorted public static boolean isSorted(Comparable[] array) { - + for (int i = 1; i < array.length; i++) { if (isLess(array[i], array[i - 1])) { return false; @@ -325,32 +325,32 @@ SHELLSORT } return true; } - + public static void main(String[] args) { - + Integer[] intArray = { 34, 1000, 23, 2, 35, 0, 9, 1 }; System.out.println("Unsorted Array: "); printArray(intArray); System.out.println("\nIs intArray sorted? " + isSorted(intArray)); - + shellSort(intArray); System.out.println("\nSelection sort:"); printArray(intArray); System.out.println("\nIs intArray sorted? " + isSorted(intArray)); - + String[] stringArray = { "z", "Z","999", "g", "c", "o", "a", "@", "b", "A","0", "." }; System.out.println("\n\nUnsorted Array: "); printArray(stringArray); - + System.out.println("\n\nSelection sort:"); shellSort(stringArray); printArray(stringArray); } } - - - + + +
- Unsorted Array: - 34 1000 23 2 35 0 9 1 + Unsorted Array: + 34 1000 23 2 35 0 9 1 Is intArray sorted? false - + Selection sort: - 0 1 2 9 23 34 35 1000 + 0 1 2 9 23 34 35 1000 Is intArray sorted? true - - - Unsorted Array: - z Z 999 g c o a @ b A 0 . - + + + Unsorted Array: + z Z 999 g c o a @ b A 0 . + Selection sort: - . 0 999 @ A Z a b c g o z + . 0 999 @ A Z a b c g o z
public class MergeSort { - + public int[] sort(int [] array){ mergeSort(array, 0, array.length-1); return array; - + } - + private void mergeSort(int[] array, int first, int last) { // We need mid to divide problem into smaller size pieces @@ -425,45 +425,45 @@ MERGESORT //merge solved pieces to get solution to original problem int a = 0, f = first, l = mid + 1; int[] temp = new int[last - first + 1]; - + while (f <= mid && l <= last) { temp[a++] = array[f] < array[l] ? array[f++] : array[l++]; } - + while (f <= mid) { temp[a++] = array[f++]; } - + while (l <= last) { temp[a++] = array[l++]; } - + a = 0; while (first <= last) { array[first++] = temp[a++]; } } - + public static void printArray(int[] array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } - + } - + public static void main(String[] args) { - + System.out.println("Original array:"); int[] example = { 100, 30, 55, 62, 2, 42, 20, 9, 394, 1, 0 }; printArray(example); - + System.out.println("\nMergesort"); MergeSort merge = new MergeSort(); merge.sort(example); printArray(example); - + } - } + }
Original array: - 100 30 55 62 2 42 20 9 394 1 0 + 100 30 55 62 2 42 20 9 394 1 0 Mergesort - 0 1 2 9 20 30 42 55 62 100 394 + 0 1 2 9 20 30 42 55 62 100 394
import java.util.Random; - + public class QuickSort { - + public void quicksort(int[] array) { quicksort(array, 0, array.length - 1); } - + private void quicksort(int[] array, int first, int last) { if (first < last) { int pivot = partition(array, first, last); @@ -545,15 +545,15 @@ QUICKSORT quicksort(array, pivot + 1, last); } } - + private int partition(int[] input, int first, int last) { /* - *notice how pivot is randomly selected this makes O(n^2) + *notice how pivot is randomly selected this makes O(n^2) *very low probability */ - int pivot = first + new Random().nextInt(last - first + 1); - + int pivot = first + new Random().nextInt(last - first + 1); + swap(input, pivot, last); for (int i = first; i < last; i++) { if (input[i] <= input[last]) { @@ -561,34 +561,34 @@ QUICKSORT first++; } } - + swap(input, first, last); return first; } - + private void swap(int[] input, int a, int b) { int temp = input[a]; input[a] = input[b]; input[b] = temp; } - + public static void printArray(int[] array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } } - + public static void main(String[] args) { - - int[] example = { 90, 6456, 20, 34, 65, -1, 54, -15, - 1, -999, 55, 529, 0 }; + + int[] example = { 90, 6456, 20, 34, 65, -1, 54, -15, + 1, -999, 55, 529, 0 }; System.out.println("Original Array"); printArray(example); System.out.println("\n\nQuickSort"); QuickSort sort = new QuickSort(); sort.quicksort(example); printArray(example); - + } } @@ -601,8 +601,8 @@ QUICKSORT Original Array - 90 6456 20 34 65 -1 54 -15 1 -999 55 529 0 - + 90 6456 20 34 65 -1 54 -15 1 -999 55 529 0 + QuickSort -999 -15 -1 0 1 20 34 54 55 65 90 529 6456 @@ -624,24 +624,24 @@ LINEAR SEARCH public class LinearSearch { - + public static void main(String[] args) { - int[] array = { 0, 2, 9, 10, 100, -2, -3, + int[] array = { 0, 2, 9, 10, 100, -2, -3, 902, 504, -1000, 20, 9923 }; - - System.out.println(linearSearch(array, 100)); + + System.out.println(linearSearch(array, 100)); //prints: 100 found at position index: 4 - - System.out.println(linearSearch(array, -1)); + + System.out.println(linearSearch(array, -1)); //prints: not found - + System.out.println(linearSearch(array, 9923)); //prints: 9923 found at position index: 11 } - + private static String linearSearch(int[] array, int toSearch){ String result ="not found"; - + for (int i=0; i< array.length; i++){ if(array[i]==toSearch){ result = array[i] +" found at position index: " + i; @@ -650,7 +650,7 @@ LINEAR SEARCH } return result; } - } + } @@ -691,32 +691,32 @@ BINARY SEARCH public class BinarySearch { - + private BinarySearch(){ } //Constructor - + public static int binarySearch(int toSearch, int[]array){ int lowestIndex = 0; int highestIndex = array.length -1; - + while (lowestIndex <= highestIndex){ - int mid = lowestIndex + + int mid = lowestIndex + (highestIndex - lowestIndex)/2; - - if ( toSearch < array[mid]) + + if ( toSearch < array[mid]) highestIndex = mid -1; - - else if ( toSearch > array[mid]) + + else if ( toSearch > array[mid]) lowestIndex = mid + 1; - + else if (toSearch==array[mid]) return mid; } return -1; } - + public static void main(String[] args) { int [] array = { -100, 1 , 2, 3, 4, 5, 100, 999, 10203}; - + System.out.println(binarySearch(999, array)); //returns index 6 System.out.println(binarySearch(-100, array)); BINARY SEARCH System.out.println(binarySearch(6, array)); //returns index -1 } - - } + + } @@ -743,8 +743,8 @@ BINARY SEARCH - + -
import java.util.Random; - + public class QuickSort { - + public void quicksort(int[] array) { quicksort(array, 0, array.length - 1); } - + private void quicksort(int[] array, int first, int last) { if (first < last) { int pivot = partition(array, first, last); @@ -545,15 +545,15 @@ QUICKSORT quicksort(array, pivot + 1, last); } } - + private int partition(int[] input, int first, int last) { /* - *notice how pivot is randomly selected this makes O(n^2) + *notice how pivot is randomly selected this makes O(n^2) *very low probability */ - int pivot = first + new Random().nextInt(last - first + 1); - + int pivot = first + new Random().nextInt(last - first + 1); + swap(input, pivot, last); for (int i = first; i < last; i++) { if (input[i] <= input[last]) { @@ -561,34 +561,34 @@ QUICKSORT first++; } } - + swap(input, first, last); return first; } - + private void swap(int[] input, int a, int b) { int temp = input[a]; input[a] = input[b]; input[b] = temp; } - + public static void printArray(int[] array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } } - + public static void main(String[] args) { - - int[] example = { 90, 6456, 20, 34, 65, -1, 54, -15, - 1, -999, 55, 529, 0 }; + + int[] example = { 90, 6456, 20, 34, 65, -1, 54, -15, + 1, -999, 55, 529, 0 }; System.out.println("Original Array"); printArray(example); System.out.println("\n\nQuickSort"); QuickSort sort = new QuickSort(); sort.quicksort(example); printArray(example); - + } }
Original Array - 90 6456 20 34 65 -1 54 -15 1 -999 55 529 0 - + 90 6456 20 34 65 -1 54 -15 1 -999 55 529 0 + QuickSort -999 -15 -1 0 1 20 34 54 55 65 90 529 6456 @@ -624,24 +624,24 @@ LINEAR SEARCH public class LinearSearch { - + public static void main(String[] args) { - int[] array = { 0, 2, 9, 10, 100, -2, -3, + int[] array = { 0, 2, 9, 10, 100, -2, -3, 902, 504, -1000, 20, 9923 }; - - System.out.println(linearSearch(array, 100)); + + System.out.println(linearSearch(array, 100)); //prints: 100 found at position index: 4 - - System.out.println(linearSearch(array, -1)); + + System.out.println(linearSearch(array, -1)); //prints: not found - + System.out.println(linearSearch(array, 9923)); //prints: 9923 found at position index: 11 } - + private static String linearSearch(int[] array, int toSearch){ String result ="not found"; - + for (int i=0; i< array.length; i++){ if(array[i]==toSearch){ result = array[i] +" found at position index: " + i; @@ -650,7 +650,7 @@ LINEAR SEARCH } return result; } - } + } @@ -691,32 +691,32 @@ BINARY SEARCH public class BinarySearch { - + private BinarySearch(){ } //Constructor - + public static int binarySearch(int toSearch, int[]array){ int lowestIndex = 0; int highestIndex = array.length -1; - + while (lowestIndex <= highestIndex){ - int mid = lowestIndex + + int mid = lowestIndex + (highestIndex - lowestIndex)/2; - - if ( toSearch < array[mid]) + + if ( toSearch < array[mid]) highestIndex = mid -1; - - else if ( toSearch > array[mid]) + + else if ( toSearch > array[mid]) lowestIndex = mid + 1; - + else if (toSearch==array[mid]) return mid; } return -1; } - + public static void main(String[] args) { int [] array = { -100, 1 , 2, 3, 4, 5, 100, 999, 10203}; - + System.out.println(binarySearch(999, array)); //returns index 6 System.out.println(binarySearch(-100, array)); BINARY SEARCH System.out.println(binarySearch(6, array)); //returns index -1 } - - } + + } @@ -743,8 +743,8 @@ BINARY SEARCH - + -
Original Array - 90 6456 20 34 65 -1 54 -15 1 -999 55 529 0 - + 90 6456 20 34 65 -1 54 -15 1 -999 55 529 0 + QuickSort -999 -15 -1 0 1 20 34 54 55 65 90 529 6456
public class LinearSearch { - + public static void main(String[] args) { - int[] array = { 0, 2, 9, 10, 100, -2, -3, + int[] array = { 0, 2, 9, 10, 100, -2, -3, 902, 504, -1000, 20, 9923 }; - - System.out.println(linearSearch(array, 100)); + + System.out.println(linearSearch(array, 100)); //prints: 100 found at position index: 4 - - System.out.println(linearSearch(array, -1)); + + System.out.println(linearSearch(array, -1)); //prints: not found - + System.out.println(linearSearch(array, 9923)); //prints: 9923 found at position index: 11 } - + private static String linearSearch(int[] array, int toSearch){ String result ="not found"; - + for (int i=0; i< array.length; i++){ if(array[i]==toSearch){ result = array[i] +" found at position index: " + i; @@ -650,7 +650,7 @@ LINEAR SEARCH } return result; } - } + }
public class BinarySearch { - + private BinarySearch(){ } //Constructor - + public static int binarySearch(int toSearch, int[]array){ int lowestIndex = 0; int highestIndex = array.length -1; - + while (lowestIndex <= highestIndex){ - int mid = lowestIndex + + int mid = lowestIndex + (highestIndex - lowestIndex)/2; - - if ( toSearch < array[mid]) + + if ( toSearch < array[mid]) highestIndex = mid -1; - - else if ( toSearch > array[mid]) + + else if ( toSearch > array[mid]) lowestIndex = mid + 1; - + else if (toSearch==array[mid]) return mid; } return -1; } - + public static void main(String[] args) { int [] array = { -100, 1 , 2, 3, 4, 5, 100, 999, 10203}; - + System.out.println(binarySearch(999, array)); //returns index 6 System.out.println(binarySearch(-100, array)); BINARY SEARCH System.out.println(binarySearch(6, array)); //returns index -1 } - - } + + }