Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Standard view
Full view
of .
0 of .
Results for:
P. 1
Ex 07

Ex 07

Ratings: (0)|Views: 118|Likes:

Availability:

See more
See less

06/06/2011

pdf

text

original

Assignment Number Assignment Number Assignment Number Assignment Number 7777
Given: 24/06/08Due: 08/07/08
Before doing the exercise, please read very carefully the submission guidelines. If youhave any questions about the exercise, please refer to theFAQpage first. If your questionis not answered there send an email.If the user enters invalid input your program should print "
error
" and main should returnthe value -1.
Ex 7.1 – Merge Sort
Write a program that recursively sorts an array of numbers. It first sorts the left half of thearray, then the right half of the array and then merges to the two into a single sorted array.The program:

Reads an integer number, which tells how many numbers are in the array

Sorts the array.

Prints the sorted array.Assumptions:

You will be given the first number.
o

You have to check that it is a valid number.
o

If it is valid then it gives the correct number of elements in the array.

There are going to be up to 100 elements in the array.
o

If there are more than 100 characters then the input is invalid.You must write the following functions:

voidmergeSort(int*arr,intsize)
o

The function recursively sorts the array.

voidmergeArrays(int*leftArr,intleftSize, int*rightArr,intrightSize, int*destArr )
o

The function merges the left and right arrays into destArr.

voidcopyArrays(int*srcArr,int*destArr,intsize)

o

The function copies the source array into the destination array.Example:
Input:Input:Input:Input:
1010 9 8 7 6 5 4 3 2 1
Output:Output:Output:Output:

1 2 3 4 5 6 7 8 9 10
The file name should be:
mergeSort.c
Ex 7.2 – Binary Search
Write a program that performs a binary search on a sorted array of integers.Given a sorted array of elements and a query element, you compare the query against theelement in the middle of the array (marked in green). If they are the same then there isnothing more to do. Otherwise, if the query is smaller than the element in the middle of the array, then it must appear before it in the array. So you need to search in the left half (marked in white). If it is bigger, then you need to search the right half (marked inyellow).You repeat this until, you find the element, or you can't divide the array no more.The program:

Reads an integer number, which tells how many numbers are in the array

Reads the sorted array of integers.

Reads an integer number, which will be the search query.

Prints a message telling if the number appears in the array or not.