Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04 B Smith: Save until Week 15? B Smith: Save until Week 15? B Smith: Skipped Spring 2005?

Slides:



Advertisements
Similar presentations
CMPT 225 Sorting Algorithms Algorithm Analysis: Big O Notation.
Advertisements

CS 253: Algorithms Chapter 2 Sorting Insertion sort Bubble Sort Selection sort Run-Time Analysis Credit: Dr. George Bebis.
Chapter 7: Sorting Algorithms
© 2006 Pearson Addison-Wesley. All rights reserved10-1 Chapter 10 Algorithm Efficiency and Sorting CS102 Sections 51 and 52 Marc Smith and Jim Ten Eyck.
Simple Sorting Algorithms
TDDB56 DALGOPT-D DALG-C Lecture 8 – Sorting (part I) Jan Maluszynski - HT Sorting: –Intro: aspects of sorting, different strategies –Insertion.
Algorithm Efficiency and Sorting
Analysis of Algorithms CS 477/677
Chapter 7 (Part 2) Sorting Algorithms Merge Sort.
C++ for Engineers and Scientists Third Edition
Sorting 2 An array a is sorted (ascending order) if: for all i a[i]  a[j] Probably the most well-studied algorithmic problem in Computer Science There.
Sorting II/ Slide 1 Lecture 24 May 15, 2011 l merge-sorting l quick-sorting.
CSE 373 Data Structures Lecture 19
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.
Data Structures/ Algorithms and Generic Programming Sorting Algorithms.
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 م. م علي عبد الكريم حبيب.
CHAPTER 7: SORTING & SEARCHING Introduction to Computer Science Using Ruby (c) Ophir Frieder at al 2012.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
CSC 211 Data Structures Lecture 15
COMP s1 Computing 2 Complexity
Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.
1 Data Structures and Algorithms Sorting. 2  Sorting is the process of arranging a list of items into a particular order  There must be some value on.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
TDDB56 DALGOPT-D – Lecture 8 – Sorting (part I) Jan Maluszynski - HT Content: Lecture 8: Sorting part I: –Intro: aspects of sorting, different.
CS 1704 Introduction to Data Structures and Software Engineering.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
Searching. The process used to find the location of a target among a list of objects Searching an array finds the index of first element in an array containing.
1 Data Structures and Algorithms Sorting I Gal A. Kaminka Computer Science Department.
1 Joe Meehean.  Problem arrange comparable items in list into sorted order  Most sorting algorithms involve comparing item values  We assume items.
SortingBigOh Sorting and "Big Oh" Adapted for ASFA from a presentation by: Barb Ericson Georgia Tech Aug 2007 ASFA AP Computer Science.
Dale Roberts Sorting Dale Roberts, Lecturer Computer Science, IUPUI Department of Computer and Information Science, School.
CSC 211 Data Structures Lecture 13
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
1 Today’s Material Iterative Sorting Algorithms –Sorting - Definitions –Bubble Sort –Selection Sort –Insertion Sort.
Sorting – Insertion and Selection. Sorting Arranging data into ascending or descending order Influences the speed and complexity of algorithms that use.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
Comparison of Optimization Algorithms By Jonathan Lutu.
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.
LAB#6. 2 Overview Before we go to our lesson we must know about : 1. data structure. 2.Algorithms. data structure is an arrangement of data in a computer.
CSCI 51 Introduction to Programming March 12, 2009.
1 Introduction to Sorting Algorithms Sort: arrange values into an order Alphabetical Ascending numeric Descending numeric Two algorithms considered here.
3 – SIMPLE SORTING ALGORITHMS
Quicksort CSE 2320 – Algorithms and Data Structures Vassilis Athitsos University of Texas at Arlington 1.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Data Structures and Algorithms Lecture 17, 18 and 19 (Sorting) Instructor: Quratulain Date: 10, 13 and 17 November, 2009 Faculty of Computer Science, IBA.
ICS201 Lecture 21 : Sorting King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
IS 2610: Data Structures Discuss HW 2 problems Binary Tree (continued) Introduction to Sorting Feb 9, 2004.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
Week 13 - Wednesday.  What did we talk about last time?  NP-completeness.
Review 1 Merge Sort Merge Sort Algorithm Time Complexity Best case Average case Worst case Examples.
Today’s Material Sorting: Definitions Basic Sorting Algorithms
Example Algorithms CSE 2320 – Algorithms and Data Structures Alexandra Stefan University of Texas at Arlington Last updated 1/31/
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.
SORTING Sorting is storage of data in some order, it can be in ascending or descending order. The term Sorting comes along-with the term Searching. There.
Sorting.
Sorting With Priority Queue In-place Extra O(N) space
Sorting Mr. Jacobs.
Introduction to Search Algorithms
Simple Sorting Algorithms
Chapter 7 Sorting Spring 14
Describing algorithms in pseudo code
8/04/2009 Many thanks to David Sun for some of the included slides!
Searching.
Simple Sorting Algorithms
CSE 373 Data Structures and Algorithms
Simple Sorting Algorithms
Simple Sorting Algorithms
Presentation transcript:

