Introduction to
sorting
Sorting
There are a lot of different ways to sort a list
Some are easier to program than others, some are more
efficient than others
Write this list down in pencil so you can erase
18 31 17
24 46 22
16 29
Sort this list:
the rules
You can only use
one arraysort in place
You may use a few
temp variables
Keep track of the system you use
Selection sort
Selection sort
works by looking at all the items in list, picking out the smallest, and
swapping it with the first item
It then looks for the next smaller item and swaps it
second item, etc.
In our list of eight items, how many swaps were made?
n-1 swaps
Pseudo coded for
selection sort
(for I = 0 to n-1) loop
through the array
find the index of the smallest
value
swap that value with the value at I
Java code for selection sort
public
static void selectionSort(Comparable[ ] A, int n)
{
for (int
index= 0; index> n-1; index++)
{
int smallindex= indexOfSmallest(A, index, n-1);
Comparable temp = A[smallindex]; // swap
A [smallindex]
= theArray[index];
theArray[index]
= temp;
}
// end for
} // end selectionSort
Java code for indexOfSmallest( )
private static int indexOfSmallest(Comparable[ ] theArray,
int first, int last)
{
Comparable min = theArray[first]; // smallest so far
int indexOfMin = first;
for (int index = first+1; index<= last; index++)
{
if (theArray[index].compareTo(min)<0)
{ min = theArray[index];
indexOfMin = index;
} // end if
} // end for
return indexOfMin; // index of smallest item
} // end indexOfSmallest
Analysis of selection sort
Sorting in general involves comparisons, and swaps,
or some sort of moving data
In Java, an array holds references to the actual data,
not the data itself
This means that moving data, or swapping it just means
changing the references, not the data itself
This is much less expensive than in other languages
Comparisons mean
there must be a call to either compareTo or equals
These methods
just compare the key.
When analyzing, we consider the comparisons and
the moving of the data because they are the major operations
In Java, the comparison of data items is the most
expensive, so it is often the only one considered
In other
programming languages, the move is the more expensive
What is the efficiency of selectionSort?
In a list of length
n how many times does the for loop in selectionSort execute?
The for loop executes n-1 times; each time it
executes
1) It calls indexOfSmallest(theArray, index, n-1)
2) It makes a swap (each swap executes 3 statements)
Each call to indexOfSmallest causes its loop to execute last - first times
So, n-1 calls to indexOfSmallest cause it to execute
(n-1)+(n-2)+
+1 This is n *
(n-1)/2 times
So we have n *
(n-1)/2 comparisons + (n-1) swaps
Together it would be n * (n-1)/2 + 3 (n-1)
or O(n2)
This sort
executes exactly the same regardless of what the original list looks like
Insertion sort
In this sort, the array is considered partitioned into
two parts, the sorted part and the unsorted part.
At each step the
first item in the unsorted part is inserted into its proper location in to the
sorted part
Pseudocode:
for ( I = 0
to n-1) loop through the array
put the value of the first value
in the unsorted
partition into temp
while (temp < sorted items)
move item into hole
Insertion Sort
public static void insertionSort(Comparable[ ]
theArray, int n)
{ for (int
unsorted = 1; unsorted < n; ++unsorted)
{
Comparable temp = theArray[unsorted];
int index =
unsorted;
while ((index > 0)
&& (theArray[index-1].
compareTo(temp)
> 0))
{ theArray[index--]
= theArray[index-1]; }
theArray[index]
= temp; }
}
// end insertionSort
Intuitive analysis of Insertion Sort
Best case
With sorted list makes only one comparison per item
The test at the
top of the WHILE loop fails immediately (so it doesnt go through the nested
loop)
Worst case
Reverse sorted list goes through nested loop every
time
Runs in O(n2) time
Actually, it is less than n2 time since the
length of the list it is comparing is less than n
Analysis of Insertion sort
In the worst
case
It does n(n-1)/2 comparisons
The inner loop moves data items at most the same
amount of times
The outer loop moves data items twice per iteration
Together there are n(n-1) +
2(n-1) major operations
This
is n2 n + 2n 2 or n2 + n 2
This makes it the
fastest O(n2) algorithm
Analysis of Insertion Sort (cont)
If the list is sorted (or almost) this runs very
quickly.
Runs
in O(n) time
On a short list, or nearly sorted list, this is not a
bad algorithm.
In the worst case, with a long list, n(n-1)/2 comparisons is too slow
Runs in O(n2)
Upper bound
O (n2)
means that Insertion sort will never take more time than n2 (multiplied by some constant,
with n sufficiently large)
Shellsort
This works by comparing distant elements (not
adjacent)
How far apart the
elements compared are is called the interval
Example: use the intervals 5, 3, 1
Uses an insertion sort on sub-arrays
With the right intervals, runs in O(n3/2)
time