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

Slides:



Advertisements
Similar presentations
CSE Lecture 3 – Algorithms I
Advertisements

Sorting A fundamental operation in computer science (many programs need to sort as an intermediate step). Many sorting algorithms have been developed Choose.
Selection and Insertion Sort Mrs. C. Furman October 1, 2008.
 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
HST 952 Computing for Biomedical Scientists Lecture 9.
Sorting Algorithms. Motivation Example: Phone Book Searching Example: Phone Book Searching If the phone book was in random order, we would probably never.
1 Lecture 23 Searching Overview  What is Searching?  Linear Search and its Implementation.  Brief Analysis of Linear Search.  Binary Search and its.
CSE 373: Data Structures and Algorithms
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
1 Sorting Algorithms (Part II) Overview  Divide and Conquer Sorting Methods.  Merge Sort and its Implementation.  Brief Analysis of Merge Sort.  Quick.
Simple Sorting Algorithms
1 ICS103 Programming in C Lecture 14: Searching and Sorting.
Algorithm Efficiency and Sorting
1 Searching Algorithms Overview  What is Searching?  Linear Search and its Implementation.  Brief Analysis of Linear Search.  Binary Search and its.
1 Search & Sorting Using Java API Searching and Sorting Using Standard Classes  Searching data other than primitive type.  Comparable interface.  Implementing.
Analysis of Algorithm.
Sorting Arrays. Selection Sort  One of the easiest ways to sort the elements of an array is by using the selection sort algorithm.  Assume that the.
1 Sorting Algorithms (Part I) Sorting Algoritms (Part I) Overview  What is Sorting?  Some Useful Array Handling Methods.  Selection Sort and its Implementation.
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
Simple Sorting Algorithms. 2 Outline We are going to look at three simple sorting techniques: Bubble Sort, Selection Sort, and Insertion Sort We are going.
Advance Data Structure 1 College Of Mathematic & Computer Sciences 1 Computer Sciences Department م. م علي عبد الكريم حبيب.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Intro to CS – Honors I Basic Sorting GEORGIOS PORTOKALIDIS
CSC 211 Data Structures Lecture 15
Chapter 14: Sorting and searching. Chapter Goals To study several sorting and searching algorithms To appreciate that algorithms for the same task can.
Lecture 5 Searching and Sorting Richard Gesick. The focus Searching - examining the contents of the array to see if an element exists within the array.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
CSE 373 Data Structures and Algorithms
Lecture 6 Sorting Algorithms: Bubble, Selection, and Insertion.
Chapter 15: Algorithms 1 ©2000, John Wiley & Sons, Inc. Horstmann/Java Essentials, 2/e 1 Chapter 15 Algorithms.
SortingBigOh Sorting and "Big Oh" Adapted for ASFA from a presentation by: Barb Ericson Georgia Tech Aug 2007 ASFA AP Computer Science.
1 ICS103 Programming in C Lecture 14: Searching and Sorting.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
BUBBLE SORT. Introduction Bubble sort, also known as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to.
1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.
Data Structure Introduction.
Comparison-Based Sorting & Analysis Smt Genap
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
Sorting Dr. Yingwu Zhu. Sorting Consider list x 1, x 2, x 3, … x n We seek to arrange the elements of the list in order Ascending or descending Some O(n.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
CS 162 Intro to Programming II Bubble Sort 1. Compare adjacent elements. If the first is greater than the second, swap them. Do this for each pair of.
3 – SIMPLE SORTING ALGORITHMS
1 Sorting (Bubble Sort, Insertion Sort, Selection Sort)
Lecture No. 04,05 Sorting.  A process that organizes a collection of data into either ascending or descending order.  Can be used as a first step for.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Chapter 9 Sorting 1. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
© 2005 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. Data Structures for Java William H. Ford William R. Topp Chapter 4 Introduction.
Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Shell Sort. Invented by Donald Shell in 1959, the shell sort is the most efficient of the O(n²) class of sorting algorithms. Of course, the shell sort.
CS 162 Intro to Programming II Sorting Introduction & Selection Sort 1.
Basic Sorting Algorithms Dr. Yingwu Zhu. Sorting Problem Consider list x 1, x 2, x 3, … x n Goal: arrange the elements of the list in order Ascending.
Chapter 16: Searching, Sorting, and the vector Type.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part 8a. Sorting(1): Elementary Algorithms.
1 compares each element of the array with the search key. works well for small arrays or for unsorted arrays works for any table slow can put more commonly.
Sort Algorithm.
ICS103 Programming in C Lecture 14: Searching and Sorting
Alternate Version of STARTING OUT WITH C++ 4th Edition
Lecture 14 Searching and Sorting Richard Gesick.
Simple Sorting Algorithms
Objectives At the end of the class, students are expected to be able to do the following: Understand the purpose of sorting technique as operations on.
Describing algorithms in pseudo code
CSC215 Lecture Algorithms.
Lecture 11 Searching and Sorting Richard Gesick.
Principles of Computing – UFCFA3-30-1
Simple Sorting Algorithms
Simple Sorting Algorithms
Principles of Computing – UFCFA3-30-1
Simple Sorting Algorithms
Presentation transcript:

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 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 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 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 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 OriginalRound1R2R

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 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. IndexOriginalRound1Round2Round

8 Lecture 21 Insertion Sort

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 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.