Review of Lecture 9 Rules for Argument Association
Download
Report
Transcript Review of Lecture 9 Rules for Argument Association
Lecture 18 Merge Sort
Comp 208 Computers in Engineering
Yi Lin
Fall, 2005
2015/7/21
Comp208 Computers in Engineering
1
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
2015/7/21
Comp208 Computers in Engineering
2
Introduction to Merge Sort
Easiest array to sort (size==1)
5
What if size==2?
Either the array is sorted: 5 8
Or isn’t. Then swap the 2 elements:
8 5
2015/7/21
5 8
Comp208 Computers in Engineering
3
Introduction to Merge Sort
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
2015/7/21
3
5
6
7
1
4
Comp208 Computers in Engineering
8
4
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.
2015/7/21
Comp208 Computers in Engineering
5
Example: How to merge two
sorted arrays
2015/7/21
2
3
1
2
1
4
5
6
7
8
9
10
Comp208 Computers in Engineering
6
Example: How to merge sort
2015/7/21
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
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
Comp208 Computers in Engineering
7
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++];
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++];
}
}
2015/7/21
}
Comp208 Computers in Engineering
8
Example: How to merge sort
2015/7/21
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
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
Comp208 Computers in Engineering
9
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;
}
2015/7/21
Comp208 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).
2015/7/21
Comp208 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.
2015/7/21
Comp208 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.
2015/7/21
Comp208 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.
2015/7/21
Comp208 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
2015/7/21
Comp208 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.
2015/7/21
Comp208 Computers in Engineering
16