Description
CS6308- Java Programming
Assignment 3
Instructions: • Use camel case for class name and variable name.
• Class name must follow your last 4 digit register number
Topic : Array
Exercise 1 – Sort
Write a program to read n integer in a 1D array and print the sorted array in the following format. Use static methods and find the number of comparisons for the sorting algorithm whose worst-case complexity is O(n^2) and O(n)
Print the array with array index position
Sample output
———————————————–
| 0 | 1| 2| 3| 4 | 5|
—————————
| 14| 20| 40 | 62 | 83| 94|
Hint:
public class SortedArray{
public static void main(String[] args){
System.out.print(“Enter the number of inputs (n): “); int n = scanner.nextInt(); int[] arr = new int[n];
…
int comparisons = bubbleSort(arr);
System.out.println(“Sorted array in ascending order:”);
…
}
public static int bubbleSort(int[] arr) { int n = arr.length; int comparisons = 0;
… return comparisons;
}
}
Exercise 2: Sort random integer/character
Write a program to read n random integer in a 1D array.
a) Apply method to sort the generated array content and return the number of comparisons done.
b) Apply another method to generate character array using the random integer and sort the array.
Hint:
public class RandomSortedArray{
public static void main(String[] args){
System.out.print(“Enter the number of inputs (n): “); int n = scanner.nextInt(); int[] arr = new int[n]; arr=RandomArray(arr);
… int comparisons=OrderNSort(arr);
}
public static int[] RandomArray(int[] arr) {
Random random = new Random(); …//read Random input arr[i] = random.nextInt(26);
return arr;
} public static char[] CharArray(int[] intArray) {
… charArray[i] = (char) (intArray[i] + ‘a’); // Convert integer to character ‘a’ to ‘z’ … }
return charArray;
}
public static int OrderNSort(char[] arr) { …
return comparisons;
}
Exercise 3: Search element Occurrence
Write a program to read n random integer in a 1D array of A and B of size n. Apply method to search the occurrence of element in B and print the number of B element occurrence in A .
Exercise 4: Sum of arrays
Write a program to read two 2D array. Apply method to perform column major sum and sort the array based on the sum of columns.
Hint:
for (int j = 0; j < columns; j++) {
int sum = 0; for (int i = 0; i < rows; i++) { sum += array[i][j];
}
columnSums[j] = sum;
Sample Input:
1 3 4
+ 2 1 3
1 2 4 5 -1 2
3 1 2 3 4 2
3 4 7 => 4 3 7
6 1 6 1 6 6
6 5 4 5 6 4
=>




Reviews
There are no reviews yet.