Lab1 Correction
Lab1 Correction
Khulais
Chapter 1. Arrays
Problem 1:
Example:
1
Faculty of Computing and Information Technology
Khulais
/* utility function to print an array */
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
// Driver program to test above functions
public static void main(String[] args)
{
RotateArray rotate = new RotateArray();
int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
rotate.printArray(arr, 7);
rotate.rotate(arr,2,7);
rotate.printArray(arr, 7);
}
}
Q1.2. How many instructions do the program? O(n)
Q1.3. What is the size of the auxiliary space used by your program? O(d)
2
Faculty of Computing and Information Technology
Khulais
/* utility function to print an array */
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
System.out.print(arr[i] + " ");
}
// Driver program to test above functions
public static void main(String[] args)
{
RotateArray rotate = new RotateArray();
int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
rotate.leftRotate(arr, 2, 7);
rotate.printArray(arr, 7);
}
}
Q2.3. What is the size of the auxiliary space used by your program? O(1)
a) Elements are first moved in first set – (See below diagram for this
movement)
3
Faculty of Computing and Information Technology
Khulais
4
Faculty of Computing and Information Technology
Khulais
// Driver program to test above functions
public static void main(String[] args)
{
RotateArray rotate = new RotateArray();
int arr[] = { 1, 2, 3, 4, 5, 6, 7 };
rotate.leftRotate(arr, 2, 7);
rotate.printArray(arr, 7);
}
}
Q3.1. How many instructions does the program execute? O(n)
Q3.2. What is the size of the auxiliary space used by your program? O(1)