ppl&dbtSlip4
Q1:-
CREATE (math:Department {name: 'Mathematics', location: 'Building A'}),
(geo:Department {name: 'Geology', location: 'Building B'}),
(chem:Department {name: 'Chemistry', location: 'Building C'}),
(cs:Department {name: 'Computer Science', location: 'Building D'})
CREATE (calculus:Course {name: 'Calculus', code: 'MATH101', credits: 4}),
(geology101:Course {name: 'Introduction to Geology', code: 'GEO101', credits: 3}),
(organicChemistry:Course {name: 'Organic Chemistry', code: 'CHEM201', credits: 4}),
(dataStructures:Course {name: 'Data Structures', code: 'CS102', credits: 3}),
(databases:Course {name: 'Database Systems', code: 'CS201', credits: 3}),
(chemistry101:Course {name: 'General Chemistry', code: 'CHEM101', credits: 4}),
(chemistryData:Course {name: 'Chemistry Data Science', code: 'CHEM202', credits: 3})
CREATE (chem)-[:CONDUCTS]->(organicChemistry),
(chem)-[:CONDUCTS]->(chemistry101),
(math)-[:CONDUCTS]->(calculus),
(geo)-[:CONDUCTS]->(geology101),
(cs)-[:CONDUCTS]->(dataStructures),
(cs)-[:CONDUCTS]->(databases),
(chem)-[:CONDUCTS]->(chemistryData),
(cs)-[:CONDUCTS]->(chemistryData)
MATCH (chem:Department {name: 'Chemistry'})-[:CONDUCTS]->(c:Course)
RETURN c.name AS Course, c.code AS Code, c.credits AS Credits
MATCH (chem:Department {name: 'Chemistry'})-[:CONDUCTS]->(c:Course)<-[:CONDUCTS]-(cs:Department {name: 'Computer Science'})
RETURN c.name AS Course, c.code AS Code, c.credits AS Credits
Q2:-
object MatrixSorter {
// Function to perform insertion sort on a 1D array
def insertionSort(arr: Array[Int]): Array[Int] = {
for (i <- 1 until arr.length) {
val key = arr(i)
var j = i - 1
// Move elements of arr[0..i-1], that are greater than key,
// to one position ahead of their current position
while (j >= 0 && arr(j) > key) {
arr(j + 1) = arr(j)
j -= 1
}
arr(j + 1) = key
}
arr
}
// Function to sort a 2D matrix
def sortMatrix(matrix: Array[Array[Int]]): Array[Array[Int]] = {
// Flatten the matrix into a 1D array
val flatArray = matrix.flatten
// Sort the flattened array using insertion sort
val sortedArray = insertionSort(flatArray)
// Reshape the sorted array back into a 2D matrix
val numRows = matrix.length
val numCols = if (numRows > 0) matrix(0).length else 0
val sortedMatrix = Array.ofDim[Int](numRows, numCols)
for (i <- 0 until numRows) {
for (j <- 0 until numCols) {
sortedMatrix(i)(j) = sortedArray(i * numCols + j)
}
}
sortedMatrix
}
// Main function
def main(args: Array[String]): Unit = {
// Sample 2D matrix
val matrix = Array(
Array(5, 3, 8),
Array(1, 4, 7),
Array(9, 2, 6)
)
println("Original Matrix:")
matrix.foreach(row => println(row.mkString(" ")))
// Sort the matrix
val sortedMatrix = sortMatrix(matrix)
println("\nSorted Matrix:")
sortedMatrix.foreach(row => println(row.mkString(" ")))
}
}