Math 130 Introduction to Computing Sorting Lecture # 17 10/11/04 B Smith: Save until Week 15? B Smith: Save until Week 15? B Smith: Skipped Spring 2005? B Smith: Skipped Spring 2005?

B. Smith2 Overview Sorting Selection Sort –Animation –Code –Analysis of Selection Sort Exchange/Bubble Sort –Animation –Code –Analysis of Bubble Sort

B. Smith3 Sorting An array a is sorted (ascending order) if: for all i a[i]  a[j] Probably the most well-studied algorithmic problem in Computer Science There are many algorithms –We will study 2 (others will be studied in M140) –Why not just one? Historical Differ in ease of programming Differ in performance What’s “best” sometimes depends on the problem

B. Smith4 Sorting in the Real World Sorting is one of the most common operations in “data mining” Often need to sort enormous sets of data –Don’t fit in memory (RAM) These are called external sorts – need to store intermediate results on disks –Can use multiple machines (parallelism) to make sorting run faster Divide and Conquer algorithms (M140) When you are faced with the job of sorting: –What sort routines are available? The “quicksort” algorithm is commonly available on many platforms –time is proportional to N*log( N ) on average to sort N items –What does your data look like? partially sorted? reverse order sorted 90% of the time?

B. Smith5 What Affects Choice of Algorithm? How large is the set of things? Are you sorting an array or a linked list? Are you sorting it all at once, or are elements coming in one at a time (incremental)? Is the data likely to be (nearly) sorted when you start? How much time to do you have to write and debug the code? How much extra space can you afford, or (related) how expensive is object allocation/deallocation? –An “in-place” sorting algorithm uses only O(1) space in addition to the input data structure Are there sorting implementations you can use? –Big idea -- don't reinvent the wheel!

B. Smith6 Selection Sort Selection sort idea: –find smallest element in the array and exchange it with the element in the first position. –find second smallest element and exchange it with the element in the second position. Do this (n-1) times find min input

