Comp 208 Computers in Engineering Yi Lin Fall, 2005

Slides:



Advertisements
Similar presentations
Pointers.
Advertisements

Dynamic memory allocation
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
Programming and Data Structure
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
Lecture 10: Heap Management CS 540 GMU Spring 2009.
CS1061: C Programming Lecture 21: Dynamic Memory Allocation and Variations on struct A. O’Riordan, 2004, 2007 updated.
Spring 2005, Gülcihan Özdemir Dağ Lecture 12, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 12 Outline 12.1Introduction.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Kernighan/Ritchie: Kelley/Pohl:
Memory Allocation. Memory A memory or store is required in a computer to store programs (or information or data). Data used by the variables in a program.
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 12 – Data Structures Outline 12.1Introduction.
1 CSE 303 Lecture 11 Heap memory allocation ( malloc, free ) reading: Programming in C Ch. 11, 17 slides created by Marty Stepp
Pointers Applications
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Lecture No.01 Data Structures Dr. Sohail Aslam
EE4E. C++ Programming Lecture 1 From C to C++. Contents Introduction Introduction Variables Variables Pointers and references Pointers and references.
17. ADVANCED USES OF POINTERS. Dynamic Storage Allocation Many programs require dynamic storage allocation: the ability to allocate storage as needed.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 12: Pointers continued, C strings.
Stack and Heap Memory Stack resident variables include:
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Pointers OVERVIEW.
ECE 103 Engineering Programming Chapter 47 Dynamic Memory Alocation Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
EFFICIENCY & SORTING II CITS Scope of this lecture Quicksort and mergesort Performance comparison.
+ Dynamic memory allocation. + Introduction We often face situations in programming where the data is dynamics in nature. Consider a list of customers.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
6-1 Embedded Systems C Programming Language Review and Dissection IV Lecture 6.
Pointers1 WHAT IS A POINTER? Simply stated, a pointer is an address. A running program consists of three parts: execution stack, code, and data. They are.
Merge Sort. In plain English: if the size of the array > 1, split the array into two halves, and recursively sort both halves; when the sorts return,
1 Dynamic Memory Allocation. 2 In everything we have done so far, our variables have been declared at compile time. In these slides, we will see how to.
MORE POINTERS Plus: Memory Allocation Heap versus Stack.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
DYNAMIC MEMORY ALLOCATION. Disadvantages of ARRAYS MEMORY ALLOCATION OF ARRAY IS STATIC: Less resource utilization. For example: If the maximum elements.
CSE 220 – C Programming malloc, calloc, realloc.
Dynamic Allocation in C
Prof. U V THETE Dept. of Computer Science YMA
Pointers and Dynamic Arrays
Chapter 12 – Data Structures
5.13 Recursion Recursive functions Functions that call themselves
Top 50 Data Structures Interview Questions
COP 3503 FALL 2012 Shayan Javed Lecture 16
Computer Science 210 Computer Organization
Linked Lists Chapter 6 Section 6.4 – 6.6
Lesson One – Creating a thread
Dynamic Memory Allocation
C Programming Language Review and Dissection IV
CSCI206 - Computer Organization & Programming
Dynamic Memory Allocation
Dynamic Memory Allocation
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.
Unit-2 Divide and Conquer
Computer Science 210 Computer Organization
CSC215 Lecture Memory Management.
Dynamic Memory Allocation
Memory Allocation CS 217.
Review & Lab assignments
24 Searching and Sorting.
EENG212 – Algorithms & Data Structures Fall 07/08 – Lecture Notes # 5b
Dynamic Memory.
Data Structures & Algorithms
Chapter 10-1: Dynamic Memory Allocation
Application: Efficiency of Algorithms II
Application: Efficiency of Algorithms II
Dynamic Memory – A Review
CMPT 225 Lecture 10 – Merge Sort.
Presentation transcript:

Comp 208 Computers in Engineering Yi Lin Fall, 2005 Lecture 18 Merge Sort Comp 208 Computers in Engineering Yi Lin Fall, 2005 2019/5/27 Comp208 Computers in Engineering

Comp208 Computers in Engineering Lecture 18 Merge Sort Objectives Learn about merge sort (O(nlogn)) It is better than Selection, Bubble, Insertion (O(n)) A new C syntax Dynamic array (malloc) Learn enough in-depth C to implement these 2019/5/27 Comp208 Computers in Engineering

Introduction to Merge Sort Easiest array to sort (size==1) What if size==2? Either the array is sorted: Or isn’t. Then swap the 2 elements: 5 5 8 8 5 5 8 2019/5/27 Comp208 Computers in Engineering

Introduction to Merge Sort What if size > 2? Split them into two equal size sub-arrays Sort these two sub-arrays This process can be done recursively. Then merge them 3 6 5 7 2 1 10 8 9 4 ……………………… 2 3 5 6 7 1 4 8 9 10 2019/5/27 Comp208 Computers in Engineering

How to merge two sorted arrays? The smallest item from the final list is either the first item of the first list, or the first item of the second list (Assume it is the case.) Then the second biggest item of the final list has to be the first item of the first list, or the second item of the second list. …… This is applied on and on until both lists are empty, therefore until both lists have been merged into a single ordered list. 2019/5/27 Comp208 Computers in Engineering

