Copyright © 2003-2011 Curt Hill Sorting Ordering an array.

Slides:



Advertisements
Similar presentations
Recursion Chapter 14. Overview Base case and general case of recursion. A recursion is a method that calls itself. That simplifies the problem. The simpler.
Advertisements

Equality Join R X R.A=S.B S : : Relation R M PagesN Pages Relation S Pr records per page Ps records per page.
Algorithms Analysis Lecture 6 Quicksort. Quick Sort Divide and Conquer.
Copyright © 2009 Curt Hill Self Organizing Lists Another form of searchable list.
CompSci Searching & Sorting. CompSci Searching & Sorting The Plan  Searching  Sorting  Java Context.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
SORTING ROUTINES. OBJECTIVES INTRODUCTION BUBBLE SORT SELECTION SORT INSERTION SORT QUICK SORT MERGE SORT.
Efficient Sorts. Divide and Conquer Divide and Conquer : chop a problem into smaller problems, solve those – Ex: binary search.
Sorting. “Sorting” When we just say “sorting,” we mean in ascending order (smallest to largest) The algorithms are trivial to modify if we want to sort.
Data Structures Data Structures Topic #13. Today’s Agenda Sorting Algorithms: Recursive –mergesort –quicksort As we learn about each sorting algorithm,
Sorting Chapter 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.
1 Sorting Problem: Given a sequence of elements, find a permutation such that the resulting sequence is sorted in some order. We have already seen: –Insertion.
Data Structures Advanced Sorts Part 2: Quicksort Phil Tayco Slide version 1.0 Mar. 22, 2015.
Sorting21 Recursive sorting algorithms Oh no, not again!
1 Sorting/Searching CS308 Data Structures. 2 Sorting means... l Sorting rearranges the elements into either ascending or descending order within the array.
Lecture 25 Selection sort, reviewed Insertion sort, reviewed Merge sort Running time of merge sort, 2 ways to look at it Quicksort Course evaluations.
Simple Sorting Algorithms
Quicksort. Quicksort I To sort a[left...right] : 1. if left < right: 1.1. Partition a[left...right] such that: all a[left...p-1] are less than a[p], and.
CHAPTER 11 Sorting.
Quicksort.
C++ Plus Data Structures
Selection Sort, Insertion Sort, Bubble, & Shellsort
Sorting CS-212 Dick Steflik. Exchange Sorting Method : make n-1 passes across the data, on each pass compare adjacent items, swapping as necessary (n-1.
Sorting Algorithms and Analysis Robert Duncan. Refresher on Big-O  O(2^N)Exponential  O(N^2)Quadratic  O(N log N)Linear/Log  O(N)Linear  O(log N)Log.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
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.
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.
1 Time Analysis Analyzing an algorithm = estimating the resources it requires. Time How long will it take to execute? Impossible to find exact value Depends.
Sorting HKOI Training Team (Advanced)
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
Chapter 12 Recursion, Complexity, and Searching and Sorting
Computer Science Searching & Sorting.
Merge Sort. What Is Sorting? To arrange a collection of items in some specified order. Numerical order Lexicographical order Input: sequence of numbers.
Chapter 10 B Algorithm Efficiency and Sorting. © 2004 Pearson Addison-Wesley. All rights reserved 9 A-2 Sorting Algorithms and Their Efficiency Sorting.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 19: Searching and Sorting.
1 Joe Meehean.  Problem arrange comparable items in list into sorted order  Most sorting algorithms involve comparing item values  We assume items.
Chapter 7: Sorting Algorithms Insertion Sort. Sorting Algorithms  Insertion Sort  Shell Sort  Heap Sort  Merge Sort  Quick Sort 2.
© 2006 Pearson Addison-Wesley. All rights reserved10 A-1 Chapter 10 Algorithm Efficiency and Sorting.
© 2006 Pearson Addison-Wesley. All rights reserved10 B-1 Chapter 10 (continued) Algorithm Efficiency and Sorting.
Sorting Chapter 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.
1 C++ Plus Data Structures Nell Dale Chapter 10 Sorting and Searching Algorithms Slides by Sylvia Sorkin, Community College of Baltimore County - Essex.
Java Methods Big-O Analysis of Algorithms Object-Oriented Programming
3 – SIMPLE SORTING ALGORITHMS
Sorting and Searching. Selection Sort  “Search-and-Swap” algorithm 1) Find the smallest element in the array and exchange it with a[0], the first element.
Chapter 8 Sorting and Searching Goals: 1.Java implementation of sorting algorithms 2.Selection and Insertion Sorts 3.Recursive Sorts: Mergesort and Quicksort.
Review 1 Selection Sort Selection Sort Algorithm Time Complexity Best case Average case Worst case Examples.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
Sorting and Searching by Dr P.Padmanabham Professor (CSE)&Director
Sorting – Part II CS 367 – Introduction to Data Structures.
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.
Data Structures - CSCI 102 Selection Sort Keep the list separated into sorted and unsorted sections Start by finding the minimum & put it at the front.
Searching and Sorting Searching: Sequential, Binary Sorting: Selection, Insertion, Shell.
Chapter 9 sorting. Insertion Sort I The list is assumed to be broken into a sorted portion and an unsorted portion The list is assumed to be broken into.
CSE 326: Data Structures Lecture 23 Spring Quarter 2001 Sorting, Part 1 David Kaplan
Sorting and Searching Algorithms CS Sorting means... l The values stored in an array have keys of a type for which the relational operators are.
Computer Science 1620 Sorting. cases exist where we would like our data to be in ascending (descending order) binary searching printing purposes selection.
PREVIOUS SORTING ALGORITHMS  BUBBLE SORT –Time Complexity: O(n 2 ) For each item, make (n –1) comparisons Gives: Comparisons = (n –1) + (n – 2)
Sorting Ordering data. Design and Analysis of Sorting Assumptions –sorting will be internal (in memory) –sorting will be done on an array of elements.
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
Copyright © 2009 Curt Hill Look Ups A Recurring Theme.
Sorting and Runtime Complexity CS255. Sorting Different ways to sort: –Bubble –Exchange –Insertion –Merge –Quick –more…
Prof. U V THETE Dept. of Computer Science YMA
Sorting With Priority Queue In-place Extra O(N) space
A Kind of Binary Tree Usually Stored in an Array
8/04/2009 Many thanks to David Sun for some of the included slides!
Sub-Quadratic Sorting Algorithms
Chapter 10 Sorting Algorithms
Searching/Sorting/Searching
Presentation transcript:

