Programming Assignment 1

advertisement
CSE 2320.003 (Fall 2014)
Lab assignment 1 (due Sunday, Sep 14th, before 12 noon)
(NO LATE SUBMISSION IS ALLOWED)
Goal: Understand the application of binary search
Problem Statement:
Rotation operation can be performed on a given sequence of numbers clockwise or anticlockwise.
Clockwise and anti clockwise rotation operations are commonly known as right rotation and left
rotation, respectively. A k left rotated sequence is a sequence resulted from performing left rotation
operation k times on the original sequence. The following examples describe k left rotation operation:
Original sequence:
1, 4, 9, 8, 12, 200, 66, 67, 80, 100
1-left rotation (on the original sequence):
4, 9, 8, 12, 200, 66, 67, 80, 100, 1
4- left rotation (on the original sequence):
12, 200, 66, 67, 80, 100, 1, 4, 9, 8
When the left rotation is performed k times on a sorted sequence, the resulting sequence is known as k
left rotated sorted sequence. The original sorted sequence can be reconstructed by performing k right
rotation operations.
You are given with a k left rotated sorted sequence of size n. Given a number x, you have to find the
position of x both in the given sequence and in the original sorted sequence. Your output should also
include the minimum and maximum element in the sequence. All these operations should take
logarithmic (O(logn)) time. Note, a linear time implementation will not be accepted and you will not
receive any credit for that.
You can assume that there is no repetition of numbers.
Input/output format:
The first line indicates the value of n. The next line contains n positive integers separated by comma. The
next line indicates the number to be searched (x). Output should contain the position of x in the given
rotated sequence, and in the sorted sequence. If x is not present the sequence, simply output -1. Look at
the sample output file carefully.
Sample input:
10
12, 34, 66, 67, 80, 100, 1, 4, 6, 8
67
Output:
position of 67 in the rotated sequence = 4, position of 67 in the sorted sequence = 8, min = 1, max = 100
Note:


You are required to write the program in C. Your program must read the input files by using Unix
shell redirection (e.g., a.out < in1.dat). Thus it is unnecessary to open and close the input files.
Nor should your program prompt for any input file name. Also all the memory allocations need
to be dynamic.
Submit the assignment via Blackboard. A wrong submission will be considered as no submission.
Copy the email to yourself to make sure that the email is sent correctly. Also, if you know of any
bug, let the TA know with the submission.
Download