0% found this document useful (0 votes)
30 views9 pages

Revision

The document discusses different data structures and sorting algorithms: 1. It explains how to implement a linked list in Java and demonstrates adding and removing nodes from the linked list. 2. It provides examples of a queue and stack data structure by showing how to add and remove elements. 3. It demonstrates selection sort, bubble sort, and quicksort algorithms by showing the step-by-step process to sort a list of numbers in ascending order for each algorithm.

Uploaded by

nurnajaanadirah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views9 pages

Revision

The document discusses different data structures and sorting algorithms: 1. It explains how to implement a linked list in Java and demonstrates adding and removing nodes from the linked list. 2. It provides examples of a queue and stack data structure by showing how to add and remove elements. 3. It demonstrates selection sort, bubble sort, and quicksort algorithms by showing the step-by-step process to sort a list of numbers in ascending order for each algorithm.

Uploaded by

nurnajaanadirah
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

1.

LINKED LIST
import java.util.LinkedList;

public class lab25Oct {


public static void main(String[] args) {
// Create a LinkedList of strings
LinkedList <Integer> number;
number = new LinkedList<>();

// Add elements to the LinkedList


number.add(1);
number.add(2);
number.add(3);
number.add(4);

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

ii. Bubble Sort


40,75, 23, 76, 44, 21, 56, 90, 34, 5
Fasa 1
Step 1 = compare the first two elements (40, 75). 40 is smallest than 75, then swap them
40,75, 23, 76, 44, 21, 56, 90, 34, 5
Step 2 = compare ( 75, 23). 23 is smallest than 75, then swap them
40, 23, 75, 76, 44, 21, 56, 90, 34, 5 => compare (75,76)
40, 23, 75, 76, 44, 21, 56, 90, 34, 5
40, 23, 75, 76, 44, 21, 56, 90, 34, 5 => compare (76, 44)
40, 23, 75, 44, 76, 21, 56, 90, 34, 5
40, 23, 75, 44, 76, 21, 56, 90, 34, 5 => compare (76, 21)
40, 23, 75, 44, 21, 76, 56, 90, 34, 5
40, 23, 75, 44, 21, 76, 56, 90, 34, 5 => compare (76, 56)
40, 23, 75, 44, 21, 56, 76, 90, 34, 5
40, 23, 75, 44, 21, 56, 76, 90, 34, 5 => compare (76, 90)
40, 23, 75, 44, 21, 56, 76, 90, 34, 5
40, 23, 75, 44, 21, 56, 76, 90, 34, 5 => compare (90, 34)
40, 23, 75, 44, 21, 56, 76, 34, 90, 5
40, 23, 75, 44, 21, 56, 76, 34, 90, 5 => compare (90, 5)
40, 23, 75, 44, 21, 56, 76, 34, 5, 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

iii. Quick Sort


40,75, 23, 76, 44, 21, 56, 90, 34, 5=> swap 76, 5
40,75, 23, 5, 44, 21, 56, 90, 34, 76 => swap 75, 34
40, 34, 23, 5, 44, 21, 56, 90, 75, 76 => swap 44, 21
40, 34, 23, 5, 21, 44, 56, 90, 75, 76 => swap 40, 21
21, 34, 23, 5, 40, 44, 56, 90, 75, 76 => 40 sorted
21, 34, 23, 5 => swap 34, 5
21, 5, 23, 34 => swap 21, 5
5, 21, 23, 34 => 5 sorted
21, 23, 34 => swap 21, 21
21, 23, 34 => 21 sorted
23, 34 => swap 23, 23
23, 34 => 23 sorted
34 => sorted
44, 56, 90, 75, 76 => swap 44, 44
44, 56, 90, 75, 76 => 44 sorted
56, 90, 75, 76 => swap 56, 56
56, 90, 75, 76 => 56 sorted
90, 75, 76 => swap 90, 75
75, 90, 76 => 75 sorted
90, 76 => swap 90, 76
76, 90 => 76 sorted
90 => sorted
 the sorted list: 5, 21, 23, 34, 40, 44, 56, 75, 76, 90
_________________________________________________________________________

important methods

public class SimpleSelection {


void selectionSort(int array[]) {
int size = array.length;

for (int step = 0; step < size - 1; step++) {


int min_idx = step;

for (int i = step + 1; i < size; i++) {

// To sort in descending order, change > to < in this line.


// Select the minimum element in each loop.
if (array[i] < array[min_idx]) {
//min_idx untuk minimum index
min_idx = i;
}
}

// put min at the correct position


int temp = array[step];
array[step] = array[min_idx];
array[min_idx] = temp;
}
}

// 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

You might also like