├── README.md ├── sorting_img ├── bubble_sort.PNG ├── insertion_sort.PNG └── selection_sort.PNG └── src ├── search └── BinarySearch.java └── sorting ├── BubbleSort.java ├── DirectInsertionSort.java ├── QuickSort.java ├── RadixSort.java ├── SelectionSort.java └── ShellSort.java /README.md: -------------------------------------------------------------------------------- 1 | # Algorithms-with-Java 2 | Advanced Sorting and Search Algorithms. techniques to find an item in a bunch, of to sort a small or huge amount of items. Indispensable for every programmer. 3 | -------------------------------------------------------------------------------- /sorting_img/bubble_sort.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/njadNissi/Algorithms-with-Java/500ada9423cab0c1e667730eaf82c821de27d403/sorting_img/bubble_sort.PNG -------------------------------------------------------------------------------- /sorting_img/insertion_sort.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/njadNissi/Algorithms-with-Java/500ada9423cab0c1e667730eaf82c821de27d403/sorting_img/insertion_sort.PNG -------------------------------------------------------------------------------- /sorting_img/selection_sort.PNG: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/njadNissi/Algorithms-with-Java/500ada9423cab0c1e667730eaf82c821de27d403/sorting_img/selection_sort.PNG -------------------------------------------------------------------------------- /src/search/BinarySearch.java: -------------------------------------------------------------------------------- 1 | package search; 2 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 3 | * Author: NJAD NISSI 安杰 * 4 | * COMPUTER SCIENCE ENGINEER * 5 | * PROGRAMMER IN C, CPP, JAVA, PYTHON. * 6 | * contact me at: njadnissi@gmail.com * 7 | ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 8 | /**Binary search works only for sorted list of items.*/ 9 | public class BinarySearch { 10 | 11 | static boolean getSearchResult(double[] numbers, int from, int to, double value){ 12 | int round = 0; 13 | int start = from; 14 | int end = to; 15 | 16 | while(start <= end){ 17 | int middle = start + (end - start) / 2; 18 | 19 | showStep(numbers, start, end, middle, round); 20 | 21 | if(numbers[middle] == value) 22 | return true; 23 | else if(numbers[middle] < value) 24 | start = middle + 1; 25 | else /**if(numbers[middle] > value)*/ 26 | end = middle - 1; 27 | 28 | round++; 29 | } 30 | return false; 31 | } 32 | 33 | static int getSearchIndex(double[] numbers, int from, int to, double value){ 34 | int round = 0; 35 | int start = from; 36 | int end = to; 37 | 38 | while(start <= end){ 39 | int middle = start + (end - start) / 2; 40 | 41 | showStep(numbers, start, end, middle, round); 42 | 43 | if(numbers[middle] == value) 44 | return middle; 45 | else if(numbers[middle] < value) 46 | start = middle + 1; 47 | else /**if(numbers[middle] > value)*/ 48 | end = middle - 1; 49 | 50 | round++; 51 | } 52 | return -1; 53 | } 54 | static void showStep(double[] numbers, int start, int end, int mid, int round) { 55 | if(round == 0) 56 | System.out.println("BINARY SEARCH ALGORITHM"); 57 | else 58 | System.out.println("round" + round + "=> search interval=[" + start + ", " + end+"] midInd= "+ mid +" value = " + numbers[mid]); 59 | System.out.println("------------------------------------------------------------------------------------------------------------------"); 60 | } 61 | 62 | static void showList(double[] numbers){ 63 | /**print the sorted array*/ 64 | System.out.print("Sorted List:{ "); 65 | for (int i = 0; i < numbers.length - 1; i++) 66 | System.out.print(numbers[i] + " "); 67 | System.out.println(numbers[numbers.length - 1]+" }"); 68 | /**print subscripts*/ 69 | System.out.print("indexes: "); 70 | for (int i = 0; i < numbers.length - 1; i++) 71 | System.out.print(i + " "); 72 | System.out.println(numbers.length - 1); 73 | } 74 | 75 | public static void main(String[] args) { 76 | 77 | double numbers[] = {-11, 0, 6, 10, 14, 20, 50, 73, 92, 100, 255}; 78 | double value = -11; 79 | 80 | showList(numbers); 81 | 82 | /**Execute the search algorithm*/ 83 | boolean searchResult = getSearchResult(numbers, 0, numbers.length, value); 84 | System.out.println("found " + value + "? : " + searchResult); 85 | 86 | int searchIndex = getSearchIndex(numbers, 0, numbers.length, value); 87 | System.out.println("found " + value + " at : " + searchIndex); 88 | 89 | } 90 | 91 | } 92 | -------------------------------------------------------------------------------- /src/sorting/BubbleSort.java: -------------------------------------------------------------------------------- 1 | package sorting; 2 | 3 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 4 | * Author: NJAD NISSI 安杰 * 5 | * COMPUTER SCIENCE ENGINEER * 6 | * PROGRAMMER IN C, CPP, JAVA, PYTHON. * 7 | * contact me at: njadnissi@gmail.com * 8 | ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 9 | /** 10 | * Bubble Sort: 11 | * (1) We start with the second element, and traverse the whole list, 12 | * looking for the right position for that (according to ascending or descending order). 13 | * (2) We repeat the process until the last iteration which is item the last item. 14 | * (#) the sorting is successful after n-1 times, n being the number of items. 15 | * */ 16 | 17 | public class BubbleSort { 18 | 19 | static void sortAscendingOrder(double[] numbers) { 20 | System.out.println("BUBBLE SORT"); 21 | showStep(numbers, 0); 22 | for (int i = 1; i < numbers.length; i++) { 23 | for (int j = 0; j < numbers.length; j++) 24 | if (numbers[i] < numbers[j]) 25 | swap(numbers, i, j); 26 | showStep(numbers, i); 27 | } 28 | } 29 | 30 | static void sortDescendingOrder(double[] numbers) { 31 | System.out.println("BUBBLE SORT"); 32 | showStep(numbers, 0); 33 | for (int i = 1; i < numbers.length; i++) { 34 | for (int j = 0; j < numbers.length; j++) 35 | if (numbers[i] < numbers[j]) 36 | swap(numbers, i, j); 37 | showStep(numbers, i); 38 | } 39 | } 40 | 41 | static void swap(double[] numbers, int i, int j) { 42 | if (i != j) { 43 | numbers[i] += numbers[j]; 44 | numbers[j] = numbers[i] - numbers[j]; 45 | numbers[i] -= numbers[j]; 46 | } 47 | } 48 | 49 | static void showStep(double[] numbers, int round) { 50 | System.out.print("round" + round + "=> "); 51 | for (int j = 0; j < numbers.length - 1; j++) 52 | System.out.print(numbers[j] + " "); 53 | System.out.println(numbers[numbers.length - 1]); 54 | System.out.println("------------------------------------------------------------------------------------------------------------------"); 55 | } 56 | 57 | public static void main(String[] args) { 58 | 59 | double numbers[] = {255, 14, 73, 92, 20, 6, 10, 100, -11, 50, 0}; 60 | 61 | sortAscendingOrder(numbers); 62 | sortDescendingOrder(numbers); 63 | 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/sorting/DirectInsertionSort.java: -------------------------------------------------------------------------------- 1 | package sorting; 2 | 3 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 4 | * Author: NJAD NISSI 安杰 * 5 | * COMPUTER SCIENCE ENGINEER * 6 | * PROGRAMMER IN C, CPP, JAVA, PYTHON. * 7 | * contact me at: njadnissi@gmail.com * 8 | ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 9 | /** 10 | * Insertion Sort or direct Insertion Sort: 11 | * (1) We divide the list into two parts: Sorted space () and unsorted space []. 12 | * (2) we select the first item of the unsorted space and insert it into the right 13 | * position (according to ascending or descending order) in the sorted space. 14 | * (3) Repeat the process until the unsorted space has no item left. 15 | * */ 16 | public class DirectInsertionSort { 17 | 18 | static void sortAscendingOrder(double[] numbers) { 19 | int sortedElNo = 1; /**directly push the first element in the sorted space*/ 20 | 21 | System.out.println("DIRECT INSERT SORTING\n(SORTED SPACE) [UNSORTED SPACE]"); 22 | showStep(numbers, -1, 0); 23 | 24 | for (int i = 0; i < numbers.length; i++) { 25 | 26 | /**Move the current value from unsorted space to sorted space.*/ 27 | for (int j = 0; j < sortedElNo - 1; j++) { 28 | if (numbers[i] < numbers[j]) { 29 | moveValue(numbers, i, j); 30 | break; 31 | } 32 | } 33 | showStep(numbers, sortedElNo - 1, i+1); 34 | 35 | sortedElNo++; 36 | } 37 | } 38 | 39 | static void sortDescendingOrder(double[] numbers) { 40 | int sortedElNo = 1; /**directly push the first element in the sorted space*/ 41 | 42 | System.out.println("DIRECT INSERT SORTING\n(SORTED SPACE) [UNSORTED SPACE]"); 43 | showStep(numbers, -1, 0); 44 | 45 | for (int i = 0; i < numbers.length; i++) { 46 | 47 | /**Move the current value from unsorted space to sorted space.*/ 48 | for (int j = 0; j < sortedElNo - 1; j++) { 49 | if (numbers[i] > numbers[j]) { 50 | moveValue(numbers, i, j); 51 | break; 52 | } 53 | } 54 | showStep(numbers, sortedElNo - 1, i+1); 55 | 56 | sortedElNo++; 57 | } 58 | } 59 | 60 | static void moveValue(double[] numbers, int from, int to) { 61 | double valueToMove = numbers[from]; 62 | /**shift toward right because is from >= to*/ 63 | for (int i = from; i > to; i--) 64 | numbers[i] = numbers[i - 1]; 65 | numbers[to] = valueToMove; 66 | } 67 | 68 | static void showStep(double[] numbers, int sortedElNo, int round) { 69 | 70 | System.out.print("round" + round + "=>( ");/**OPEN SORTED SPACE*/ 71 | if (sortedElNo < 0) { 72 | System.out.print(" ) [ "); 73 | for (int j = 0; j < numbers.length - 1; j++) 74 | System.out.print(numbers[j] + " "); 75 | } else { 76 | for (int j = 0; j < numbers.length - 1; j++) { 77 | if (j == sortedElNo) 78 | System.out.print(numbers[j] + " ) [ ");/**CLOSE SORTED SPACE*/ 79 | else 80 | System.out.print(numbers[j] + " "); 81 | } 82 | } 83 | System.out.print(numbers[numbers.length - 1]);/**Appending the last element without adding spaces after*/ 84 | System.out.println(" ]\n------------------------------------------------------------------------------------------------------------------"); 85 | } 86 | 87 | public static void main(String[] args) { 88 | 89 | double[] numbers = {255, 14, 73, 92, 20, 6, 10, 100, -11, 50, 0}; 90 | 91 | sortAscendingOrder(numbers); 92 | sortDescendingOrder(numbers); 93 | 94 | } 95 | 96 | } 97 | -------------------------------------------------------------------------------- /src/sorting/QuickSort.java: -------------------------------------------------------------------------------- 1 | package sorting; 2 | 3 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 4 | * Author: NJAD NISSI 安杰 * 5 | * COMPUTER SCIENCE ENGINEER * 6 | * PROGRAMMER IN C, CPP, JAVA, PYTHON. * 7 | * contact me at: njadnissi@gmail.com * 8 | ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 9 | public class QuickSort { 10 | } 11 | -------------------------------------------------------------------------------- /src/sorting/RadixSort.java: -------------------------------------------------------------------------------- 1 | package sorting; 2 | 3 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 4 | * Author: NJAD NISSI 安杰 * 5 | * COMPUTER SCIENCE ENGINEER * 6 | * PROGRAMMER IN C, CPP, JAVA, PYTHON. * 7 | * contact me at: njadnissi@gmail.com * 8 | ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 9 | public class RadixSort { 10 | 11 | static void sort(double[] numbers) { 12 | 13 | } 14 | 15 | public static void main(String[] args) { 16 | 17 | double numbers[] = {255, 14, 73, 92, 20, 6, 10, 100, -11, 50, 0}; 18 | 19 | sort(numbers); 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /src/sorting/SelectionSort.java: -------------------------------------------------------------------------------- 1 | package sorting; 2 | 3 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 4 | * Author: NJAD NISSI 安杰 * 5 | * COMPUTER SCIENCE ENGINEER * 6 | * PROGRAMMER IN C, CPP, JAVA, PYTHON. * 7 | * contact me at: njadnissi@gmail.com * 8 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 9 | /** 10 | * Selection Sort: 11 | * (1) We divide the list into two parts: Sorted space () and unsorted space []. 12 | * (2) we select the minimum(for ascending order) or maximum(for descending order) 13 | * item from the unsorted space and move it to next position int the sorted space. 14 | * (3) Repeat the process until the unsorted space has no item left. 15 | * (#) the sorting is successful after n-1 times, n being the number of items. 16 | * */ 17 | public class SelectionSort { 18 | 19 | static void sortAscendingOrder(double[] numbers) { 20 | int sortedElementsNo = 0; 21 | 22 | System.out.println("SIMPLE SELECT SORTING\n(SORTED SPACE) [UNSORTED SPACE]"); 23 | showStep(numbers, -1, 0); 24 | 25 | for (int round = 1; round <= numbers.length; round++) { 26 | /**If this number is the minimum of the unsorted list.*/ 27 | int minIndex = getMinIndex(numbers, sortedElementsNo); 28 | 29 | swap(numbers, sortedElementsNo, minIndex); 30 | 31 | /**DISPLAY ALL STEPS OF THE METHOD*/ 32 | showStep(numbers, sortedElementsNo, round); 33 | 34 | sortedElementsNo++; 35 | } 36 | } 37 | 38 | static void sortDescendingOrder(double[] numbers) { 39 | int sortedElementsNo = 0; 40 | 41 | System.out.println("SIMPLE SELECT SORTING\n(SORTED SPACE) [UNSORTED SPACE]"); 42 | showStep(numbers, -1, 0); 43 | 44 | for (int round = 1; round <= numbers.length; round++) { 45 | /**If this number is the minimum of the unsorted list.*/ 46 | int maxIndex = getMaxIndex(numbers, sortedElementsNo); 47 | 48 | swap(numbers, sortedElementsNo, maxIndex); 49 | 50 | /**DISPLAY ALL STEPS OF THE METHOD*/ 51 | showStep(numbers, sortedElementsNo, round); 52 | 53 | sortedElementsNo++; 54 | } 55 | } 56 | 57 | static void swap(double[] numbers, int i, int j) { 58 | if (i != j) { 59 | numbers[i] += numbers[j]; 60 | numbers[j] = numbers[i] - numbers[j]; 61 | numbers[i] -= numbers[j]; 62 | } 63 | } 64 | 65 | static int getMinIndex(double[] numbers, int startPos) { 66 | int min = startPos; 67 | 68 | for (int i = startPos + 1; i < numbers.length; i++) 69 | if (numbers[i] < numbers[min]) min = i; 70 | return min; 71 | } 72 | 73 | static int getMaxIndex(double[] numbers, int startPos) { 74 | int max = startPos; 75 | 76 | for (int i = startPos + 1; i < numbers.length; i++) 77 | if (numbers[i] > numbers[max]) max = i; 78 | return max; 79 | } 80 | 81 | static void showStep(double[] numbers, int sortedElNo, int round) { 82 | 83 | System.out.print("round" + round + "=>( ");/**OPEN SORTED SPACE*/ 84 | if (sortedElNo < 0) { 85 | System.out.print(" ) [ "); 86 | for (int j = 0; j < numbers.length - 1; j++) 87 | System.out.print(numbers[j] + " "); 88 | } else { 89 | for (int j = 0; j < numbers.length - 1; j++) { 90 | if (j == sortedElNo) 91 | System.out.print(numbers[j] + " ) [ ");/**CLOSE SORTED SPACE*/ 92 | else 93 | System.out.print(numbers[j] + " "); 94 | } 95 | } 96 | System.out.print(numbers[numbers.length - 1]);/**Appending the last element without adding spaces after*/ 97 | System.out.println(" ]\n------------------------------------------------------------------------------------------------------------------"); 98 | } 99 | 100 | public static void main(String[] args) { 101 | 102 | double numbers[] = {255, 14, 73, 92, 20, 6, 10, 100, -11, 50, 0}; 103 | 104 | sortAscendingOrder(numbers); 105 | sortDescendingOrder(numbers); 106 | 107 | } 108 | 109 | } -------------------------------------------------------------------------------- /src/sorting/ShellSort.java: -------------------------------------------------------------------------------- 1 | package sorting; 2 | 3 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 4 | * Author: NJAD NISSI 安杰 * 5 | * COMPUTER SCIENCE ENGINEER * 6 | * PROGRAMMER IN C, CPP, JAVA, PYTHON. * 7 | * contact me at: njadnissi@gmail.com * 8 | ** * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ 9 | public class ShellSort { 10 | } 11 | --------------------------------------------------------------------------------