Sorting for beginners

By:Mohammad-Ali Bandzar | May 9 2019

An introduction to Sorting with java

In java there are many different ways one can sort an array. The main difference between sorting algorithms in java is computational time, which is often dependant on the data type and input size and the amount of memory the sorting algorithm uses. The two which i will be talking about in this tutorial include selection sort becuase i feel that that is a great introductory sorting algorithm for beginners and CockTail shakcker sort, becuase i did that for a presentation in class.

Selection Sort

Selection sort is probably the simpilst sorting algorithm not just in java but in general. All selection sort does is it goes through every element in the array and when its done, it swaps the arrays first value with the smallest value its found and it effectively keeps doing this untill it has gone over the whole array. This sorting algorithm has a time complexity of n^2, if you do not know big-O notation that is ok, all it means is that your program will need to go over the array n^2 times (where n is the array length) to sort the array using selection sort. An example selection sort algrithm can be found at the bottom of this page.

CockTail Shaker Sort:

CockTail shaker sort is a sorting algorithm that goes through the array and compares each element with the next element in the array(if applicible), if the next element is smaller it will swap the two elements. When it reaches the end of the array it will preform the same operation going from the end of the array to the begining. This will continue untill the algorithem can make a complete pass over the array without swapping any 2 elements.

Examples:
    public static void selectionSort(int[] arr){  
        for (int i = 0; i < arr.length - 1; i++)  
        {  
            int index = i;  
            for (int j = i + 1; j < arr.length; j++){  
                if (arr[j] < arr[index]){  
                    index = j;//searching for lowest index  
                }  
            }  
            int smallerNumber = arr[index];   
            arr[index] = arr[i];  
            arr[i] = smallerNumber;  
        }  
    }  
 void cocktailSort(int a[]) 
    { 
        boolean swapped = true; 
        int start = 0; 
        int end = a.length; 
  
        while (swapped == true) { 
            // reset the swapped flag on entering the 
            // loop, because it might be true from a 
            // previous iteration. 
            swapped = false; 
  
            // loop from bottom to top same as 
            // the bubble sort 
            for (int i = start; i < end - 1; ++i) { 
                if (a[i] > a[i + 1]) { 
                    int temp = a[i]; 
                    a[i] = a[i + 1]; 
                    a[i + 1] = temp; 
                    swapped = true; 
                } 
            } 
  
            // if nothing moved, then array is sorted. 
            if (swapped == false) 
                break; 
  
            // otherwise, reset the swapped flag so that it 
            // can be used in the next stage 
            swapped = false; 
  
            // move the end point back by one, because 
            // item at the end is in its rightful spot 
            end = end - 1; 
  
            // from top to bottom, doing the 
            // same comparison as in the previous stage 
            for (int i = end - 1; i >= start; i--) { 
                if (a[i] > a[i + 1]) { 
                    int temp = a[i]; 
                    a[i] = a[i + 1]; 
                    a[i + 1] = temp; 
                    swapped = true; 
                } 
            } 
  
            // increase the starting point, because 
            // the last stage would have moved the next 
            // smallest number to its rightful spot. 
            start = start + 1; 
        } 
    } 
  
    /* Prints the array */
    void printArray(int a[]) 
    { 
        int n = a.length; 
        for (int i = 0; i < n; i++) 
            System.out.print(a[i] + " "); 
        System.out.println(); 
    } 

THANKS FOR READING
credits:https://www.javatpoint.com/selection-sort-in-java
https://www.geeksforgeeks.org/cocktail-sort/

this was a sorting gif