Programming Assignment 8 CS113 Spring 2002. Programming Assignment 8 Implement the sorting routine of your choice. Compare average performance of your.

Slides:



Advertisements
Similar presentations
The debugger Some programs may compile correctly, yet not produce the desirable results. These programs are valid and correct C programs, yet not the programs.
Advertisements

Searching for Data Relationship between searching and sorting Simple linear searching Linear searching of sorted data Searching for string or numeric data.
Analysis of Algorithms CS 477/677 Instructor: Monica Nicolescu Lecture 6.
§7 Quicksort -- the fastest known sorting algorithm in practice 1. The Algorithm void Quicksort ( ElementType A[ ], int N ) { if ( N < 2 ) return; pivot.
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.
Quick Sort, Shell Sort, Counting Sort, Radix Sort AND Bucket Sort
Chapter 8, Sorting. Sorting, Ordering, or Sequencing “Since only two of our tape drives were in working order, I was ordered to order more tape units.
Event-drive SimulationCS-2303, C-Term Project #3 – Event-driven Simulation CS-2303 System Programming Concepts (Slides include materials from The.
Data Structures and Algorithms PLSD210 Sorting. Card players all know how to sort … First card is already sorted With all the rest, ¶Scan back from the.
Recursive Sorting Why is recursive sorting faster ? Merge Sort Quick Sort Description Quick Sort Pseudocode Choice of Quick Sort pivot record The Quick.
An Introduction to Programming with C++ Fifth Edition
CS 261 – Data Structures Hash Tables Part III: Hash like sorting algorithms.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
Searching Arrays Linear search Binary search small arrays
1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible.
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.
CS261 Data Structures Hash-like Sorting. Hash Tables: Sorting Can create very fast sort programs using hash tables These sorts are not ‘general purpose’
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
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 Project 5: Median. 2 The median of a collection of numbers is the member for which there are an equal number less than or equal and greater than or.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 8: Searching and Sorting Arrays.
Copyright © 2012 Pearson Education, Inc. Chapter 8: Searching and Sorting Arrays.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 7: One More Loop Problem, Generating “random” values, Midterm Review.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
Lecture 4. RAM Model, Space and Time Complexity
ICS 145B -- L. Bic1 Project: Main Memory Management Textbook: pages ICS 145B L. Bic.
Chapter 8 Searching and Sorting Arrays Csc 125 Introduction to C++ Fall 2005.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 8: Searching and Sorting Arrays.
Searching. Linear (Sequential) Search Search an array or list by checking items one at a time. Linear search is usually very simple to implement, and.
Pointers to Functions In C programming language. Introduction  While many programming languages support the concept of pointers to data, only a few enable.
Sorting. 1. Sorting The most basic technique in programming So many solutions for it O( n 2 ), O( n lg n ), O( n ) depending on simplicity of mind complexity.
Today’s Lecture Predefined Functions. Introduction to Functions  Reuse Issue  Building Blocks of Programs  Two types of functions  Predefined  Programmer.
 2008 Pearson Education, Inc. All rights reserved Case Study: Random Number Generation C++ Standard Library function rand – Introduces the element.
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
CS221 Random Numbers. Random numbers are often very important in programming Suppose you are writing a program to play the game of roulette The numbers.
1 Data Structures CSCI 132, Spring 2014 Lecture 34 Analyzing Hash Tables.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Searching When we maintain a collection of data,
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
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.
Sorting and Searching. Searching  Problem definition: Given a value X, return the index of X in the array if such X exist. Otherwise, return NOT_FOUND(-1).
+ Arrays & Random number generator. + Introduction In addition to arrays and structures, C supports creation and manipulation of the following data structures:
1D Arrays and Random Numbers Artem A. Lenskiy, PhD May 26, 2014.
ONE DIMENSIONAL ARRAYS AND RANDOM NUMBERS. Introduction In addition to arrays and structures, C supports creation and manipulation of the following data.
Advanced Pointer Topics. Pointers to Pointers u A pointer variable is a variable that takes some memory address as its value. Therefore, you can have.
CS 261 – Data Structures Hash Tables Hash-like Sorting.
1 Data Structures CSCI 132, Spring 2014 Lecture 33 Hash Tables.
CSE202: Lecture 13The Ohio State University1 Function Scope.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
1 Generating Random Numbers Textbook ch.6, pg
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
L131 Assignment Operators Topics Increment and Decrement Operators Assignment Operators Debugging Tips rand( ) math library functions Reading Sections.
Searching Arrays Linear search Binary search small arrays
Generic Programming in C
Why they aren't really random
CMPT 201 Functions.
Data Structures and Algorithms
Describing algorithms in pseudo code
Data Structures and Algorithms
Searching and Sorting Arrays
Miscellaneous functions
TUTORIAL 11 CS 137 F18 November 27th.
CS150 Introduction to Computer Science 1
CSE 373 Data Structures and Algorithms
Module 14 Miscellaneous Topics
Presentation transcript:

Programming Assignment 8 CS113 Spring 2002

Programming Assignment 8 Implement the sorting routine of your choice. Compare average performance of your sorting routine with that of the system function qsort() for large, randomized datasets of integers.

Programming Assignment 8 Implement the sorting routine of your choice. Compare performance of your sorting routine with that of the system function qsort() for large, randomized datasets of integers.

int rand(void) Function is part of the C++ standard library Be sure to #include Returns a pseudo random integer, simulating a uniform random distribution The number returned tends to look random Function really generates a repeating pattern of pseudo random numbers (period = 2 32 )

void srand(unsigned int seed) Function srand() uses the argument seed as a seed for a new sequence of pseudo random numbers to be returned by subsequent calls to the rand(). If the function srand() is then called with the same seed value, the sequence of pseudo-random numbers will be repeated. If rand() is called before calling srand(), then the same sequence will be generated as when srand() is first called with a seed value of 1.

Pseudo Random Numbers Problem: if program does not call random seed function, then sequence of numbers will be the same every time. This may be helpful (for debugging a simulation, in which a particular sequence of pseudo random numbers causes a bug). This may be unhelpful (when we want to test a program using many different sequences of pseudo random numbers, e.g., sorting).

Create an array of random values #include int *random_array(size_t n) { size_t i; int *data = new int[n]; // seed the random number generator with current time srand(time(0)); for(i=0;i<n;++i) data[i] = rand(); // get a random integer from uniform dist. return(data); // return result }

Programming Assignment 8 Implement the sorting routine of your choice. Compare performance of your sorting routine with that of the system function qsort() for large, randomized datasets of integers.

qsort() The qsort() function is an implementation of the quicksort algorithm provided in stdlib. It sorts a table of data in place. The contents of the table are sorted in ascending order according to a user-supplied comparison function. Be sure to #include

void qsort(void *base, size_t nel, size_t width, int (*compar) (const void *, const void *)); *base is a pointer to the first entry in array to be sorted. nel is the number of elements in the array to be sorted. width is the size (in bytes) of each entry in the array. *compar() is the name of the comparison function which is called with two arguments that point to the elements being compared. NOTE: to learn more type “man qsort” on csa.bu.edu

int (*compar) (const void *, const void *)) Comparison function that compares two elements in the array. Comparison function returns an integer: –less than zero if first argument < second –equal to if arguments are equal –greater than zero if the first argument is > second You write your own comparison function, and pass it as an argument to qsort().

