Lecture 19 Merge Sort Comp 208 Computers in







![Example Merge two sorted arrays void merge(int a[], int size. A, int b[], int Example Merge two sorted arrays void merge(int a[], int size. A, int b[], int](https://slidetodoc.com/presentation_image_h2/312aff61127ce152629bd04057c96fcc/image-8.jpg)

![Example Merge Sort void merge. Sort (int a[], int size){ int left. Size, right. Example Merge Sort void merge. Sort (int a[], int size){ int left. Size, right.](https://slidetodoc.com/presentation_image_h2/312aff61127ce152629bd04057c96fcc/image-10.jpg)






- Slides: 16
Lecture 19 Merge Sort Comp 208 Computers in Engineering Yi Lin Winter, 2007 12/27/2021 Comp 208 Computers in Engineering 1
Lecture 18 Merge Sort Objectives l Learn about merge sort (O(nlogn)) l l It is better than Selection, Bubble, Insertion (O(n 2)) A new C syntax l l Dynamic array (malloc) Learn enough in-depth C to implement these 12/27/2021 Comp 208 Computers in Engineering 2
Introduction to Merge Sort l Easiest array to sort (size==1) 5 l What if size==2? l l Consider it is composed of two arrays with one element each. Merge them into one array 5 5 8 8 12/27/2021 Comp 208 Computers in Engineering 3
Introduction to Merge Sort l What if size > 2? 1. 2. 3. Split them into two equal size sub-arrays Sort these two sub-arrays 1. This process can be done recursively. Then merge them 3 6 5 7 2 1 10 8 9 4 9 10 …………… 2 12/27/2021 3 5 6 7 1 4 Comp 208 Computers in Engineering 8 4
How to merge two sorted arrays? l The smallest item from the final list is l l either the first item of the first list, or the first item of the second list (Assume it is the case. ) l Then the second biggest item of the final list has to be § § l 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. 12/27/2021 Comp 208 Computers in Engineering 5
Example: How to merge two sorted arrays 12/27/2021 2 3 1 2 1 4 5 6 7 8 9 10 Comp 208 Computers in Engineering 6
Example: How to merge sort 12/27/2021 3 1 6 2 5 3 7 4 2 5 1 6 10 8 7 9 4 10 3 2 6 3 5 7 6 2 7 1 10 8 4 9 4 10 3 6 5 2 7 1 10 4 8 9 8 4 9 3 6 5 7 2 1 10 8 9 4 5 7 8 9 Comp 208 Computers in Engineering 7
Example Merge two sorted arrays void merge(int a[], int size. A, int b[], int size. B, int c[]){ int c_i, a_i, b_i; for(c_i=0, a_i=0, b_i=0; c_i<size. A+size. B; c_i++){ if(a_i >= size. A) { /* 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 >= size. B) { c[c_i] = a[a_i++]; continue; } 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[]. */ c[c_i] = a[a_i++]; } else{ c[c_i] = b[b_i++]; } } 12/27/2021 } Comp 208 Computers in Engineering 8
Example: How to merge sort 12/27/2021 3 1 6 2 5 3 7 4 2 5 1 6 10 8 7 9 4 10 3 2 6 3 5 7 6 2 7 1 10 8 4 9 4 10 3 6 5 2 7 1 10 4 8 9 8 4 9 3 6 5 7 2 1 10 8 9 4 5 7 8 9 Comp 208 Computers in Engineering 9
Example Merge Sort void merge. Sort (int a[], int size){ int left. Size, right. Size; int* output; if(size==1) return; left. Size = size/2; // Split into two subarrays right. Size = size-left. Size; merge. Sort(&a[0], left. Size); // recursively sort the left array merge. Sort(&a[left. Size], right. Size); // 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], left. Size, &a[left. Size], right. Size, output); memcpy(a, output, size*sizeof(int)); // copy memory spaces from array output to array a. free(output); return; } 12/27/2021 Comp 208 Computers in Engineering 10
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). 12/27/2021 Comp 208 Computers in Engineering 11
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. 12/27/2021 Comp 208 Computers in Engineering 12
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. 12/27/2021 Comp 208 Computers in Engineering 13
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. 12/27/2021 Comp 208 Computers in Engineering 14
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 12/27/2021 Comp 208 Computers in Engineering 15
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. 12/27/2021 Comp 208 Computers in Engineering 16