~Sorting algorithms ~Start worksheets ~Start labs tomorrow or

Selection and Insertion Sorts
NOTES
~Sorting algorithms
~Start worksheets
~Start labs tomorrow or Thursday
March 17, 2015
Selection and Insertion Sorts
March 17, 2015
Sorting
Objective #1: Understand the basics of sorting.
• When you rearrange data and put it into a certain kind of
order, you are sorting the data. You can sort data
alphabetically, numerically, and in other ways. Often you
need to sort data before you use searching algorithms to find
a particular piece of data.
• There are a number of different sorting algorithms that are
widely used by programmers. Each algorithm has its own
advantages and disadvantages.
Selection and Insertion Sorts
March 17, 2015
• The key field is the field upon which the data is sorted. For
example, often last name is used as the key field when you sort
a list of people's names. You would not use first name as a key
field.
• A key value is a specific value that is stored within the key
field. If you are sorting with the key field of last name then a
key value at some point during the algorithm's execution would
be "Minich".
• The input size is the number of elements in an array or
ArrayList that are being sorted.
• There are two basic types of algorithms used to sort data: the
incremental approach and the divide and conquer approach.
> Using the incremental approach, one sorts the whole list at
once usually using loops.
> The divide and conquer approach splits the list up into parts
and then sorts each part separately. This algorithm then puts
the sorted parts together into a large sorted list.
Selection and Insertion Sorts
March 17, 2015
Objective #2: Understand the selection sort.
• The selection sort is an incremental sorting algorithm.
• Every key value is examined starting at the beginning of the list.
Assuming that you are sorting the list in ascending order, you use
a temporary variable to "remember" the position of the largest key
value. By the time you have examined every key value, you swap
the key value that was the largest with the last key value in the list.
Next, you repeat the process again from the beginning of the list,
however, you will not need to compare anything to the new last
key value in the list since you know it is the largest.
• This algorithm uses nested loops and is easy to code. However, it
is quite inefficient since it continues processing even if the list is
already sorted. Whether the original data is close to being sorted or
not, this algorithm takes quite awhile since a lot of loop iterations
and comparisons must be made.
• Here are some animations that illustrate the selection sort
> http://max.cs.kzoo.edu/~abrady/java/sorting/
SelectionSort.html
> http://www.cosc.canterbury.ac.nz/mukundan/dsal/SSort.html
Selection and Insertion Sorts
March 17, 2015
• Characteristics
> two nested loops
> relatively efficient for small lists of less than 100 elements
> many comparisons
> only 1 exchange (swap) at the end of each pass
> non-adjacent elements are compared
> no boolean variable is used to allow for an early exit
> a "temp variable" is used as a marker
• Here is an example of how the elements of an array (or ArrayList)
would move after the passes of the outer loop in a selection sort. The
underlined values are "set in concrete" as I like to say meaning that
they are in their fixed locations.
• 50 60 30 20 45
• 20 60 30 50 45 the complete array is scanned from left to right &
since 20 is the smallest it is swapped w/ the 50 in
index position 0
• 20 30 60 50 45 the array from index positions 1 thru 4 is scanned
& since 30 is smallest it is swapped w/ the 60 in
index position 1
• 20 30 45 50 60 the array from index positions 2 thru 4 is scanned
& since 45 is smallest it is swapped w/ the 60 in
index position 2
• 20 30 45 50 60 the array from index positions 3 thru 4 is scanned
& since 50 is the smallest it is swapped w/ itself
Selection and Insertion Sorts
March 17, 2015
• Here is an example of a selection sort
•
public static void selectionSort(int[] arr)
{
for (int i = 0; i < arr.length - 1; i++)
{
int minIndex = i;
int min = arr[minIndex];
for ( int j = i + 1; j < arr.length; j++)
{
if ( arr[j] < min)
{
minIndex = j;
min = arr[minIndex];
}
}
int temp = a[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
// swap
Selection and Insertion Sorts
March 17, 2015
Objective #3: Understand the insertion sort.
• The insertion sort is another incremental sorting algorithm.
• Two lists are used throughout this algorithm. One list is sorted and the other is
unsorted. The sorted list begins with one key value. Successively, one key
value from the unsorted list is placed into the proper order with the smaller,
sorted list. As more key values are placed into the sorted list, that list becomes
larger.
• The insertion sort is relatively quick for small lists that are close to already
being sorted.
• Here are some animations that illustrate the insertion sort
> http://max.cs.kzoo.edu/~abrady/java/sorting/InsertionSort.html
> http://www.cosc.canterbury.ac.nz/mukundan/dsal/ISort.html
Selection and Insertion Sorts
March 17, 2015
• Characteristics
> two nested loops
> boolean variable allows inner loop to exit early saving a bit of time here and
there
> compares non-adjacent elements
> no exchanges (swaps), rather many elements must shift
> a lot of comparisons are made slowing down this algorithm
> very efficient when data is close to being in order
> very inefficient when data is really out of order
• Here is an example of how the elements of an array (or ArrayList) would move
after the passes of the outer loop in an insertion sort:
• 50 30 60 40 45
• 30 50 60 40 45 30 was compared to 50, the 50 is shifted to the right & the 30 is
inserted to the left of the 50
• 30 50 60 40 45 60 was compared to 50 & since it is greater than the 50 it stays
where it is
• 30 40 50 60 45 40 was compared to 60, the 50 & the 30 & since it is greater
than 30 it is inserted between 30 & 50 while 50 & 60 are shifted
right
• 30 40 45 50 60 45 was compared to 60, the 50, & the 40 & since it is greater
than 40 it is inserted between 40 & 50 while 50 & 60 are shifted
right
Selection and Insertion Sorts
March 17, 2015
· Here is an example of an insertion sort
public static void insertionSort(int[] arr)
{
int j = 0;
for (int i = 1; i < arr.length; i++)
{
int value = arr[i];
j = i;
while (j > 0 && arr[j - 1] >= value)
{
arr[j] = arr[j - 1];
// shift
j--;
}
arr[j] = value;
}
}
// insert