CS 221 – May 23 Sorting in parallel (section 3.7) Special algorithm: parallel version of bubble sort. Lab: – Please implement a serial version of this.

Slides:



Advertisements
Similar presentations
Bubble Sort Algorithm 1.Initialize the size of the list to be sorted to be the actual size of the list. 2.Loop through the list until no element needs.
Advertisements

Chapter Matrices Matrix Arithmetic
Lecture 19: Parallel Algorithms
Decision Maths 1 Sorting Algorithms Bubble Sort A V Ali : 1.Start at the beginning of the data set. 2.Compare the first two elements,
Chapter 9: Advanced Array Manipulation
Bubble Sort Algorithm It is so named because numbers (or letters) which are in the wrong place “bubble-up” to their correct positions (like fizzy lemonade)
Practice Quiz Question
Sorting I Chapter 8 Kruse and Ryba. Introduction Common problem: sort a list of values, starting from lowest to highest. –List of exam scores –Words of.
Sorting A fundamental operation in computer science (many programs need to sort as an intermediate step). Many sorting algorithms have been developed Choose.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 9A Sorting (Concepts)
 Sort: arrange values into an order  Alphabetical  Ascending numeric  Descending numeric  Does come before or after “%”?  Two algorithms considered.
CS 106 Introduction to Computer Science I 02 / 29 / 2008 Instructor: Michael Eckmann.
Visual C++ Programming: Concepts and Projects
CS 171: Introduction to Computer Science II Simple Sorting Ymir Vigfusson.
CS Data Structures I Chapter 10 Algorithm Efficiency & Sorting III.
Parallel Sorting Algorithms Comparison Sorts if (A>B) { temp=A; A=B; B=temp; } Potential Speed-up –Optimal Comparison Sort: O(N lg N) –Optimal Parallel.
1 Percentiles. 2 Seems like on a standardized test we don’t care much about the test score as much as the percentile. (OK not always – ACT, for example.)
Chapter 11 Sorting and Searching. Topics Searching –Linear –Binary Sorting –Selection Sort –Bubble Sort.
1 Lecture 25: Parallel Algorithms II Topics: matrix, graph, and sort algorithms Tuesday presentations:  Each group: 10 minutes  Describe the problem,
Merge sort, Insertion sort
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
CS 206 Introduction to Computer Science II 03 / 16 / 2009 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
Computer Programming Sorting and Sorting Algorithms 1.
What is the best way to start? 1.Plug in n = 1. 2.Factor 6n 2 + 5n Let n be an integer. 4.Let n be an odd integer. 5.Let 6n 2 + 5n + 4 be an odd.
1 Parallel Algorithms III Topics: graph and sort algorithms.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
Introduction to Programming with C++ Fourth Edition
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Sorting and Searching Arrays CSC 1401: Introduction to Programming with Java Week 12 – Lectures 1 & 2 Wanda M. Kunkle.
Value Iteration 0: step 0. Insertion Sort Array index67 Iteration i. Repeatedly swap element i with.
CSC220 Data Structure Winter
9/17/20151 Chapter 12 - Heaps. 9/17/20152 Introduction ► Heaps are largely about priority queues. ► They are an alternative data structure to implementing.
COMP102 Lab 131 COMP 102 Programming Fundamentals I Presented by : Timture Choi.
1 Lab Session-XI CSIT121 Spring 2002 w Sorting the arrays (array application) w Bubble Sort w Structures and Their Usage w Passing Struct Variables to.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Recursion, Complexity, and Sorting By Andrew Zeng.
Chapter 10 B Algorithm Efficiency and Sorting. © 2004 Pearson Addison-Wesley. All rights reserved 9 A-2 Sorting Algorithms and Their Efficiency Sorting.
Searching and Sorting Topics Linear and Binary Searches Selection Sort Bubble Sort.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Chapter 6: Arrays: Lists and Tables
© 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.
© 2006 Pearson Addison-Wesley. All rights reserved10 B-1 Chapter 10 (continued) Algorithm Efficiency and Sorting.
Foundation of Computing Systems
CS 361 – Chapters 8-9 Sorting algorithms –Selection, insertion, bubble, “swap” –Merge, quick, stooge –Counting, bucket, radix How to select the n-th largest/smallest.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
An Introduction to Programming with C++ Fifth Edition Chapter 11 Arrays.
Data Structure CS 322. What is an array? Initializing arrays Accessing the values of an array Multidimensional arrays LAB#1 : Arrays.
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.
Bubble Sort.
Lecture #9: Sorting Algorithms خوارزميات الترتيب Dr. Hmood Al-Dossari King Saud University Department of Computer Science 22 April 2012.
UNIT 5.  The related activities of sorting, searching and merging are central to many computer applications.  Sorting and merging provide us with a.
1 Searching and Sorting Searching algorithms with simple arrays Sorting algorithms with simple arrays –Selection Sort –Insertion Sort –Bubble Sort –Quick.
Section 3.5 Revised ©2012 |
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Sorting means to arrange the data in a certain order. What does sorting mean?
Bubble Sort. Sorting  Computers only use numeric values for sorting  Does this mean you cannot sort a file by a character field (such as last name or.
PERFECT SQUARES What does that mean?. SQUARE  You could make a list of “perfect squares” by thinking of numbers of objects where these objects could.
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Bubble Sort Bubble sort is one way to sort an array of numbers. Adjacent values are swapped until the array is completely sorted. This algorithm gets its.
Algorithm Efficiency and Sorting
Selection Sort Sorted Unsorted Swap
Sorting "There's nothing in your head the sorting hat can't see. So try me on and I will tell you where you ought to be." -The Sorting Hat, Harry Potter.
Searching and Sorting Arrays
CS 101 – Oct. 21 Sorting Much-studied problem in CS – many ways to do it Given a list of data, need to arrange it “in order” Some methods do better based.
Parallel sorting.
Matrix Operations Chapter 4, Sections 1, 2, 3.
Presentation transcript:

CS 221 – May 23 Sorting in parallel (section 3.7) Special algorithm: parallel version of bubble sort. Lab: – Please implement a serial version of this algorithm. – Save the parallelizing of it until tomorrow

Sort Arrange elements of an array in order Let’s assume we have an array of integers, to sort in ascending order At some point, sorting requires some elements to be swapped – It would be nice if these elements are “near” each other. Why? – Bubble sort sounds like a good starting point. – But pure bubble sort only looks at adjacent elements. Need a way to look “a little” farther away

Parallel sorting We’ll play with a special version of bubble sort that is specially designed to be parallelizable. We’ll treat the 1-D array as if it’s 2-D – Ability to compare to neighbor on right and below It’s most convenient for the number of entries to be an even perfect square. – Eventually, we’d want each process to get an even # of rows. – Thus, we’ll assume the size of the array is of the form (2pk) 2, where k = 1,2,3,… (even perfect square) – For parallelizing, p = # processes. So, if p = 8, it would be good to try 16 2, 32 2, 48 2, etc.

Overview Handout example assumes 16 entries, so we can arrange as 2-D array (4x4). We’ll perform several passes over the array. – Odd number pass: Sort the rows. Even rows ascending; odd rows descending – Even number pass: Sort the columns. All columns sorted in descending order. – After each pass, see if array completely sorted. If so, quit. – You only need about n passes, where n = # rows.

Example Sort by rows: 3. Sort by rows: 2. Sort by columns: 4. Sort by columns: 5. Just one more pass and we’re done! Where is our answer?

Handing each row & col The “inner” part of the algorithm looks at individual rows and columns Odd number pass: look at rows only – Compare elements [0] with [1]; [2] with [3]; [4] with [5]… – Compare elements [1] with [2]; [3] with [4] – This corresponds to handout: people looking to their right – Careful: alternating sense Even number pass: look at columns only – As with rows, look at [0] and [1], [2] with [3] etc. – And then look at [1] with [2], [3] with [4], etc.

Lab Functions you may need besides main: – Randomize – to initialize array to random data – Sort – overview of algorithm – Swap two integers – Print_array_2d – to see progress as program runs – Is_already_sorted – needed after every pass of algorithm The even numbered rows must be ascending, left to right Odd rows must be descending from left to right Also, lowest # on each row must be >= highest number on next row – Snake – to convert 2-d back into 1-d answer.