Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Lecture 21 Introduction to Sorting I Overview  What is Sorting?  Some Useful Array Handling Methods.  Selection Sort and its Implementation.  Brief.

Similar presentations


Presentation on theme: "1 Lecture 21 Introduction to Sorting I Overview  What is Sorting?  Some Useful Array Handling Methods.  Selection Sort and its Implementation.  Brief."— Presentation transcript:

1 1 Lecture 21 Introduction 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

2 2 Lecture 21 What 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.

3 3 Lecture 21 Some 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 int[length]; Random generator = new Random(); for (int i = 0; i < a.length; i++) a[i] = generator.nextInt(n); return a; } public static void swap(int[] a, int i, int j) { int temp = a[i]; a[i] = a[j]; a[j] = temp; } public static void print(int[] a) { for (int i = 0; i < a.length; i++) System.out.print(a[i] + " "); System.out.println(); }

4 4 Lecture 21 Selection 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.

5 5 Lecture 21 Selection Sort and Its Implementation (Cont’d) public class SelectSort { public static int minimumPosition(int[] a, int from) { int minPos = from; for (int i = from + 1; i < a.length; i++) if (a[i] < a[minPos]) minPos = i; return minPos; } public static void sort (int[] a) { for (int n = 0; n < 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 OriginalRound1R2R3 0 7 111 1 2 222 2 1 774 3 4 447

6 6 Lecture 21 Brief 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 n 2 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 n 2 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.

7 7 Lecture 21 Insertion Sort and Its Implementation  This method works as follows:  1. Starting with i=1, take the i th 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 i=2, 3, …, n-1; where n is the size of the array. IndexOriginalRound1Round2Round3 07211 12722 21174 34447

8 8 Lecture 21 Insertion Sort

9 9 Lecture 21 Insertion Sort and Its Implementation (cont’d) public class InsertSort { public static void sort(int[] a) { int current, n, j; for (n = 1; n < a.length; n++) { current = a[n]; for (j = n; j>0 && current < a[j-1]; j--) a[j] = a[j-1]; a[j] = 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); }}

10 10 Lecture 21 Brief 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 ½ n 2 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 ½ n 2 data movements.  Thus, Insertion sort is also regarded as a quadratic sorting method.


Download ppt "1 Lecture 21 Introduction to Sorting I Overview  What is Sorting?  Some Useful Array Handling Methods.  Selection Sort and its Implementation.  Brief."

Similar presentations


Ads by Google