Java Sort Algorithm

java sort algorithmWelcome to my Java sort algorithm tutorial. Here I will cover all of the elementary sorting algorithms : Bubble, Selection and Insertion sort.

I also created a new method we can use to analyze the arrays so we can learn how the sorts work. I want this video to be very interactive so that you really understand the sort algorithms.

I also cover the linear and binary search algorithms. The code below will help you learn these algorithms perfectly.

If you like videos like this, it helps to tell Google+ [googleplusone]

Code From the Video

 

22 thoughts on “Java Sort Algorithm”

  1. Hi Darek,
    i always have a confusion, why we use two looping statements in sorting algorithms, it may looks silly question, but need some clarification.

    1. One of the loops is starting at the end of the array and the other at the beginning. It is almost like a bat hitting a ball. The ball starts at one end and moves toward the bat. The bat hits it and it goes back to the beginning. The only difference in this situation is that after the bat hits the ball it moves closer to the balls starting position by 1 index. I hope that helps

      1. Hey Derek great tutorial as always, I was wondering if you can apply the same algorithm to Doubly Linked List and can you make a tutorial video of it.

  2. Hi Derek,

    I was asked in the interview, how will you sort an array with 1 million values in it? Please let me know your thoughts on this.

    Thanks
    Raj

    1. Hi Raj

      QuickSort is normally faster then heap sort because of how it manages data in memory, but QuickSorts worst case performance is considerably worse then Heap Sort.

  3. Hi Derek,

    Your bubbleSort() function does not work correctly for a reverse
    sorted array as the outer for loop iterates n-2 times when it should be iterating n-1 times.

    To verify this for yourself, in your generateRandomArray() function, sort and reverse the array using inbuilt java functions
    and then call the bubbleSort function.

    (This is the second time I am trying to report this error. Hope you fix it.)

      1. The correct bubble sort is:
        public void bubbleSort(){

        // i starts at the end of the Array
        // As it is decremented all indexes greater
        // then it are sorted

        for(int i = arraySize – 1; i > 0; i–){

        // The inner loop starts at the beginning of
        // the array and compares each value next to each
        // other. If the value is greater then they are
        // swapped

        for(int j = 0; j < i; j++){

        // To change sort to Descending change to theArray[j + 1]){

        swapValues(j, j+1);

        printHorzArray(i, j);

        }

        }

        }

        }

  4. Derek Banas,

    Just want to say that I’ve been learning more about serious problem solving in your tutorials than any other resource on the internet or otherwise.

    I take my hat off and thank-you.

    Thanks,

    Aidan

  5. Derek……you have been my online teacher and mentor for about 2 months now….and will continue to be for a long long time….thanks for taking your time to do allthe great stuff.

Leave a Reply

Your email address will not be published. Required fields are marked *