Title: Introduction to Sorting I
1Introduction to Sorting I
- Overview
- What is Sorting?
- Some Useful Array Handling Methods.
- Selection Sort and its Implementation.
- Brief Analysis of Selection Sort.
- Insertion Sort and its Implementation.
- Brief Analysis of Insertion sort.
- Preview Introduction to Sorting II
2What is sorting?
- The efficiency of data handling can be
substantially increased if the data is sorted.
Imagine searching for a word in a dictionary in
which the words are not sorted! - Sorting is the process of re-arranging a
collection of data items according to some
key-field. - It is a very common activity in data management.
Even when a list is maintained in some order,
there is often a need to re-arrange the list in a
different order. - Because it takes so much processing time, sorting
is a serious topic in computer science, about
which many different algorithms have been
written. - However, the question of which sorting algorithms
is more efficient is an open question as this
depends on many factors, including - the size of the data
- the initial arrangement of the data random,
sorted, partially sorted or in reverse order - the structure of the data.
- We shall undertake detailed analysis of a number
of these algorithms in ICS202 using an analysis
method called Big-O notation. - For now we shall make a brief analysis on each
algorithm we consider based on two criteria - the
number of comparisons and the number of data
movements involved. - In this lecture we consider two simple algorithms
- Selection Sort and Insertion sort.
3Some Useful Array Handling Methods
- Swapping operation is common in most of the
sorting algorithms we shall consider. - Also to test each algorithm, we need to generate
an array of random values and print it before and
after sorting. - Thus, we begin by looking at a class called
ArrayUtil, which encapsulates these useful
operations.
import java.util.Random public class ArrayUtil public static int randomIntArray(int length, int n) int a new intlength Random generator new Random() for (int i 0 i lt a.length i) ai generator.nextInt(n) return a public static void swap(int a, int i, int j) int temp ai ai aj aj temp public static void print(int a) for (int i 0 i lt a.length i) System.out.print(ai " ") System.out.println()
4Selection Sort and Its Implementation
- This includes the following steps
- 1. Find the smallest (or largest) item in the
list - 2. Swap this item with the item at the beginning
of the list - 3. Repeat steps 1 and 2 using the remaining items
in the list until it remains only one item in the
list.
5Selection Sort and Its Implementation (Contd)
public class SelectSort public static int minimumPosition(int a, int from) int minPos from for (int i from 1 i lt a.length i) if (ai lt aminPos) minPos i return minPos public static void sort(int a) for (int n 0 n lt a.length - 1 n) int minPos minimumPosition(a, n) if (minPos ! n) ArrayUtil.swap(a, minPos, n) public class SelectSortTest public static void main(String args) int a ArrayUtil.randomIntArray(20, 100) ArrayUtil.print(a) SelectSort.sort(a) ArrayUtil.print(a)
Index Original Round1 R2 R3
0 7 1 1 1
1 2 2 2 2
2 1 7 7 4
3 4 4 4 7
6Brief Informal Analysis of Selection Sort
- Selection Sort has the advantage of simplicity
and is adequate for small list of say, few
hundred items. - The main loop executes n (number of items) times.
Moreover, in each iteration, it has to search
the rest of the items to find the minimum about
n comparisons maximum. Thus it needs at most n2
comparison. - However, the number of data movements is much
less - at most one for each iteration, thus
giving a maximum of n data movements. In fact if
the array is already sorted, no data movement is
involved. - However, because of the n2 comparisons, selection
sort is regarded as a quadratic sorting method. - One problem with selection sort is that it is not
intelligent to know that the array is already
sorted or partially sorted . That is, it
performs the same number of comparison regardless
of the original order of the array.
7Insertion Sort and Its Implementation
- This method works as follows
- 1. Starting with i1, take the ith element to be
the current element - 2. Scan the array from i-1 down to 0, shifting
each element greater than the current element
down by one position. - 3. Insert the current element where the shifting
in step 2 stops. - 4. Repeat steps 1 to 3 for i2, 3, , n-1 where
n is the size of the array.
for (n 1 n lt a.length n) current
an for (j n jgt0 current lt
aj-1 j--) aj
aj-1 aj current
Index Original Round1 Round2 Round3
0 7 2 1 1
1 2 7 2 2
2 1 1 7 4
3 4 4 4 7
8Insertion Sort
9Insertion Sort and Its Implementation (contd)
public class InsertSort public static void sort(int a) int current, n, j for (n 1 n lt a.length n) current an for (j n jgt0 current lt aj-1 j--) aj aj-1 aj current
public class InsertSortTest public static void main(String args) int a ArrayUtil.randomIntArray(20, 100) ArrayUtil.print(a) InsertSort.sort(a) ArrayUtil.print(a)
10Brief Informal Analysis of Insertion Sort
- Like Selection sort, Insertion sort also has the
advantage of simplicity. - Again the main loop executes n-times.
- The number of comparison is usually much less
than that of Selection sort since the comparison
stops once an element greater or equal to the
current element is found. - In fact, the best case is when the array is
already sorted in which case there would be only
one comparison in each iteration, giving a total
of n. - However, in the worst case, when data is in
reverse order, there could be up to n comparisons
in the last iteration. Thus on the average (for
random data) we have approximately ½ n2
comparisons. - The main disadvantage of insertion sort is with
regards to number of data movements. In each
iteration, the current element has to be placed
in its correct place by moving each element
greater than the current by one. Thus on the
average we have ½ n2 data movements. - Thus, Insertion sort is also regarded as a
quadratic sorting method.
11Applet
- http//blackcat.brynmawr.edu/spoonen/JavaProject/
sorter.html