Skip to content

Instantly share code, notes, and snippets.

@technoglot
Last active May 1, 2019 01:35
Show Gist options
  • Save technoglot/a048ccdc15baf601d7e5b4deb5563e11 to your computer and use it in GitHub Desktop.
Save technoglot/a048ccdc15baf601d7e5b4deb5563e11 to your computer and use it in GitHub Desktop.
Collection of popular sorting algorithms programmed in Java.
public class BinarySearch {

    public static void main(String[] args) {
        //Make an array
        int[] intArray = {25, 52, 4, 79, 23, 80, 77, 54, 45, 11, 5, 100, 27, 25, 45, 1, 4, 2, 9, 33};

        //Call Selection Sort Method
        printArray(intArray);
        sortMethod(intArray);
        search(intArray, 27);
    }

    public static void sortMethod(int[] array) {
        int x = array.length;

        //Check the first index
        for (int i = 0; i < x - 1; i++) {
            int minIndex = i;

            //Check the index beside the index defined by i
            for (int j = i + 1; j < x; j++) {
                if (array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }

            //Swap the numbers once you found the smallest value
            int temp = array[minIndex];
            array[minIndex] = array[i];
            array[i] = temp;
            printArray(array);
        }

    }

    public static void printArray(int[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }

    public static void search(int[] intArray, int find) {
        //Search for a value
        long start = System.nanoTime();
        int index = 0;

        int Left = 0, Right = intArray.length - 1;
        while (Left <= Right) {
            int Middle = Left + (Right - Left) / 2;

            // Check if x is present at mid
            if (intArray[Middle] == find) {
                index = Middle;
            }

            // If x greater, ignore left half
            if (intArray[Middle] < find) {
                Left = Middle + 1;
            } // If x is smaller, ignore right half
            else {
                Right = Middle - 1;
            }

            //Element was not found
            if (index == -1) {
                System.out.println("Value was not found");
            }
        }
        long end = System.nanoTime();
        System.out.println("Value found at index: " + index);
        System.out.println("Elapsed time: " + (end - start) + " nanoseconds");
    }
}
public class LinearSearch {

    public static void main(String[] args) {
        //Make an array
        int[] intArray = {25, 52, 4, 79, 23, 80, 77, 54, 45, 11, 5, 100, 27, 25, 45, 1, 4, 2, 9, 33};

        //Call Selection Sort Method
        printArray(intArray);
        sortMethod(intArray);
        search(intArray, 27);
    }

    public static void sortMethod(int[] array) {
        int x = array.length;

        //Check the first index
        for (int i = 0; i < x - 1; i++) {
            int minIndex = i;

            //Check the index beside the index defined by i
            for (int j = i + 1; j < x; j++) {
                if (array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }

            //Swap the numbers once you found the smallest value
            int temp = array[minIndex];
            array[minIndex] = array[i];
            array[i] = temp;
            printArray(array);
        }

    }

    public static void printArray(int[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }

    public static void search(int[] intArray, int find) {
        //Search for a value        
        int index;
        long start = System.nanoTime();
        
        for (int i = 0; i < intArray.length; i++) {
            if (intArray[i] == find) {
                index = i;
                System.out.println("Value found at index: " + index);
            }
        }

        long end = System.nanoTime();
        System.out.println("Elapsed time: " + (end - start) + " nanoseconds");

    }
}
public class BubbleSort_Int {

    public static void main(String[] args) {
        //Make an array
        int[] intArray = {25, 52, 4, 79, 23, 80, 77, 54, 45, 11, 5, 100, 27, 25, 45, 1, 4, 2, 9, 33};

        //Call Selection Sort Method
        printArray(intArray);
        sortMethod(intArray);
    }

    public static void sortMethod(int[] array) {
        int x = array.length;

        //Keeps track of the amount of times that the array is sorted
        for (int i = 0; i < x - 1; i++) {

            //Compare the value of index j and index j+1
            for (int j = 0; j < x - 1; j++) {
                if (array[j] > array[j + 1]) {

                    //Swap the numbers once you found the smallest value of the two
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                    printArray(array);
                }
            }
        }
    }

    public static void printArray(int[] array) {
        for (int k = 0; k < array.length; k++) {
            System.out.print(array[k] + " ");
        }
        System.out.println();
    }
}
public class BubbleSort_String {

    public static void main(String[] args) {
        //Make an array
        String[] strArray = {"Java", "Sort", "Studenten", "Algoritme", "Array", "Internet", "Juan 3:16", "Computer"};

        //Call Selection Sort Method
        printArray(strArray);
        sortMethod(strArray);
    }

    public static void sortMethod(String[] array) {
        int x = array.length;

        //Keeps track of the amount of times that the array is sorted
        for (int i = 0; i < x - 1; i++) {

            //Compare the value of index j and index j+1
            for (int j = 0; j < x - 1; j++) {
                if (array[j].compareTo(array[j + 1]) > 0) {

                    //Swap the numbers once you found the smallest value of the two
                    String temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                    printArray(array);
                }
            }
        }
    }

    public static void printArray(String[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
    }

}
public class ShakerSort {

    public static void main(String[] args) {
        //Make an array
        int[] intArray = {25, 52, 4, 79, 23, 80, 77, 54, 45, 11, 5, 100, 27, 25, 45, 1, 4, 2, 9, 33};

        //Call Selection Sort Method
        printArray(intArray);
        sortMethod(intArray);
    }

    public static void sortMethod(int[] array) {
        boolean swapped = true;
        int start = 0;
        int end = array.length;

        //While numbers were swapped, swapped is true. When no swaps were done, the loop goes on
        while (swapped == true) {
            //Swapped gets the value false in order to enter the first for loop
            swapped = false;

            //Sort phase L-t-R
            for (int j = start; j < end - 1; j++) {
                if (array[j] > array[j + 1]) {

                    //Swap the numbers once you found the smallest value of the two
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                    //Swap took place so swapped is now set to true 
                    swapped = true;

                    //Call method to print array contents
                    printArray(array);
                }
            }

            //If nothing was swapped, the array is sorted and the application ceases to run
            if (swapped == false) {
                break;
            //Otherwise, if something was swapped, the value of variable swapped must be changed
            //from true to false to move on to the next phase of swapping    
            } else {
                swapped = false;
            }
            
            //End is decreased by one because the last value is in the corresponding place
            end -= 1;
            
            //Sort phase R-t-L
            for (int k = end - 1; k >= start; k--) {
                if (array[k] > array[k + 1]) {

                    //Swap the numbers once you found the smallest value of the two
                    int temp = array[k];
                    array[k] = array[k + 1];
                    array[k + 1] = temp;
                    //Swap took place so swapped is now set to true
                    swapped = true;

                     //Call method to print array contents
                    printArray(array);
                }
            }
            //Start is increased by one because the first value is in the corresponding place
            start += 1;
        }
    }

    public static void printArray(int[] array) {
        for (int z = 0; z < array.length; z++) {
            System.out.print(array[z] + " ");
        }

        System.out.println();
    }

}
public class SelectionSort_Int {

    public static void main(String[] args) {
        //Make an array
        int[] intArray = {25, 52, 4, 79, 23, 80, 77, 54, 45, 11, 5, 100, 27, 25, 45, 1, 4, 2, 9, 33};

        //Call Selection Sort Method
        printArray(intArray);
        sortMethod(intArray);

    }

    public static void sortMethod(int[] array) {
        int x = array.length;

        //Check the first index
        for (int i = 0; i < x - 1; i++) {
            int minIndex = i;

            //Check the index beside the index defined by i
            for (int j = i + 1; j < x; j++) {
                if (array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }

            //Swap the numbers once you found the smallest value
            int temp = array[minIndex];
            array[minIndex] = array[i];
            array[i] = temp;
            printArray(array);
        }

    }

    public static void printArray(int[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }

        System.out.println();
    }
}
public class SelectionSort_String {

    public static void main(String[] args) {
        //Make an array
        String[] strArray = {"Java", "Sort", "Studenten", "Algoritme", "Array", "Internet", "Juan 3:16", "Computer"};

        //Call Selection Sort Method
        printArray(strArray);
        sortMethod(strArray);
    }

    public static void sortMethod(String[] array) {
        int x = array.length;

        //Check the first index
        for (int i = 0; i < x - 1; i++) {
            int minIndex = i;

            //Check the index beside the index defined by i
            for (int j = i + 1; j < x; j++) {
                //< 0(zero) to check if the string that is being compared is "smaller" than the string its being compared to
                if (array[j].compareTo(array[minIndex]) < 0) {
                    minIndex = j;
                }
            }

            //Swap the numbers once you found the smallest value
            String temp = array[minIndex];
            array[minIndex] = array[i];
            array[i] = temp;
            printArray(array);
        }

    }

    public static void printArray(String[] array) {
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + " ");
        }

        System.out.println();
    }
}
public class CountingSort {

    public static void main(String[] args) {
        //Make an array
        int[] intArray = {25, 52, 4, 79, 23, 80, 77, 54, 45, 11, 5, 100, 27, 25, 45, 1, 4, 2, 9, 33};

        //Call Methods
        printArray(intArray);
        sortMethod(intArray);
    }

    public static void sortMethod(int[] array) {
        //Create an array 
        int[] count = new int[101];

        //Initialize the values of array count to zero
        for (int i = 0; i < count.length; i++) {
            count[i] = 0;
            
            //Print results
            printArray(count);
        }

        //Finding the frequency of each value in the array intArray and 
        //allocating the frequency in the count array by storing the frequency
        //in the index of the array that corresponds to it's value in intArray
        for (int j = 0; j < array.length; j++) {
            int x = array[j];
            count[x] += 1;
            
            //Print results
            printArray(count);
        }

        //Cumulative frequency count; sum the values of an element with the value of the previous element
        for (int a = 1; a < count.length; a++) {
            count[a] += count[a - 1];
            
            //Print results
            printArray(count);
        }

        //New array, same length as intArray
        int[] intArrayCopy = new int[array.length];

        //Something 
        for (int b = 0; b < array.length; b++) {
            intArrayCopy[count[array[b]] - 1] = array[b];
            count[array[b]]--;
            
            //Print results
            printArray(count);
        }

        //Copy contents of intArrayCopy, into intArray
        for (int z = 0; z < array.length; z++) {
            array[z] = intArrayCopy[z];
        }

        //Print results
        printArray(array);
    }

    public static void printArray(int[] array) {
        for (int z = 0; z < array.length; z++) {
            System.out.print(array[z] + " ");
        }

        System.out.println();
    }

}
import java.util.Arrays;

public class Merge {

    public static void main(String[] args) {
        int[] array = {25, 52, 4, 79, 23, 80, 77, 54, 45, 11, 5, 100, 27, 25, 45, 1, 4, 2, 9, 33};
        mergeSort(array, new int[array.length], 0, array.length - 1);
        
        //Print the sorted array
        System.out.println(Arrays.toString(array));
    }

    public static void mergeSort(int[] array, int[] temp, int leftPointer, int rightPointer) {
        if (leftPointer >= rightPointer) {

        } else {
            int middle = (leftPointer + rightPointer) / 2;
            //Sort left half
            mergeSort(array, temp, leftPointer, middle);
            //Sort right half
            mergeSort(array, temp, middle + 1, rightPointer);
            //Merge both halves
            mergeHalves(array, temp, leftPointer, rightPointer);
        }
    }

    public static void mergeHalves(int[] array, int[] temp, int leftPointer, int rightPointer) {
        int leftEnd = (rightPointer + leftPointer) / 2;
        int rightStart = leftEnd + 1;
        int size = rightPointer - leftPointer + 1;

        int left = leftPointer;
        int right = rightStart;
        int index = leftPointer;

        while (left <= leftEnd && right <= rightPointer) {
            if (array[left] <= array[right]) {
                temp[index] = array[left];
                left++;
            } else {
                temp[index] = array[right];
                right++;
            }
            index++;
        }
        System.arraycopy(array, left, temp, index, leftEnd - left + 1);
        System.arraycopy(array, right, temp, index, rightPointer - right + 1);
        System.arraycopy(temp, leftPointer, array, leftPointer, size);
    }
}
public class MergeSort {
     
    static int[] array;
    static int[] tempMergArr;
    static int length;
 
    public static void main(String a[]){
        //Create an array of integers
        int[] arr = {25, 52, 4, 79, 23, 80, 77, 54, 45, 11, 5, 100, 27, 25, 45, 1, 4, 2, 9, 33};
        
        print(arr);
        
        //Create an object for the class
        MergeSort ms = new MergeSort();
        ms.sort(arr);
        
        print(arr);
    }
     
    public static void sort(int inputArr[]) {
        array = inputArr;
        length = inputArr.length;
        tempMergArr = new int[length];
        sort(0, length - 1);
    }
 
    public static void sort(int lowerIndex, int higherIndex) {
         
        if (lowerIndex < higherIndex) {
            int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
            
            // Below step sorts the left side of the array
            sort(lowerIndex, middle);
            
            // Below step sorts the right side of the array
            sort(middle + 1, higherIndex);
            
            // Now merge both sides
            merge(lowerIndex, middle, higherIndex);
        }
    }
 
    public static void merge(int lowerIndex, int middle, int higherIndex) {
 
        for (int i = lowerIndex; i <= higherIndex; i++) {
            tempMergArr[i] = array[i];
        }
        
        int i = lowerIndex;
        int j = middle + 1;
        int k = lowerIndex;
        
        while (i <= middle && j <= higherIndex) {
            if (tempMergArr[i] <= tempMergArr[j]) {
                array[k] = tempMergArr[i];
                i++;
            } else {
                array[k] = tempMergArr[j];
                j++;
            }
            k++;
        }
        
        while (i <= middle) {
            array[k] = tempMergArr[i];
            k++;
            i++;
        } 
    }
    
    public static void print(int[] arr){
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println("");
    } 
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment