Sorting an array

  • Sorting an array = re-arrange the values in an array so that the values are ordered

  • Note:

    • In our discussion, we will sort the array in ascending order

    • We can change the order very easily

  • Example:

     Input array:     67    5   78   34
     Output array:     5   34   67   78 

  • There are many sorting algorithms for arrays

  • We will first study the selection sort algorithm

The selection sort algorithm

  • The selection sort algorithm:

      • Selection sort finds the smallest number in the list and swaps it with the first element.

            

      • It then finds the smallest number remaining and swaps it with the second element

      • And so on, until only a single number remains (= the last number in the list).

  • Check out the animation:

The selection sort algorithm

  • The selection sort algorithm:

      • Selection sort finds the smallest number in the list and swaps it with the first element.

            

      • It then finds the smallest number remaining and swaps it with the second element

      • And so on, until only a single number remains (= the last number in the list).

  • Check out the animation:

The selection sort algorithm

  • The selection sort algorithm:

      • Selection sort finds the smallest number in the list and swaps it with the first element.

            

      • It then finds the smallest number remaining and swaps it with the second element

      • And so on, until only a single number remains (= the last number in the list).

  • Check out the animation:

The selection sort algorithm

  • The selection sort algorithm:

      • Selection sort finds the smallest number in the list and swaps it with the first element.

      • It then finds the smallest number remaining and swaps it with the second element

            

      • And so on, until only a single number remains (= the last number in the list).

  • Check out the animation:

The selection sort algorithm

  • The selection sort algorithm:

      • Selection sort finds the smallest number in the list and swaps it with the first element.

      • It then finds the smallest number remaining and swaps it with the second element

            

      • And so on, until only a single number remains (= the last number in the list).

  • Check out the animation:

The selection sort algorithm

Let's write the Selection Sort algorithm:

    public static void selectionSort(int[] list) 
    {
        for (int i = 0; i < list.length - 1; i++)
        {
            /* -----------------------------------------------
               Find the minimum in the list[i..list.length-1]
               ----------------------------------------------- */
           int min   = list[i];     // Assume first element is min
           int minIndex = i;           // Index where min is found

           for ( int k = minIndex+1; k < list.length; k++ )
               if ( list[k] < min ) // Found a smaller element
               {
                   min   = list[k]; // Update min value
                   minIndex = k;       // Update index
               }

            /* ------------------------------------------------------
               Swap list[i] with list[minIndex] if necessary;
               ------------------------------------------------------ */
            if ( minIndex != i )
            {   // Swap list[minIndex] and list[i]
                int help       = list[minIndex];
                list[minIndex] = list[i];
                list[i]        = help;
            }
        }
    } 

The selection sort algorithm

Repeatedly do:   (1) find the smallest element in the sub list list[i] .. list[N]

    public static void selectionSort(int[] list) 
    {
        for (int i = 0; i < list.length-1; i++)
        {
            /* -----------------------------------------------
               Find the minimum in the list[i..list.length-1]
               ----------------------------------------------- */

                                <---- find min in sublist ---->
                   +---+---+---+---+---+---+---+---+---+---+---+
                   | 1 | 2 | 3 |...|...|...| 4 |...|...|...|...|
                   +---+---+---+---+---+---+---+---+---+---+---+
                                 ^           ^
                                 |           |
                                 i         minIndex


            /* ------------------------------------------------------
               Swap list[i] with list[minIndex] if necessary;
               ------------------------------------------------------ */
            if ( minIndex != i )
            {   // Swap list[minIndex] and list[i]
                int help       = list[minIndex];
                list[minIndex] = list[i];
                list[i]        = help;
            }
        }
    } 

The selection sort algorithm

Repeatedly do:   (2) if list[minIndex] is out of place:   swap list[minIndex] <--> list[i]

    public static void selectionSort(int[] list) 
    {
        for (int i = 0; i < list.length-1; i++)
        {
            /* -----------------------------------------------
               Find the minimum in the list[i..list.length-1]
               ----------------------------------------------- */

                                <---- find min in sublist ---->
                   +---+---+---+---+---+---+---+---+---+---+---+
                   | 1 | 2 | 3 |...|...|...| 4 |...|...|...|...|
                   +---+---+---+---+---+---+---+---+---+---+---+
                                 ^           ^
                                 |           |
                                 i         minIndex


            /* ------------------------------------------------------
               Swap list[i] with list[minIndex] if necessary
               ------------------------------------------------------ */
            if ( minIndex != i )
            {   // Swap list[minIndex] and list[i]
                   +---+---+---+---+---+---+---+---+---+---+---+
                   | 1 | 2 | 3 | 4 |...|...|...|...|...|...|...|
                   +---+---+---+---+---+---+---+---+---+---+---+
            }
        }
    } 

The selection sort algorithm

Let's flesh out the selection sort algorithm:

    public static void selectionSort(int[] list) 
    {
        for (int i = 0; i < list.length-1; i++)
        {
            /* -----------------------------------------------
               Find the minimum in the list[i..list.length-1]
               ----------------------------------------------- */
           int min   = list[i];     // Assume first element is min
           int minIndex = i;           // Index where min is found

           for ( int k = minIndex+1; k < list.length; k++ )
               if ( list[k] < min ) // Found a smaller element
               {
                   min   = list[k]; // Update min value
                   minIndex = k;       // Update index
               }

            /* ------------------------------------------------------
               Swap list[i] with list[minIndex] if necessary
               ------------------------------------------------------ */
            if ( minIndex != i )
            {   // Swap list[minIndex] and list[i]
                int help       = list[minIndex];
                list[minIndex] = list[i];
                list[i]        = help;
            }
        }
    } 

The selection sort algorithm

The standard "find min" algorithm assumes that the first element in the array is the minimum:

    public static void selectionSort(int[] list) 
    {
        for (int i = 0; i < list.length-1; i++)
        {
            /* -----------------------------------------------
               Find the minimum in the list[i..list.length-1]
               ----------------------------------------------- */
           int min      = list[i];     // Assume first element is min
           int minIndex = i;           // Index where min is found

           for ( int k = minIndex+1; k < list.length; k++ )
               if ( list[k] < min ) // Found a smaller element
               {
                   min   = list[k]; // Update min value
                   minIndex = k;       // Update index
               }

            /* ------------------------------------------------------
               Swap list[i] with list[minIndex] if necessary
               ------------------------------------------------------ */
            if ( minIndex != i )
            {   // Swap list[minIndex] and list[i]
                int help       = list[minIndex];
                list[minIndex] = list[i];
                list[i]        = help;
            }
        }
    } 

The selection sort algorithm

We look for a better minimum in the remaining elements:

    public static void selectionSort(int[] list) 
    {
        for (int i = 0; i < list.length-1; i++)
        {
            /* -----------------------------------------------
               Find the minimum in the list[i..list.length-1]
               ----------------------------------------------- */
           int min      = list[i];     // Assume first element is min
           int minIndex = i;           // Index where min is found

           for ( int k = minIndex+1; k < list.length; k++ )
               if ( list[k] < min ) // Found a smaller element
               {
                   min      = list[k]; // Update min value
                   minIndex = k;       // Update index
               }

            /* ------------------------------------------------------
               Swap list[i] with list[minIndex] if necessary
               ------------------------------------------------------ */
            if ( minIndex != i )
            {   // Swap list[minIndex] and list[i]
                int help       = list[minIndex];
                list[minIndex] = list[i];
                list[i]        = help;
            }
        }
    } 

The selection sort algorithm

If we find a better minimum, we remember its value and index:

    public static void selectionSort(int[] list) 
    {
        for (int i = 0; i < list.length-1; i++)
        {
            /* -----------------------------------------------
               Find the minimum in the list[i..list.length-1]
               ----------------------------------------------- */
           int min      = list[i];     // Assume first element is min
           int minIndex = i;           // Index where min is found

           for ( int k = minIndex+1; k < list.length; k++ )
               if ( list[k] < min ) // Found a smaller element
               {
                   min      = list[k]; // Update min value
                   minIndex = k;       // Update its index
               }

            /* ------------------------------------------------------
               Swap list[i] with list[minIndex] if necessary
               ------------------------------------------------------ */
            if ( minIndex != i )
            {   // Swap list[minIndex] and list[i]
                int help       = list[minIndex];
                list[minIndex] = list[i];
                list[i]        = help;
            }
        }
    } 

The selection sort algorithm

If the smallest value in the sublist is out of place, swap list[minIndex] <--> list[i] :

    public static void selectionSort(int[] list) 
    {
        for (int i = 0; i < list.length-1; i++)
        {
            /* -----------------------------------------------
               Find the minimum in the list[i..list.length-1]
               ----------------------------------------------- */
           int min      = list[i];     // Assume first element is min
           int minIndex = i;           // Index where min is found

           for ( int k = minIndex+1; k < list.length; k++ )
               if ( list[k] < min ) // Found a smaller element
               {
                   min      = list[k]; // Update min value
                   minIndex = k;       // Update its index
               }

            /* ------------------------------------------------------
               Swap list[i] with list[minIndex] if necessary
               ------------------------------------------------------ */
            if ( minIndex != i )
            {   // Swap list[minIndex] and list[i]
                int help       = list[minIndex];  // Standard exchange alg
                list[minIndex] = list[i];
                list[i]        = help;
            }
        }
    } 

Demo program for the selection sort algorithm

DEMO: demo/04-inheritance/19-selsort :

   public static void main(String[] args)
   {
       int[] myList = {8, 4, 9, 7, 3, 5, 6, 1, 2};

       // Print list[] before sorting
       for ( int i = 0; i < myList.length; i++)
           System.out.print(myList[i]+ " ");
       System.out.println();

       selectionSort(myList);

       // Print list[] after sorting
       for ( int i = 0; i < myList.length; i++)
           System.out.print(myList[i]+ " ");
       System.out.println();
   }


Output: 8 4 9 7 3 5 6 1 2 1 2 3 4 5 6 7 8 9

I will use the selection sort method to show you an application of polymorphism next