update,
This commit is contained in:
37
_resources/it114105/itp4510/Lab04/Lab4.1/README.md.original
Normal file
37
_resources/it114105/itp4510/Lab04/Lab4.1/README.md.original
Normal file
@@ -0,0 +1,37 @@
|
||||
### Q1
|
||||
For each of the following code snippets, identify the critical section(s), compute the time (T(n)) each one takes, and specify the asymptotic complexity using Big-O.
|
||||
|
||||
(i)
|
||||
```
|
||||
for(int k=0; k<n; k+=3) {
|
||||
for(int p=n; p>6; p--) {
|
||||
System.out.println(p%2);
|
||||
}
|
||||
}
|
||||
|
||||
O(n^2)
|
||||
```
|
||||
|
||||
(ii)
|
||||
```
|
||||
for(int k=0;k<=n/8;k++) {
|
||||
System.out.println(k);
|
||||
}
|
||||
System.out.println(“Next”);
|
||||
for (int p=n; p>=1;p--) {
|
||||
System.out.println(p%2);
|
||||
}
|
||||
|
||||
O(n)
|
||||
```
|
||||
|
||||
(iii)
|
||||
```
|
||||
for (int k=0; k<n-1; k++) {
|
||||
for (int m=k+1; m<n; m++) {
|
||||
System.out.println(k*m);
|
||||
}
|
||||
}
|
||||
|
||||
O(n^2)
|
||||
```
|
23
_resources/it114105/itp4510/Lab04/Lab4.2/BubbleSort.java
Normal file
23
_resources/it114105/itp4510/Lab04/Lab4.2/BubbleSort.java
Normal file
@@ -0,0 +1,23 @@
|
||||
public class BubbleSort {
|
||||
public static void main(String [] args) {
|
||||
int [] arr = {21, 13, 8, 42, 19, 5, 34, 61};
|
||||
bubbleSort(arr);
|
||||
|
||||
for (int i=0; i<arr.length; i++)
|
||||
System.out.print(arr[i] + " ");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
public static void bubbleSort(int [] array) {
|
||||
for (int pass=0; pass < array.length-1; pass++)
|
||||
for (int i=0; i < array.length-pass-1; i++)
|
||||
if (array [ i ] > array [ i+1 ])
|
||||
swap(array, i, i+1);
|
||||
}
|
||||
|
||||
public static void swap (int [ ] array, int first, int second) {
|
||||
int temp = array [ first ];
|
||||
array [ first ] = array [ second ];
|
||||
array [ second ] = temp;
|
||||
}
|
||||
}
|
59
_resources/it114105/itp4510/Lab04/Lab4.2/QucikSort.java
Normal file
59
_resources/it114105/itp4510/Lab04/Lab4.2/QucikSort.java
Normal file
@@ -0,0 +1,59 @@
|
||||
public class QuickSort {
|
||||
|
||||
public static void main(String [ ] args) {
|
||||
int [ ] arr = {21, 13, 8, 42, 19, 5, 34, 61};
|
||||
quicksort(arr, 0, arr.length-1);
|
||||
for (int i=0; i<arr.length; i++)
|
||||
System.out.print(arr[i] + " ");
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
|
||||
public static void quicksort (int array[], int first, int last) {
|
||||
int pivot;
|
||||
int i;
|
||||
|
||||
if (first >= last)
|
||||
return;
|
||||
|
||||
pivot = partition(array, first, last);
|
||||
|
||||
quicksort(array, first, pivot - 1); /* sort left side */
|
||||
quicksort(array, pivot + 1, last); /* sort right side */
|
||||
}
|
||||
|
||||
public static int partition(int array[], int left, int right){
|
||||
int i = left;
|
||||
|
||||
while (true) {
|
||||
while (array[i] <= array[right] && i != right)
|
||||
--right;
|
||||
|
||||
if (i == right)
|
||||
return i ;
|
||||
|
||||
if (array[i] > array[right]) {
|
||||
swap(array, i, right);
|
||||
i = right;
|
||||
}
|
||||
while (array[left] <= array[i] && left != i )
|
||||
++left;
|
||||
|
||||
if (i == left)
|
||||
return i;
|
||||
|
||||
if (array[left] > array[i]) {
|
||||
swap(array, i, left);
|
||||
i = left;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void swap(int [ ] arr, int x, int y) {
|
||||
int temp=arr[x];
|
||||
arr[x] = arr[y];
|
||||
arr[y] = temp;
|
||||
}
|
||||
}
|
||||
|
||||
|
80
_resources/it114105/itp4510/Lab04/Lab4.2/QuickVsBubble.java
Normal file
80
_resources/it114105/itp4510/Lab04/Lab4.2/QuickVsBubble.java
Normal file
@@ -0,0 +1,80 @@
|
||||
public class QuickVsBubble {
|
||||
public static void main(String [] args) {
|
||||
int n = Integer.parseInt(args[0]);
|
||||
int [] arrayQS = new int[n];
|
||||
int [] arrayBS = new int[n];
|
||||
|
||||
for (int i=0; i<n; i++) {
|
||||
int x = (int) (Math.random() * 10000);
|
||||
arrayQS[i] = x;
|
||||
arrayBS[i] = x;
|
||||
}
|
||||
|
||||
long startTime, endTime;
|
||||
|
||||
startTime = System.nanoTime();
|
||||
quicksort(arrayQS, 0, arrayQS.length-1);
|
||||
endTime = System.nanoTime();
|
||||
System.out.println("Quick Sort = " + (endTime-startTime));
|
||||
|
||||
startTime = System.nanoTime();
|
||||
bubbleSort(arrayBS);
|
||||
endTime = System.nanoTime();
|
||||
System.out.println("Bubble Sort = " + (endTime-startTime));
|
||||
}
|
||||
|
||||
public static void quicksort (int array[], int first, int last) {
|
||||
int pivot;
|
||||
|
||||
if (first >= last)
|
||||
return;
|
||||
|
||||
pivot = partition(array, first, last);
|
||||
|
||||
quicksort(array, first, pivot - 1); /* sort left side */
|
||||
quicksort(array, pivot + 1, last); /* sort right side */
|
||||
}
|
||||
|
||||
public static int partition(int array[], int left, int right){
|
||||
int i = left;
|
||||
|
||||
while (true) {
|
||||
while (array[i] <= array[right] && i != right)
|
||||
--right;
|
||||
|
||||
if (i == right)
|
||||
return i ;
|
||||
|
||||
if (array[i] > array[right]) {
|
||||
swap(array, i, right);
|
||||
i = right;
|
||||
}
|
||||
while (array[left] <= array[i] && left != i )
|
||||
++left;
|
||||
|
||||
if (i == left)
|
||||
return i;
|
||||
|
||||
if (array[left] > array[i]) {
|
||||
swap(array, i, left);
|
||||
i = left;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void swap(int [ ] arr, int x, int y) {
|
||||
int temp=arr[x];
|
||||
arr[x] = arr[y];
|
||||
arr[y] = temp;
|
||||
}
|
||||
|
||||
|
||||
public static void bubbleSort(int [] array) {
|
||||
for (int pass=0; pass < array.length-1; pass++)
|
||||
for (int i=0; i < array.length-pass-1; i++)
|
||||
if (array [ i ] > array [ i+1 ])
|
||||
swap(array, i, i+1);
|
||||
}
|
||||
}
|
||||
|
||||
|
125
_resources/it114105/itp4510/Lab04/Lab4.2/README.md.original
Normal file
125
_resources/it114105/itp4510/Lab04/Lab4.2/README.md.original
Normal file
@@ -0,0 +1,125 @@
|
||||
### Q1
|
||||
Show the steps of sorting the integers 23, 48, 11, 3, 79, 35, 7, 19 by using
|
||||
i. Selection sort
|
||||
ii. Bubble sort
|
||||
iii. Insertion sort
|
||||
iv. Merge sort
|
||||
v. In-Place Quick sort (with 1 st element as pivot)
|
||||
|
||||
```
|
||||
# Selection sort
|
||||
23 48 11 3 79 35 7 19
|
||||
3 [48 11 23 79 35 7 19]
|
||||
3 7 [11 23 79 35 48 19]
|
||||
3 7 11 [23 79 35 48 19]
|
||||
3 7 11 19 [79 35 48 23]
|
||||
3 7 11 19 23 [35 48 79]
|
||||
3 7 11 19 23 35 [48 79]
|
||||
3 7 11 19 23 35 48 [79]
|
||||
|
||||
# Bubble Sort
|
||||
[23 11 3 48 35 7 19] 79
|
||||
[11 3 23 35 7 19] 48 79
|
||||
[3 11 23 7 19] 35 48 79
|
||||
[3 11 7 19] 23 35 48 79
|
||||
[3 7 11] 19 23 35 48 79
|
||||
3 7 11 19 23 35 48 79
|
||||
|
||||
# Insertion Sort
|
||||
23 48 11 3 79 35 7 19
|
||||
[23 48] 11 3 79 35 7 19
|
||||
[11 23 48] 3 79 35 7 19
|
||||
[3 11 23 48] 79 35 7 19
|
||||
[3 11 23 48 79] 35 7 19
|
||||
[3 11 23 35 48 79] 7 19
|
||||
[3 7 11 23 35 48 79] 19
|
||||
[3 7 11 19 23 35 48 79]
|
||||
|
||||
# Merge Sort
|
||||
{23 48 11 3 79 35 7 19}
|
||||
{23 48 11 3}{79 35 7 19}
|
||||
{23 48}{11 3} {79 35}{7 19}
|
||||
{23}{48}{11}{3}{79}{35}{7}{19}
|
||||
{23 48}{3 11}{35 79}{7 19}
|
||||
{3 11 23 48} {7 19 35 79}
|
||||
{3 7 11 19 23 35 48 79}
|
||||
|
||||
# Qucik sort
|
||||
|
||||
index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | StoreIndex
|
||||
data | 23| 48| 11| 3| 79| 35| 7| 19| 1
|
||||
Step 1| 23| 48| 11| 3| 79| 35| 7| 19| 1
|
||||
Step 2| 23| 11| 48| 3| 79| 35| 7| 19| 2
|
||||
Step 3| 23| 11| 3| 48| 79| 35| 7| 19| 3
|
||||
Step 4| 23| 11| 3| 48| 79| 35| 7| 19| 3
|
||||
Step 5| 23| 11| 3| 48| 79| 35| 7| 19| 3
|
||||
Step 6| 23| 11| 3| 7| 79| 35| 48| 19| 4
|
||||
Step 7| 23| 11| 3| 7| 19| 35| 48| 79| 5
|
||||
Step 8| 19| 11| 3| 7| 23| 35| 48| 79| 5
|
||||
```
|
||||
|
||||
### Q2
|
||||
Show the steps for sorting the integers 79, 48, 35, 23, 19, 11, 7, 3 by using the above five methods.
|
||||
|
||||
```
|
||||
# Selection Sort
|
||||
79 48 35 23 19 11 7 3
|
||||
3 [48 35 23 19 11 7 79]
|
||||
3 7 [35 23 19 11 48 79]
|
||||
3 7 11 [23 19 35 48 79]
|
||||
3 7 11 19 [23 35 48 79]
|
||||
3 7 11 19 23 [35 48 79]
|
||||
3 7 11 19 23 35 [48 79]
|
||||
3 7 11 19 23 35 48 [79]
|
||||
|
||||
# Bubble Sort
|
||||
79 48 35 23 19 11 7 3
|
||||
[48 35 23 19 11 7 3] 79
|
||||
[35 23 19 11 7 3] 48 79
|
||||
[23 19 11 7 3] 35 48 79
|
||||
[19 11 7 3] 23 35 48 79
|
||||
[11 7 3] 19 23 35 48 79
|
||||
[7 3] 11 19 23 35 48 79
|
||||
[3] 7 11 19 23 35 48 79
|
||||
3 7 11 19 23 35 48 79
|
||||
|
||||
# Insertion Sort
|
||||
79 48 35 23 19 11 7 3
|
||||
[48 79] 35 23 19 11 7 3
|
||||
[35 48 79] 23 19 11 7 3
|
||||
[23 35 48 79] 19 11 7 3
|
||||
[19 23 35 48 79] 11 7 3
|
||||
[11 19 23 35 48 79] 7 3
|
||||
[7 11 19 23 35 48 79] 3
|
||||
[3 7 11 19 23 35 48 79]
|
||||
|
||||
# Merge Sort
|
||||
{79 48 35 23 19 11 7 3}
|
||||
{79 48 35 23} {19 11 7 3}
|
||||
{79 48} {35 23} {19 11} {7 3}
|
||||
{79} {48} {35} {23} {19} {11} {7} {3}
|
||||
{48 79} {23 35} {11 19} {3 7}
|
||||
{23 35 48 79} {3 7 11 19}
|
||||
{3 7 11 19 23 35 48 79}
|
||||
|
||||
# Quick Sort
|
||||
index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | StoreIndex
|
||||
data | 79| 48| 35| 23| 19| 11| 7| 3| 1
|
||||
Step 1| 79| 48| 35| 23| 19| 11| 7| 3| 2
|
||||
Step 2| 79| 48| 35| 23| 19| 11| 7| 3| 3
|
||||
Step 3| 79| 48| 35| 23| 19| 11| 7| 3| 4
|
||||
Step 4| 79| 48| 35| 23| 19| 11| 7| 3| 5
|
||||
Step 5| 79| 48| 35| 23| 19| 11| 7| 3| 6
|
||||
Step 6| 79| 48| 35| 23| 19| 11| 7| 3| 7
|
||||
Step 7| 79| 48| 35| 23| 19| 11| 7| 3| 8
|
||||
Step 8| 3| 48| 35| 23| 19| 11| 7| 79| 8
|
||||
```
|
||||
|
||||
### Q3
|
||||
| Algorithms | Data is in random order | Data is ascesndinf order(Guess) | Data is in descending order | In place? (yes or no) |
|
||||
| --- | --- | --- | --- | --- |
|
||||
| Selection | O(n^2) | O(n^2) | O(n^2) | yes |
|
||||
| Bubble | O(n^2) | O(n) | O(n^2) | yes |
|
||||
| Insertion | O(n^2) | O(n) | O(n^2) | yes |
|
||||
| Merge | O(n log n) | O(n log n) | O(n log n) | no |
|
||||
| Quick | O(n log n) | O(n^2) | O(n^2) | yes |
|
229
_resources/it114105/itp4510/Lab04/Lab4.3/READMD.md
Normal file
229
_resources/it114105/itp4510/Lab04/Lab4.3/READMD.md
Normal file
@@ -0,0 +1,229 @@
|
||||
### Q1
|
||||
Explain why we need to maintain a binary search tree in balance such as an AVL tree.
|
||||
```
|
||||
Only a balanced tree can guarantee search performance of O(logN).
|
||||
and, AVL tree’s theoretical search performance is 1.44 log2N. (see lecture notes)
|
||||
```
|
||||
|
||||
### Q2
|
||||
Build a binary search tree with the following integers being inserted in the order from
|
||||
left to right: 72, 48, 35, 80, 60, 85, 75, 21, 9, 28
|
||||
|
||||
```
|
||||
72
|
||||
/ \
|
||||
/ \
|
||||
/ \
|
||||
/ \
|
||||
48 80
|
||||
/ \ / \
|
||||
/ \ / \
|
||||
35 60 75 85
|
||||
/
|
||||
21
|
||||
/ \
|
||||
9 28
|
||||
```
|
||||
|
||||
### Q3
|
||||
With the integers in Q2, show the steps of building an AVL tree. Indicate also the
|
||||
balance value of each node and the rotations so required to maintain the tree in
|
||||
balance.
|
||||
|
||||
```
|
||||
insert 72
|
||||
|
||||
72
|
||||
|
||||
insert 48
|
||||
72
|
||||
/
|
||||
48
|
||||
|
||||
insert 35
|
||||
72
|
||||
/
|
||||
48
|
||||
/
|
||||
35
|
||||
|
||||
=>Right rotate at 72
|
||||
48
|
||||
/ \
|
||||
/ \
|
||||
35 72
|
||||
|
||||
|
||||
insert 80
|
||||
48
|
||||
/ \
|
||||
/ \
|
||||
35 72
|
||||
\
|
||||
80
|
||||
|
||||
insert 60
|
||||
48
|
||||
/ \
|
||||
/ \
|
||||
35 72
|
||||
/ \
|
||||
/ \
|
||||
60 80
|
||||
|
||||
insert 85
|
||||
48
|
||||
/ \
|
||||
/ \
|
||||
35 72
|
||||
/ \
|
||||
/ \
|
||||
60 80
|
||||
\
|
||||
85
|
||||
|
||||
=> Left rotate at 48
|
||||
72
|
||||
/ \
|
||||
/ \
|
||||
48 80
|
||||
/ \ \
|
||||
/ \ 85
|
||||
35 60
|
||||
|
||||
insert 75
|
||||
72
|
||||
/ \
|
||||
/ \
|
||||
/ \
|
||||
/ \
|
||||
48 80
|
||||
/ \ / \
|
||||
/ \ / \
|
||||
35 60 75 85
|
||||
|
||||
insert 21
|
||||
72
|
||||
/ \
|
||||
/ \
|
||||
/ \
|
||||
/ \
|
||||
48 80
|
||||
/ \ / \
|
||||
/ \ / \
|
||||
35 60 75 85
|
||||
/
|
||||
21
|
||||
|
||||
insert 9
|
||||
72
|
||||
/ \
|
||||
/ \
|
||||
/ \
|
||||
/ \
|
||||
48 80
|
||||
/ \ / \
|
||||
/ \ / \
|
||||
35 60 75 85
|
||||
/
|
||||
21
|
||||
/
|
||||
9
|
||||
|
||||
=> Right rotate at 35
|
||||
72
|
||||
/ \
|
||||
/ \
|
||||
/ \
|
||||
/ \
|
||||
48 80
|
||||
/ \ / \
|
||||
/ \ / \
|
||||
21 60 75 85
|
||||
/ \
|
||||
9 35
|
||||
|
||||
insert 28
|
||||
72
|
||||
/ \
|
||||
/ \
|
||||
/ \
|
||||
/ \
|
||||
48 80
|
||||
/ \ / \
|
||||
/ \ / \
|
||||
21 60 75 85
|
||||
/ \
|
||||
9 35
|
||||
/
|
||||
28
|
||||
|
||||
=> double rotate: left rotate at 21
|
||||
72
|
||||
/ \
|
||||
/ \
|
||||
/ \
|
||||
/ \
|
||||
48 80
|
||||
/ \ / \
|
||||
/ \ / \
|
||||
35 60 75 85
|
||||
/
|
||||
21
|
||||
/ \
|
||||
9 28
|
||||
|
||||
=> double rotate: right ritate at 48
|
||||
72
|
||||
/ \
|
||||
/ \
|
||||
/ \
|
||||
/ \
|
||||
35 80
|
||||
/ \ / \
|
||||
/ \ / \
|
||||
21 48 75 85
|
||||
/ \ \
|
||||
9 28 60
|
||||
```
|
||||
|
||||
### Q4
|
||||
Show the steps of deleting the following nodes from the AVL tree in Q3. Indicate also the balance value of each node and the rotations so required to maintain the tree in balance.
|
||||
|
||||
60, 48
|
||||
|
||||
```
|
||||
delete 60
|
||||
72
|
||||
/ \
|
||||
/ \
|
||||
/ \
|
||||
/ \
|
||||
35 80
|
||||
/ \ / \
|
||||
/ \ / \
|
||||
21 48 75 85
|
||||
/ \
|
||||
9 28
|
||||
|
||||
delete 48
|
||||
72
|
||||
/ \
|
||||
/ \
|
||||
35 80
|
||||
/ / \
|
||||
21 / \
|
||||
/ \ 75 85
|
||||
9 28
|
||||
|
||||
=> rotate at 35
|
||||
72
|
||||
/ \
|
||||
/ \
|
||||
/ \
|
||||
21 80
|
||||
/ \ / \
|
||||
9 35 / \
|
||||
/ 75 85
|
||||
28
|
||||
```
|
Reference in New Issue
Block a user