Revision
Revision
LINKED LIST
import java.util.LinkedList;
number.add(0, 5);
number.add(3, 6);
number.add(6, 7);
System.out.println("LinkedList: " + number);
number.remove(4);
System.out.println("Updated LinkedList: " + number);
}
}
1. Draw all nodes related to the linked list
2. Draw the insertion of a node b (containing 16) at the behind node 2 in the linked list position
3. Deletion of a node contain 6 in a linked list at a given position
4. Explain the steps taken for each delete , add node
5. Show the output
2. STACK
3. QUEUE
1. draw queue
2. add queue
3. remove queue
4. peek queue
4. SORTING-
b) For each of the following sorting algorithms, show the steps taken to sort the list in an
ascending order.
[15 Marks]
a) Bubble Sort
b) Quick Sort
c) Selection Sort
i. Selection Sort - 5m
Step 1 ( 40, 75, 23, 76, 44, 21, 56, 90, 34, 5) – Find the smallest number
5, 40, 75, 23, 76, 44, 21, 56, 90, 34 => 5 sorted
Step 2 (40, 75, 23, 76, 44, 21, 56, 90, 34) – ignore 5
5, 21, 40, 75, 23, 76, 44, 56, 90, 34 => 21 sorted
Step 3(40, 75, 23, 76, 44, 56, 90, 34) – ignore 5 and 21
5, 21, 23, 40, 75, 76, 44, 56, 90, 34 => 23 sorted
Step 4 (40, 75, 76, 44, 56, 90, 34) – ignore 5, 21, 23
5, 21, 23, 34, 40, 75, 76, 44, 56, 90 => 34 sorted
Step 5 (40, 75, 76, 44, 56, 90) – ignore 5, 21, 23, 34
5, 21, 23, 34, 40, 75, 76, 44, 56, 90 => 40 sorted
Step 6 (75, 76, 44, 56, 90) – ignore 5, 21, 23, 34, 40
5, 21, 23, 34, 40, 44, 75, 76, 56, 90 => 44 sorted
Step 6 ( 75, 76, 56, 90) – ignore 5, 21, 23, 34, 40, 44
5, 21, 23, 34, 40, 44, 56, 75, 76, 90 => 56 sorted
Step 7 (75, 76, 90) - ignore 5, 21, 23, 34, 40, 44, 56
5, 21, 23, 34, 40, 44, 56, 75, 76, 90 => 75 sorted
Step 8 (76, 90) – ignore 5, 21, 23, 34, 40, 44, 56, 75
5, 21, 23, 34, 40, 44, 56, 75, 76, 90 => 76 sorted
Step 9 (90) – ignore 5, 21, 23, 34, 40, 44, 56, 75, 76
5, 21, 23, 34, 40, 44, 56, 75, 76, 90 => 90 sorted
Fasa 2
40, 23, 75, 44, 21, 56, 76, 34, 5, 90 => compare (40, 23)
23, 40, 75, 44, 21, 56, 76, 34, 5, 90
23, 40, 75, 44, 21, 56, 76, 34, 5, 90 => compare (40,75)
23, 40, 75, 44, 21, 56, 76, 34, 5, 90
23, 40, 75, 44, 21, 56, 76, 34, 5, 90 => compare (75, 44)
23, 40, 44, 75, 21, 56, 76, 34, 5, 90
23, 40, 44, 75, 21, 56, 76, 34, 5, 90 => compare (75,21)
23, 40, 44, 21, 75, 56, 76, 34, 5, 90
23, 40, 44, 21, 75, 56, 76, 34, 5, 90 => compare (75, 56)
23, 40, 44, 21, 56, 75, 76, 34, 5, 90
23, 40, 44, 21, 56, 75, 76, 34, 5, 90 => compare (75, 76)
23, 40, 44, 21, 56, 75, 76, 34, 5, 90
23, 40, 44, 21, 56, 75, 76, 34, 5, 90 = compare (76, 34)
23, 40, 44, 21, 56, 75, 34, 76, 5, 90
23, 40, 44, 21, 56, 75, 34, 76, 5, 90 => compare (76, 5)
23, 40, 44, 21, 56, 75, 34, 5, 76, 90 => 76 sorted
Fasa 3
23, 40, 44, 21, 56, 75, 34, 5, 76, 90 => compare (23, 40)
23, 40, 44, 21, 56, 75, 34, 5, 76, 90
23, 40, 44, 21, 56, 75, 34, 5, 76, 90 => compare (40, 44)
23, 40, 44, 21, 56, 75, 34, 5, 76, 90
23, 40, 44, 21, 56, 75, 34, 5, 76, 90 => compare ( 44, 21)
23, 40, 21, 44, 56, 75, 34, 5, 76, 90
23, 40, 21, 44, 56, 75, 34, 5, 76, 90 => compare ( 44, 56)
23, 40, 21, 44, 56, 75, 34, 5, 76, 90
23, 40, 21, 44, 56, 75, 34, 5, 76, 90 => compare (56, 75)
23, 40, 21, 44, 56, 75, 34, 5, 76, 90
23, 40, 21, 44, 56, 75, 34, 5, 76, 90 => compare (75, 34)
23, 40, 21, 44, 56, 34, 75, 5, 76, 90
23, 40, 21, 44, 56, 34, 75, 5, 76, 90 => compare (75, 5)
23, 40, 21, 44, 56, 34, 5, 75, 76, 90 => 75 sorted
Fasa 4
23, 40, 21, 44, 56, 34, 5, 75, 76, 90 => compare ( 23, 40)
23, 40, 21, 44, 56, 34, 5, 75, 76, 90
23, 40, 21, 44, 56, 34, 5, 75, 76, 90 => compare ( 40, 21)
23, 21, 40, 44, 56, 34, 5, 75, 76, 90
23, 21, 40, 44, 56, 34, 5, 75, 76, 90 => compare ( 40, 44)
23, 21, 40, 44, 56, 34, 5, 75, 76, 90
23, 21, 40, 44, 56, 34, 5, 75, 76, 90 => compare ( 44, 56)
23, 21, 40, 44, 56, 34, 5, 75, 76, 90
23, 21, 40, 44, 56, 34, 5, 75, 76, 90 => compare ( 56, 34)
23, 21, 40, 44, 34, 56, 5, 75, 76, 90
23, 21, 40, 44, 34, 56, 5, 75, 76, 90 => compare ( 56, 5)
23, 21, 40, 44, 34, 5, 56, 75, 76, 90 => 56 sorted
Fasa 5
23, 21, 40, 44, 34, 5, 56, 75, 76, 90 => compare ( 23, 21)
21, 23, 40, 44, 34, 5, 56, 75, 76, 90
21, 23, 40, 44, 34, 5, 56, 75, 76, 90 => compare ( 23, 40)
21, 23, 40, 44, 34, 5, 56, 75, 76, 90
21, 23, 40, 44, 34, 5, 56, 75, 76, 90 => compare ( 40, 44 )
21, 23, 40, 44, 34, 5, 56, 75, 76, 90
21, 23, 40, 44, 34, 5, 56, 75, 76, 90 => compare ( 44, 34 )
21, 23, 40, 34, 44, 5, 56, 75, 76, 90
21, 23, 40, 34, 44, 5, 56, 75, 76, 90 => compare ( 44, 5 )
21, 23, 40, 34, 5, 44, 56, 75, 76, 90 => 44 sorted
Fasa 6
21, 23, 40, 34, 5, 44, 56, 75, 76, 90 => compare (21, 23)
21, 23, 40, 34, 5, 44, 56, 75, 76, 90
21, 23, 40, 34, 5, 44, 56, 75, 76, 90 => compare (23, 40)
21, 23, 40, 34, 5, 44, 56, 75, 76, 90
21, 23, 40, 34, 5, 44, 56, 75, 76, 90 => compare (40, 34)
21, 23, 34, 40, 5, 44, 56, 75, 76, 90
21, 23, 34, 40, 5, 44, 56, 75, 76, 90 => compare (40, 5)
21, 23, 34, 5, 40, 44, 56, 75, 76, 90 => 40 sorted
Fasa 7
21, 23, 34, 5, 40, 44, 56, 75, 76, 90 => compare (21, 23)
21, 23, 34, 5, 40, 44, 56, 75, 76, 90
21, 23, 34, 5, 40, 44, 56, 75, 76, 90 => compare (23, 34)
21, 23, 34, 5, 40, 44, 56, 75, 76, 90
21, 23, 34, 5, 40, 44, 56, 75, 76, 90 => compare (34, 5)
21, 23, 5, 34, 40, 44, 56, 75, 76, 90 => 34 sorted
Fasa 8
21, 23, 5, 34, 40, 44, 56, 75, 76, 90 => compare (21, 23)
21, 23, 5, 34, 40, 44, 56, 75, 76, 90
21, 23, 5, 34, 40, 44, 56, 75, 76, 90 => compare (23, 5)
21, 5, 23, 34, 40, 44, 56, 75, 76, 90 => 23 sorted
Fasa 9
21, 5, 23, 34, 40, 44, 56, 75, 76, 90 => compare (21, 5)
5, 21, 23, 34, 40, 44, 56, 75, 76, 90 => 21 sorted
5, 21, 23, 34, 40, 44, 56, 75, 76, 90 => 5 sorted
important methods
// driver code
public static void main(String args[]) {
int[] data = { 40,75, 23, 76, 44, 21, 56, 90, 34, 5};
SimpleSelection ss = new SimpleSelection();
//method ni jangan lupa panggil balik supaya dia susun dalam ascending order
ss.selectionSort(data);
System.out.println("Sort array in Ascending Order: ");
System.out.println(Arrays.toString(data));
}
}
5. TREE-
-BST
-add
-delete
-inorder, postorder, preorder BST
-AVL Tree