B. Smith7 void selection(int a[], int l, int r) { for (int i = l; i < r; i++) { int min = i; for (int j = i+1; j <= r; j++) if (a[j] < a[min]) min = j; swap(&a[i], &a[min]); } min to be index of smallest value Find smallest of entire list Swap smallest with next unsorted I = 0 I = 1I =2 Selection Sort Code (RS) Implementation of Selection Sort: void swap(int* a, int* b) { int t; t= *a; *a = *b; *b = t; return;}

B. Smith8 For I = 0 –for j=1, j <= N, find min –The inner j loop iterates (N-1) times For I = 1 –for j=2, j <= N, find min –The inner j loop iterates (N-2) times For I = 2 –for j=3, j <= N, find min –The inner j loop iterates (N-3) times e.g., N=5, Iterate thru a[1] to a[4], 4 loops e.g., Iterate thru a[2] to a[4], 3 loops e.g., Iterate thru a[3] to a[4], 2 loops For the last subarray, the number of iterations is just 1 In general, total number of iterations (& comparisons) is: (N - 1) + (N - 2) + (N - 3) + … = N(N - 1)/2 In general, total number of iterations (& comparisons) is: (N - 1) + (N - 2) + (N - 3) + … = N(N - 1)/2 N = number of elements in array Selection Sort Analysis

B. Smith9 Selection Sort Analysis What is the worst input for selection sort? Analysis of the worst case: –Outer loop executes N-1 times (no exch rqd. for final element). –Inner loop executes N-i-1 comparisons. (N-1) + (N-2) + … = N*(N-1)/2 = O(N 2 ) Max number of exchanges is proportional to array size (one exchange per outer loop). Worst case: Advantages of selection sort: –Easy to write –Can be done “in place” –Can be done on linked lists too (keep a tail pointer) Disadvantage: –It is about N 2 even in the best case for comparisons –So the running time (over all inputs) is approximately O(N 2 )

B. Smith10 Improving Selection Sort –Can find the min in a balanced tree in O(log n) May use a tree to improve the insert time for insertion sort or find- min time for selection sort Produce O(n log n) sorting algorithms But if we’re sorting an array, that’s a lot of code And the constant may be high for object allocation and pointer manipulation –Can find a min in O(log n) time using a “heap” Heap sort like selection sort, but maintains unsorted part in heap Complicated to write, but can be done in-place Selection sort finds minimum by linear search Can we do better than this?

B. Smith11 Exchange (“Bubble”) Sort (RS) Bubble Sort Idea –Elements next to each other are swapped so that the list gets sorted 1.Start with comparison of first two elements 2.For ascending order, smaller value is placed before larger value –For descending order,smaller value is placed after larger value 3.Continue until last two items are compared and evaluated for exchange 4.When you can go through the list with no exchanges, the elements are sorted

B. Smith12 Bubble Sort – pass

B. Smith Bubble Sort pass 1

B. Smith14 Bubble Sort - pass

B. Smith15 Bubble Sort Note that the largest value always sank to the bottom of list The smaller elements slowly rose or “bubbled” up from the bottom Each pass always places the largest in the list at the bottom –Hence, no need to do “compare and exchange” for the final two elements –For each pass, one less compare/exchange operation is required

B. Smith16 Bubble Sort – Analysis Similar to Selection Sort –number of comparisons is O(N 2 ). –number of moves depends on initial order of the values in the list Worst case analysis –Occurs when list items are in reverse sorted order this is when Selection Sort beats Bubble Sort. –Although both require N(N-1)/2 comparisons… »Selection Sort needs only N-1 moves »Bubble Sort needs N(N-1)/2 moves For random data –Selection Sort generally performs as well as or better than bubble sort

B. Smith17 Bubble Sort Code (RS) void bubble(int a[], int l, int r) { int i, j; for (i = l; i < r; i++) for (j = r; j > i; j--) compswap(a[j-1], a[j]); } #define key(A) (A) #define less(A, B) (key(A) < key(B)) #define compswap(A, B) if(less(B, A)) swap(A, B) B Smith: These macros may cause problems. Consider converting to subroutines B Smith: These macros may cause problems. Consider converting to subroutines

B. Smith18 Summary Sorting Selection Sort Animation Code Analysis of Selection Sort Exchange/Bubble Sort Animation Code Analysis Bubble Sort Analysis

B. Smith19 Extra Slides

B. Smith20 Generic Sort Template #include typedef int Item; #define key(A) (A) #define less(A, B) (key(A) < key(B)) #define exch(A, B) { Item t = A; A = B; B = t; } #define compexch(A, B) if (less(B, A)) exch(A, B) void sort(Item a[], int l, int r) //Insertion Sort (R.S.) { int i, j; for (i = l+1; i <= r; i++) for (j = i; j > l; j--) compexch(a[j-1], a[j]); } // This program takes optional command line arguments as input // e.g. "Sort01 5 1" will give you 5 random integers and // "Sort " will give you 99 random integers // and "Sort01" will allow you to input as many integers as you desire main(int argc, char *argv[]) { int i, N = atoi(argv[1]), sw = atoi(argv[2]); //srand(time(NULL)); int *a = malloc(N*sizeof(int)); /*dynamic array allocation */ if (sw) for (i = 0; i < N; i++) a[i] = 1000*(1.0*rand()/RAND_MAX); else for (N = 0; scanf("%d", &a[N]) == 1 ; N++) ; //enter floating pt number or a character to quit entry sort(a, 0, N-1); // Generic enough to use with any Sort function for (i = 0; i < N; i++) printf("%3d ", a[i]); printf("\n"); }