Example: How to merge two sorted arrays 2 2 3 5 6 7 1 2 1 1 4 8 9 10 2019/5/27 Comp208 Computers in Engineering

Example: How to merge sort 1 2 3 4 5 6 7 8 9 10 3 6 5 7 2 1 10 8 9 4 7 6 5 3 2 10 9 8 4 1 2 7 5 6 3 4 9 8 10 1 6 3 10 1 2 7 5 4 9 8 6 3 10 1 7 5 2 9 8 4 3 6 7 5 2 1 10 9 8 4 3 6 7 5 2 1 10 9 8 4 5 7 8 9 5 7 8 9 2019/5/27 Comp208 Computers in Engineering

Example Merge two sorted arrays void merge(int a[], int sizeA, int b[], int sizeB, int c[]){ int c_i, a_i, b_i; for(c_i=0, a_i=0, b_i=0; c_i<sizeA+sizeB; c_i++){ if(a_i >= sizeA) { /* All elements in a[] have been inserted into c[]. At this time, there are still some elements in c[] which are not filled. They must be the rest of elements in b[]. */ c[c_i] = b[b_i++]; continue; } if(b_i >= sizeB) { c[c_i] = a[a_i++]; if(a[a_i] < b[b_i]){ /* If the first element in a[] is smaller than that in b[], copy the * element in a[] to c[]. */ else{ 2019/5/27 Comp208 Computers in Engineering

Example: How to merge sort 1 2 3 4 5 6 7 8 9 10 3 6 5 7 2 1 10 8 9 4 2 3 5 6 7 3 6 5 7 2 1 4 8 9 10 1 10 8 9 4 3 6 3 6 2 5 7 5 7 2 1 10 1 10 4 8 9 8 9 4 3 3 6 6 5 7 5 7 2 2 1 1 10 10 8 9 8 9 4 4 5 5 7 7 8 8 9 9 2019/5/27 Comp208 Computers in Engineering

Comp208 Computers in Engineering Example Merge Sort void mergeSort (int a[], int size){ int leftSize, rightSize; int* output; if(size==1) return; leftSize = size/2; // Split into two subarrays rightSize = size-leftSize; mergeSort(&a[0], leftSize); // recursively sort the left array mergeSort(&a[leftSize], rightSize); // recursively sort the right array // allocate memory spaces to output array. It has size of elements. Each element is an int. output = (int*)malloc(size*sizeof(int)); merge(&a[0], leftSize, &a[leftSize], rightSize, output); memcpy(a, output, size*sizeof(int)); // copy memory spaces from array output to array a. free(output); return; } 2019/5/27 Comp208 Computers in Engineering

Dynamic Memory Allocation When a variable declaration is executed the C compiler allocates memory for an object of the specified type A C program also has access to a large chunk of memory called the free store Dynamic memory allocation enables us to allocate blocks of memory of any size within the program, not just when declaring variables This memory can be released when we no longer need it. These are useful for creating dynamic arrays and dynamic data structures such as linked lists (which we do not cover in this course). 2019/5/27 Comp208 Computers in Engineering

Comp208 Computers in Engineering malloc The malloc function removes a specified number of contiguous memory bytes from the free store and returns a pointer to this block It is defined by:    void *malloc(number_of_bytes) It returns a pointer of type void * that is the start in memory of the reserved portion of number_of_bytes. If memory cannot be allocated a NULL pointer is returned. This pointer can be converted to any type. The argument type is defined in stdlib.h and is an unsigned integer. 2019/5/27 Comp208 Computers in Engineering

Comp208 Computers in Engineering sizeof We often to not know how many bytes to reserve but we do know how many values we want space for. The sizeof function can be used to find the number of bytes used by a specific type of value. To reserve a block of memory capable of holding 100 integers, we can write: int *ip; ip = (int *) malloc(100*sizeof(int)); Sizeof() can be used to find the size of any data type, variable or structure Even if you know the actual size you want, programs are more portable if you use sizeof() The duality between pointers and arrays allows us to treat the reserved memory like an array. 2019/5/27 Comp208 Computers in Engineering

Comp208 Computers in Engineering Deallocating Memory Suppose a large program calls mergesort many times in the course of a computation involving large arrays. Each time a new block of memory is allocated but after the call, it is no longer accessible. That memory is called garbage. Programs that create garbage are said to have a memory leak. This can lead to severe deterioration in performance and eventually to program failure Some languages (such as Java) automatically check for blocks of memory that cannot be accessed and return them to the free store. This is called automatic Garbage Collection. 2019/5/27 Comp208 Computers in Engineering

Comp208 Computers in Engineering free() C does not do garbage collection It is up to the programmer to return memory to the free store when it is no longer needed. The function free()takes a pointer to an object as its value and frees the memory that pointer refers to DANGER: Make sure the pointer is not NULL or you can cause a spectacular crash 2019/5/27 Comp208 Computers in Engineering

What’s so great about mergesort? Insertion sort, Selection sort, Bubble sort all take time O(n^2) to sort n values. If we look at the call tree for mergesort, we can see that it takes O(n log n) time. For large data sets that is a tremendous improvement Mergesort is one of a group of very efficient sorting algorithms that are used in most applications. 2019/5/27 Comp208 Computers in Engineering