1 CSE 326: Data Structures Sorting in (kind of) linear time Zasha Weinberg in lieu of Steve Wolfman Winter Quarter 2000.

Slides:



Advertisements
Similar presentations
Sorting in Linear Time Introduction to Algorithms Sorting in Linear Time CSE 680 Prof. Roger Crawfis.
Advertisements

COMP2130 Winter 2015 Storing signed numbers in memory.
Analysis of Algorithms
CENG536 Computer Engineering department Çankaya University.
Analysis of Algorithms CS 477/677 Linear Sorting Instructor: George Bebis ( Chapter 8 )
Sorting in Linear Time Comp 550, Spring Linear-time Sorting Depends on a key assumption: numbers to be sorted are integers in {0, 1, 2, …, k}. Input:
Linear Sorts Counting sort Bucket sort Radix sort.
MS 101: Algorithms Instructor Neelima Gupta
1 Sorting in Linear Time How can we do better?  CountingSort  RadixSort  BucketSort.
CSC 213 – Large Scale Programming. Today’s Goals  Review discussion of merge sort and quick sort  How do they work & why divide-and-conquer?  Are they.
CSE332: Data Abstractions Lecture 14: Beyond Comparison Sorting Dan Grossman Spring 2010.
Lower bound for sorting, radix sort COMP171 Fall 2005.
CSCE 3110 Data Structures & Algorithm Analysis
Floating Point Numbers
CSE332: Data Abstractions Lecture 12: Introduction to Sorting Tyler Robison Summer
CS 447 – Computer Architecture Lecture 3 Computer Arithmetic (2)
Booth’s Algorithm.
Divide and Conquer Sorting
CSE 326: Data Structures Sorting Ben Lerner Summer 2007.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 5 Linear-time sorting Can we do better than comparison sorting? Linear-time sorting.
1 CSE 326: Data Structures: Sorting Lecture 16: Friday, Feb 14, 2003.
1 CSE 326: Data Structures: Sorting Lecture 17: Wednesday, Feb 19, 2003.
Floating Point Numbers.  Floating point numbers are real numbers.  In Java, this just means any numbers that aren’t integers (whole numbers)  For example…
Binary Number Systems.
CSE 373 Data Structures Lecture 15
The Binary Number System
Data Representation Number Systems.
Binary Real Numbers. Introduction Computers must be able to represent real numbers (numbers w/ fractions) Two different ways:  Fixed-point  Floating-point.
Computer Arithmetic Nizamettin AYDIN
Linear Sorts Chapter 12.3, Last Updated: :39 AM CSE 2011 Prof. J. Elder Linear Sorts?
Data Representation - Part II. Characters A variable may not be a non-numerical type Character is the most common non- numerical type in a programming.
Floating Point. Agenda  History  Basic Terms  General representation of floating point  Constructing a simple floating point representation  Floating.
CH09 Computer Arithmetic  CPU combines of ALU and Control Unit, this chapter discusses ALU The Arithmetic and Logic Unit (ALU) Number Systems Integer.
David Luebke 1 10/13/2015 CS 332: Algorithms Linear-Time Sorting Algorithms.
CSC 41/513: Intro to Algorithms Linear-Time Sorting Algorithms.
Binary Fractions. Fractions A radix separates the integer part from the fraction part of a number Columns to the right of the radix have negative.
Introduction to Algorithms Jiafen Liu Sept
CSE332: Data Abstractions Lecture 14: Beyond Comparison Sorting Dan Grossman Spring 2012.
Targil 6 Notes This week: –Linear time Sort – continue: Radix Sort Some Cormen Questions –Sparse Matrix representation & usage. Bucket sort Counting sort.
1 CSE 326: Data Structures Sorting It All Out Henry Kautz Winter Quarter 2002.
Operators & Identifiers The Data Elements. Arithmetic Operators exponentiation multiplication division ( real ) division ( integer quotient ) division.
CISC105 – General Computer Science Class 9 – 07/03/2006.
CSE 326: Data Structures Lecture 24 Spring Quarter 2001 Sorting, Part B David Kaplan
1 Data Representation Characters, Integers and Real Numbers Binary Number System Octal Number System Hexadecimal Number System Powered by DeSiaMore.
CSE 326 Sorting David Kaplan Dept of Computer Science & Engineering Autumn 2001.
Searching and Sorting Recursion, Merge-sort, Divide & Conquer, Bucket sort, Radix sort Lecture 5.
CSE 326: Data Structures Lecture #16 Sorting Things Out Bart Niswonger Summer Quarter 2001.
1 Sorting. 2 Introduction Why is it important Where to use it.
CSC 213 – Large Scale Programming. Bucket-Sort  Buckets, B, is array of Sequence  Sorts Collection, C, in two phases: 1. Remove each element v from.
1 Algorithms CSCI 235, Fall 2015 Lecture 17 Linear Sorting.
O(n) Algorithms. Binsort/Bucketsort: Algorithm /* Put a range of values in bins and then sort the contents of each bin and then output the values of each.
Numbers in Computers.
CS 232: Computer Architecture II Prof. Laxmikant (Sanjay) Kale Floating point arithmetic.
Fixed-point and floating-point numbers Ellen Spertus MCS 111 October 4, 2001.
Lecture 5 Algorithm Analysis Arne Kutzner Hanyang University / Seoul Korea.
CS6045: Advanced Algorithms Sorting Algorithms. Sorting So Far Insertion sort: –Easy to code –Fast on small inputs (less than ~50 elements) –Fast on nearly-sorted.
Fundamentals of Computer Science
Introduction to Algorithms
Algorithm Design and Analysis (ADA)
Lecture 5 Algorithm Analysis
Lecture 5 Algorithm Analysis
Data Structures & Algorithms
Lecture 5 Algorithm Analysis
Lecture 5 Algorithm Analysis
Presentation transcript:

1 CSE 326: Data Structures Sorting in (kind of) linear time Zasha Weinberg in lieu of Steve Wolfman Winter Quarter 2000

2 BinSort (a.k.a. BucketSort) If all keys are 1…K Have array of size K Put keys into correct bin (cell) of array

3 BinSort example K=5. list=(5,1,3,4,3,2,1,1,5,4,5) Bins in array key = 11,1,1 key = 22 key = 33,3 key = 44,4 key = 55,5,5 Sorted list: 1,1,1,2,3,3,4,4,5,5,5

4 BinSort Pseudocode procedure BinSort (List L,K) LinkedList bins[1..K] { Each element of array bins is linked list. Could also do a BinSort with array of arrays. } For Each number x in L bins[x].Append(x) End For For i = 1..K For Each number x in bins[i] Print x End For

5 BinSort Running time

6 BinSort Conclusion: K is a constant –BinSort is linear time K is variable –Not simply linear time K is large (e.g ) –Impractical

7 BinSort is “stable” Stable Sorting algorithm. –Items in input with the same key end up in the same order as when they began. –Important if keys have associated values –Critical for RadixSort

8 RadixSort Radix = “The base of a number system” (Webster’s dictionary) History: used in 1890 U.S. census by Hollerith * Idea: BinSort on each digit, bottom up. * Thanks to Richard Ladner for this fact, taken from Winter 1999 CSE 326 course web.

9 RadixSort – magic! It works. Input list: 126, 328, 636, 341, 416, 131, 328 BinSort on lower digit: 341, 131, 126, 636, 416, 328, 328 BinSort result on next-higher digit: 416, 126, 328, 328, 131, 636, 341 BinSort that result on highest digit: 126, 131, 328, 328, 341, 416, 636

10 Not magic. It provably works. Keys –N-digit numbers –base B Claim: after i th BinSort, least significant i digits are sorted. –e.g. B=10, i=3, keys are 1776 and comes before 1776 for last 3 digits.

11 Induction to the rescue!!! base case: –i=0. 0 digits are sorted (that wasn’t hard!)

12 Induction is rescuing us… Induction step –assume for i, prove for i+1. –consider two numbers: X, Y. Say X i is i th digit of X (from the right) X i+1 < Y i+1 then i+1 th BinSort will put them in order X i+1 > Y i+1, same thing X i+1 = Y i+1, order depends on last i digits. Induction hypothesis says already sorted for these digits. (Careful about ensuring that your BinSort preserves order aka “stable”…)

13 Paleontology fact Early humans had to survive without induction.

14 Running time of Radixsort How many passes? How much work per pass? Total time? Conclusion –Not truly linear if K is large. In practice –RadixSort only good for large number of items, relatively small keys –Hard on the cache, vs. MergeSort/QuickSort

15 What data types can you RadixSort? Any type T that can be BinSorted Any type T that can be broken into parts A and B, –You can reconstruct T from A and B –A can be RadixSorted –B can be RadixSorted –A is always more significant than B, in ordering

16 Example: 1-digit numbers can be BinSorted 2 to 5-digit numbers can be BinSorted without using too much memory 6-digit numbers, broken up into A=first 3 digits, B=last 3 digits. –A and B can reconstruct original 6-digits –A and B each RadixSortable as above –A more significant than B

17 RadixSorting Strings 1 Character can be BinSorted Break strings into characters Need to know length of biggest string (or calculate this on the fly).

18 RadixSorting Strings example 5 th pass 4 th pass 3 rd pass 2 nd pass 1 st pass String 1 zippy String 2 zap String 3 ants String 4 flaps NULLs are just like fake characters

19 RadixSorting Strings running time N is number of strings L is length of longest string RadixSort takes O(N*L)

20 RadixSorting IEEE floats/doubles You can RadixSort real numbers, in most representations We do IEEE floats/doubles, which are used in C/C++. Some people say you can’t RadixSort reals. In practice (like IEEE reals) you can.

21 Anatomy of a real number * * Sign (positive or negative) Significand (a.k.a. mantissa) Exponent

22 IEEE floats in binary * * *2 -1 * okay, simplified to focus on the essential ideas. Sign: 1 bit Significand: always 1.fraction. fraction uses 23 bits Biased exponent: 8 bits. –Bias: represent –127 to +127 by adding 127 (so range is 0-254)

23 Observations significand always starts with 1  only one way to represent any number Exponent always more significant than significand Sign is most significant, but in a weird way

24 Pseudocode procedure RadixSortReals (Array[1..N]) RadixSort Significands in Array as unsigned ints RadixSort biased exponents in Array as u-ints Sweep thru Array, put negative #’s separate from positive #’s. Flip order of negative #’s, & put them before the positive #’s. Done.