Copyright © Curt Hill Sorting Ordering an array

Copyright © Curt Hill Considered Topics Simple sort schemes –Algorithms with some code More complicated sort schemes Performance considerations –Time to sort in terms of the number of records N –How the number of compares and moves relate to the size of N

Copyright © Curt Hill Selection Sort Basic idea Scan the entire array Find the smallest element Move to the top Remove the top from further consideration Repeat until entire array is sorted

Copyright © Curt Hill How it works Top element Least element Sorted part of array Unsorted part of array

Copyright © Curt Hill Code void sort(int ar[], int size){ int temp; for(int i=0;i<size-1;i++){ temp = i; for (int j=i+1;j<size;j++) if(ar[temp]>ar[j]) temp = j; if(temp!=i) { int val =ar[i]; ar[i] = ar[temp]; ar[temp] = val; } //swap } // outer for }

Copyright © Curt Hill Best and Worst Cases This is an unusual algorithm in that the best and worst case are almost the same Best case –Already sorted –No moves are needed –All the compares are still done Worst case –Inversely sorted –Same number of compares –N-1 moves

Copyright © Curt Hill How it performs The first element is compared with all the other elements –N-1 compares The second element is compared with remaining –N-2 compares Compares: (N-1)+(N-2)+…1  (N-1) 2 /2 Moves N-1

Copyright © Curt Hill Comparing running times Mostly we are not concerned with many of the little issues of this analysis –It is N-1 instead of N –There is a factor of N 2 /2 instead of N 2 When we have two different factors we always take the most expensive –N 2 compares instead of N moves Thus selection sort is O(N 2 )

Copyright © Curt Hill Common Os Constant time O(c) or O(1) –Hashing is constant time Logarithmic time O(log 2 N) –Binary and tree searches Linear time O(N) –File scans, bad searches N log N, O(N log 2 N) – no other name –Good sorts N Squared O(N 2 ) –Bad sorts Polynomial O(N X ) –Expensive but doable Exponential O(e N ) –Intractable

Copyright © Curt Hill Bubble Sort Basic idea Start at top Compare adjacent elements Exchange if out of order Repeat until a pass has no exchanges

Copyright © Curt Hill First Pass Small items bubble up slowly –One element per pass Large items sink quickly –Keep descending until they find a larger item or hit bottom

Copyright © Curt Hill Code void sort (int ar[], int size){ bool swapped; do { swapped = false; for(int j = 0;j<size-1;j++) if(ar[j] < ar[j+1]){ int temp = ar[j]; ar[j] = ar[j+1]; ar[j+1] = temp; swapped = true; } // if } // do while(swapped); }

Copyright © Curt Hill How it performs Bubble sort makes many moves but always a short distance It also does many redundant compares O(N 2 ) Big O notation makes this comparable with selection –Usually much worse –Have to be creative to make a worse sort

Copyright © Curt Hill Best and Worst Cases Best case –Already sorted –One pass through does no exchanges and quits Worst case –Inversely sorted –The smallest only moves up one –N-1 passes –The case of all elements sorted except first element is in last slot is almost as bad

Copyright © Curt Hill Bubble Again Consider two symmetric cases, sorted with one exception: largest or smallest as far away as possible –One takes two passes the other N-1 The problem is the direction of the scan –Items going in that direction move fast –Items going other direction slowly This suggests a fix

Copyright © Curt Hill Shaker Sort Basic idea is same as bubble sort Scan top to bottom in odd passes Scan bottom to top in even passes

Copyright © Curt Hill First and Second Passes First pass goes top to bottom Second pass goes bottom to top

Copyright © Curt Hill How it performs Insignificantly different The worst case occurs very infrequently The extra work to handle them complicates every run O(N 2 )

Copyright © Curt Hill The Previous Problems The problem with both of these is the short distance things are moved They usually move in the right direction but seldom far enough One fix is to compare non-adjacent elements How?

Copyright © Curt Hill Shell Sort Start with a gap g, where 1  g  N Do a sort pass comparing elements separated by the gap and exchanging if needed Decrease the gap in each pass –Do not divide size by 2 When the gap is one it is a bubble sort but most of the large distance moving has been done

Copyright © Curt Hill First Pass First: 8 and 1 exchanged Third: 14 and 2 exchanged Fourth: 6 and 14 exchanged Gap =

Copyright © Curt Hill How it performs The analysis is extremely difficult Empirically the O(N 1.25 ) This makes it better for any all but insignificant table size than bubble or selection The break even point between O(N 1.25 ) and O(N log 2 N) is size=65000, however the constant factor on Shell is large so the break even point is much smaller Still inferior to the N log N sorts for large tables

Copyright © Curt Hill Insertion Sort Partition the array into two pieces The first one and all the rest The first part of the array is already sorted Remove the first unsorted item Insert into the correct location of the sorted part

Copyright © Curt Hill How it works Sorted part of array Unsorted part of array Remove Insert

Copyright © Curt Hill How it performs Best case is sorted Worst case is inversely sorted Yet another N 2 Moves N-1

Copyright © Curt Hill Merge Sort Merge increasingly larger sorted runs into a single much larger run Start with runs of 1 Merge two runs into a temporary area Copy it back

Copyright © Curt Hill Pass Start: Each element is a run of Run 1 Run 3 Run 2 Run 4 End of pass 1: runs of 2 Run 1 Run 2 Run 3 Run 4 Run 5 Run 6 Run 7

Copyright © Curt Hill Pass Runs of Run 1 Run 2 Runs of 4 Run 1 Run 2 Run 3 Run

Copyright © Curt Hill Important points on Merge An item in a run can never be compared with any other element in the same run It generalizes to files nicely Requires extra copy space equal to the size of longest run In last pass that is entire array First of O(N log 2 N) sorts The insertion process generates many moves

Copyright © Curt Hill Quick Sort A complicated but very fast sort –Usually the best of the in memory sorts Never compares two items twice Always moves things in the right direction Usually moves them a relatively long distance

Copyright © Curt Hill Algorithm I The first item is called the pivot –It will be the middle element From the top look for an item that is larger From the bottom look for an item that is smaller The two items are respectively in the wrong “half” of the table –Recall the pivot will be the middle item Exchange the two

Copyright © Curt Hill Algorithm II When searches collide move the pivot there Now have three partitions: –Lower – sort it by itself –Pivot – nothing more needs to be done –Higher – sort it by itself

Copyright © Curt Hill Quick Sort Start, pivot is 8 start looking st exch 2 nd exch Pivot exch Done found Three partitions

Copyright © Curt Hill Performance A pair of distinct keys are never compared twice The trick is partitioning the array into two separate pieces that never interact again (½ N) 2 + (½ N) 2 < N 2 –20 2 =400 – = 200 O(N log 2 N)

Copyright © Curt Hill More on Performance A happy accident is that the pivot may be placed in a CPU register –It is the only value compared to the entire array –This makes it free and quick to access Notice the recursive nature of this algorithm –The array is partitioned into two pieces –These are different sizes and the sort is recursively invoked on them

Copyright © Curt Hill Best and Worst Case It does better on unsorted file than sorted –Counter-intuitive The worst case is the sorted or inversely sorted file –The chosen partition divides the table into two, not three, partitions –N 2 In this case

Copyright © Curt Hill Improvements 1 The worst case makes one think about choosing a different pivot Any searching for a pivot will slow the average process with a search The case of a sorted array to be sorted is extremely unlikely –For 10 elements –2 chances in for it to be already sorted

Copyright © Curt Hill Improvements 2 The partitioning scheme is complicated enough that it does worse than simple sorts in very small arrays: 6-12 entries –Recursion to sort an table of length 3 is wasteful in memory and CPU cycles The only real improvement is to use a simpler sort when the partition size gets small –If the partition is small just use a simple N 2 sort

Two more thoughts Virtual memory can disrupt sorting when pieces of the array are paged out –True for any sort –If possible fix the pages Quick sort could use threads –Spawn a thread for one of the partitions if it were of sufficient size –Would need to be large to make thread overhead worth while Copyright © Curt Hill

Heap Sort Builds a binary tree in the array The positions of the left and right sub-trees are implicit rather than needing pointers Also O(N log 2 N) sort Rather complicated Will not be shown

Copyright © Curt Hill Heap Sort Performance Slowest of the O(N log 2 N) sorts Advantages: –Does not need recursion of quicksort –Does not need extra space of mergesort –Worst case is still O(N log 2 N) unlike other two

Summary Several sorts with varying performance: –N 2 : Selection, Bubble, Shaker, Insertion –N 1.25 : Shell –N log 2 N: Merge, Quick, Heap Copyright © Curt Hill