update,
This commit is contained in:
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 |
|
Reference in New Issue
Block a user