// compare two integers for system qsort routine // return 0 if equal, -1 if *pa < *pb, otherwise 1 int compar(const void *pa, const void *pb) { int *a,*b; a = (static_cast (pa)); b = (static_cast (pb)); if(*a < *b) return -1; else if (*a == *b) return 0; else return 1; }

Comparing Performance To characterize performance, your program must generate large data sets of randomly chosen numbers and run qsort and your sort routine on the same data sets. Choose at least five different sizes (such as n=1000, 2000, 3000, 4000, 5000). Measure the average number of comparisons made for each size of n. Take the average performance over 100 trials at each size n.

Basic Experiment For data set size N = 1000, 2000, … 5,000 For m trials (e.g., 100) Generate new random data set of size N Copy data set to second array Clear comparison counter Sort first data array using algorithm qsort Record comparison count, clear comparison count Sort second data array using your algorithm Record comparison count Free random data arrays Compute average comparison count for each algorithm at data set size N Repeat until done all sizes N. Create a table or plot result on a graph.

Average # comparisons in experiments trials sorting an array of N integers selected from a uniform random distribution (average of 100 trials) Dataset size, N comparisons

Counting Comparisons Used in Sorting static size_t compar_count; // function that returns current value of comparison count size_t get_compar_count() { return compar_count; } // reset comparison count void reset_compar_count() { compar_count = 0; }

// compare two integers for system qsort routine // return 0 if equal, -1 if *pa < *pb, otherwise 1 int compar(const void *pa, const void *pb) { int *a,*b; a = (static_cast (pa)); b = (static_cast (pb)); // increment comparison counter ++compar_count; if(*a < *b) return -1; else if (*a == *b) return 0; else return 1; }

Copy an Array int *copy_array(int *data, size_t n) { size_t i; int *c_data = new int[n]; for(i=0;i<n;++i) c_data[i] = data[i]; return c_data; }

Correctness Testing To test the correctness of your sorting routine, you must choose a small (n about 20) set of randomly chosen data and then sort the data using each routine on that set. To generate an array of random integers, use the library function rand(). Another method for correctness testing is to write/call a function that checks to see if an array is sorted.

Check to see if an array of integers is sorted // check to see that the array is sorted in increasing order bool sorted(int *data, size_t n) { for(size_t i=1;i<n;++i) if(data[i] < data[i-1]) return false; // order is violated!! return true; }