Tuesday, June 23, 2009

Selection sort of Linked List ( Java)

1.Given a linked list of integer elements, construct a SELECTION SORT algorithm that puts the integer elements into ascending order. Note that a selection sort does O(n) copies. On each pass, the smallest element is placed into its proper position. For example, on the third pass:

Before: 5 10 55 22 65 12 75

After : 5 10 12 22 65 55 75

The code should NOT re-link nodes - just copy elements. Do not create a new list.

Solution in java

class Node{
public int value;
public Node next;
public Node(int givenval){
value = givenval;
}
}
class List{

private int length;
private Node head;
private Node tail;
//adds a new node to the list with specified value
public void addtoList(int value){
Node node = new Node(value);
if(length ==0){
node.next = null;

}
else{
node.next = head;
}
head = node;
length++;
}
//prints contents of the list
public void Print(){
Node node = head;
while(node != null){
System.out.println(node.value);
node = node.next;
}
}
public void selectionSort(){
for(Node node1 = head; node1!=null; node1 = node1.next){//number of
//iterations
Node min = node1;//assumes min node is the node under considerations
//selects the min node
for(Node node2 = node1; node2!=null; node2 = node2.next){
if(min.value > node2.value){
min = node2;
}

}
//swaps the min node with the node in its actual position
Node temp = new Node(node1.value);
node1.value = min.value;
min.value = temp.value;
}
}
}




public class Main {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

List list = new List();
list.addtoList(1);
list.addtoList(2);
list.addtoList(3);
list.addtoList(4);
list.addtoList(5);
System.out.println("Before sorting");
list.Print();
list.selectionSort();
System.out.println("After sorting");
list.Print();

}

}

Thursday, June 11, 2009

Database Quiz


Scroll down for answers

1. Set of permitted values of each attributes are called

A . Row
B. Column
C. Domain
D. Schema

2. Which of the following statements is not true

A. All primary keys are super keys
B. A candidate key can have a proper subset which is a super key
C. All super keys are not candidate keys
D. A candidate key may not be a super key

3. Entity integrity means that

A. A primary key cannot be null.
B. A foreign key cannot be null.
C. A primary key can be partially null.
D. A foreign key can be partially null.

4. The target of a foreign key should be

A. Foreign key in another table
B. Primary key in another table
C. Super key in another table
D. Primary key in the same table

5. Which of the following can not be the mapping cardinality of a foreign key

A. One- one
B. Many - many
C. Many- one

6. SQL is a

A. Host language
B. Application language
C. Data manipulation language

7. In the following ER diagram,


mapping cardinality of the relation is many one, which means
one manager can manage

A. zero or more departments
B. one or more departments
C. more than one department
D. exactly one department

8. Entity, that can not be uniquely identified by its own attributes , is called

A . Composite entity
B. Strong entity
C. Weak entity
D. Simple entity

In schema S = {A,B,C,D,E,F,G,H}, suppose the following hold for S.
AB --> CD
E --> F
F --> G

9. Which of the following holds true with respect to the above schema

A. CD --> AB
B. AB --> ACD
C. A --> CD
D. AB --> C

10. Which of the following does not hold true.

A. E --> G
B. ABE -->CDE
C. AB --> ABH
D. AB --> null set







Answers
  1. A
  2. B
  3. A
  4. B
  5. B
  6. C
  7. A
  8. C
  9. D
  10. C