Qsort.

Slides:



Advertisements
Similar presentations
1 Lecture13: Other C Topics 12/17/2012. Topics Variable-length argument lists Pointers to functions Command-line arguments Suffixes for integer and floating-point.
Advertisements

Recursive Sorting Why is recursive sorting faster ? Merge Sort Quick Sort Description Quick Sort Pseudocode Choice of Quick Sort pivot record The Quick.
1 Homework / Exam HW7 due class 25 Exam 3 - class 26 –Open Book, Open Notes –Covers up through end of K&R 7 –and Appendix B Standard Library –Plus UNIX.
Pointers Chapters 6+9 in ABC. abp 12 int a = 1, b = 2, *p; & - reference operator (address) * - dereference operator (value) p = &a; // *p is now 1 abp.
Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be.
Functions Definition: Instruction block called by name Good design: Each function should perform one task and do it well Functions are the basic building.
21-2 Understand various methods of sorting. Use the qsort function to sort. Binary search Related Chapter: ABC 8.5.
SEARCHING UNIT II. Divide and Conquer The most well known algorithm design strategy: 1. Divide instance of problem into two or more smaller instances.
Pointers to Functions In C programming language. Introduction  While many programming languages support the concept of pointers to data, only a few enable.
Lecture 13: Arrays, Pointers, Code examples B Burlingame 2 Dec 2015.
Functions. What is a function? It’s a group of statements that has a specific purpose and can be be repeatedly executed as needed. By using functions:
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.
Chapter 7 Pointers Associate Prof. Yuh-Shyan Chen Dept. of Computer Science and Information Engineering National Chung-Cheng University.
SCP1103 Basic C Programming SEM1 2010/2011 Arithmetic Expressions Week 5.
Generic Programming in C
Arithmetic Expressions
UMBC CMSC 104 – Section 01, Fall 2016
Why functions segments of code that repeat several times
2016.
EPSII 59:006 Spring 2004.
C Language By Sra Sontisirikit
CS1010 Programming Methodology
Functions Dr. Sajib Datta
Chapter 7 - Pointers Outline 7.1 Introduction
Pointers.
Student Book An Introduction
Programming Languages and Paradigms
Data Structures and Algorithms
2011/11/20: Lecture 15 CMSC 104, Section 4 Richard Chang
Some examples.
Sorting.
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
FUNCTIONS WITH ARGUMENTS
Merge Sort Merge sort is a recursive algorithm for sorting that decomposes the large problem.
CSC212 Data Structure - Section RS
Data Structures and Algorithms
כתיבת מאקרו הפקודה assert מצביעים לפונקציות
Functions, Part 1 of 3 Topics Using Predefined Functions
Pointers Call-by-Reference CSCI 230
Miscellaneous functions
Functions with arrays.
Structures vol2.
Header files.
Revision.
Pointers.
Relational, Logical, and Equality Operators
Simulating Reference Parameters in C
Module 2-3: Passing pointers
Pointers Pointers are variables that contain memory addresses as their values. A variable name refers to a specific value. A pointer contains an address.
Files.
Functions.
Pointers Pointers point to memory locations
Extra Practice for Recursion
Functions, Part 1 of 3 Topics Using Predefined Functions
EENG212 ALGORITHMS & DATA STRUCTURES
Functions continued.
More on conditional statements
Bubble sort.
EECE.2160 ECE Application Programming
Arrays.
Arrays.
(PART 2) prepared by Senem Kumova Metin modified by İlker Korkmaz
Exercise 5 1. We learned bubble sort during class. This problem requires you to modify the code for bubble sorting method to implement the selection sorting.
Strings #include <stdio.h>
Functions, Part 1 of 3 Topics Using Predefined Functions
2008/11/05: Lecture 15 CMSC 104, Section 0101 John Y. Park
CSCE 206 Lab Structured Programming in C
IPC144 Introduction to Programming Using C Week 5 – Lesson 1
Revision.
Presentation transcript:

Qsort

Complexity Source: http://bigocheatsheet.com/ 2018 Risto Heinsar

qsort Simple to use and (typically) a quick way to sort arrays, including structure and string arrays. Bubble sort was easy to learn, but horribly slow How fast is it? At the worst case, as fast as bubble sort (n2) Typically much faster (n*log2(n)) E.g. sorting 1000 numbers: 10002 = 1 000 000 operations 1000 * log2(1000) = 1000 * 9.97 = 9 970 operations 2018 Risto Heinsar

qsort function Function prototype: void qsort (void *base, size_t num, size_t size, int (*compar)(const void*,const void*)); Parameters: base – pointer to the first element of the array num – how many elements in the array size – size of a single element compar – pointer to a compare function, returns an integer 2018 Risto Heinsar

Compar function int compar (const void *var1, const void *var2); Parameter: const void *var const – shouldn’t be changed void – type is not set, can accommodate any type, must cast to correct type *var – beginning of element Different compare functions for different types! Return value: integer < 0 var1 should be before var2 == 0 var1 is equivalent to var2 > 0 var1 should be after var2 2018 Risto Heinsar

Sample #include <stdio.h> #include <stdlib.h>   int ComparFunc(const void *a, const void *b); int main(void) { int i, nums[] = {40, 10, 100, 90, 20, 25}; int numCount = sizeof(nums) / sizeof(int); qsort(nums, (size_t)numCount, sizeof(int), ComparFunc); for (i = 0; i < numCount; printf ("%3d ", nums[i]), i++); return 0; } int ComparFunc(const void *a, const void *b) if (*(int*)a > *(int*)b) return 1; else if (*(int*)a < *(int*)b) return -1; else return 0; 2018 Risto Heinsar

Sample shorter (integers only) #include <stdio.h> #include <stdlib.h>   int ComparFunc(const void *a, const void *b); int main(void) { int i, nums[] = {40, 10, 100, 90, 20, 25}; int numCount = sizeof(nums) / sizeof(int); qsort(nums, (size_t)numCount, sizeof(int), ComparFunc); for (i = 0; i < numCount; printf ("%3d\n", nums[i]), i++); return 0; } int ComparFunc(const void *a, const void *b) return *(int*)a - *(int*)b; 2018 Risto Heinsar

Lab task Download and open the base code from the web The data you are going to be working with is already initialized The function prototypes are already set. Implement those functions! Three compare functions Three print functions Add your function calls to the switch menu Once completed, show and continue with the test revision task 2018 Risto Heinsar