Presentation is loading. Please wait.

Presentation is loading. Please wait.

Comp 208 Computers in Engineering Yi Lin Fall, 2005

Similar presentations


Presentation on theme: "Comp 208 Computers in Engineering Yi Lin Fall, 2005"— Presentation transcript:

1 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

2 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

3 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

4 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

5 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

6 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

7 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

8 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

9 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

10 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

11 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

12 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

13 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

14 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

15 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

16 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


Download ppt "Comp 208 Computers in Engineering Yi Lin Fall, 2005"

Similar presentations


Ads by Google