Uploaded by Jov Dela Cruz

Exercise-6- -Sorting-Heap-Merge-and-Shell-CASTILLO

advertisement
Mark Andrei R. Castillo
09/03/2022
Exercise 6 | Sorting (Heap, Merge, and Shell)
Task 1
1. Perform the code below about the Shell Sort compile and show your sample output.
2. Perform the code below about the Heap Sort compile and show your sample output.
3. Perform the code below about the Merge Sort compile and show your sample output.
Task 2
1. Create a Java class that will dynamically accept array elements using an array.
2. Create another class that will accept the elements of the array created in item 1. The accepted elements
must be stored in an ArrayList.
3. Create a class that has methods that will perform the following sorting algorithms on the elements of the
ArrayList.
4.
Array
ArrayList
Shell Sort
Merge Sort
Heap Sort
5. Research and discuss the complexity of Shell, Merge and Heap. One paragraph each.
1. Perform the code below about the Shell Sort compile and show your sample output.
OUTPUT:
ShellSortTester.java
SOURCE CODE:
package com.mycompany.excercise6;
System.out.println(" Array before shell sort: ");
System.out.print(" ");
public class ShellSortTester {
public static void main(String[] args) {
for (int k = 0; k < list1.length; k++) {
System.out.print(list1[k] + " ");
ShellSort shellsorting = new ShellSort();
int[] list1 = {11,10,9,8,7,6,5,4,3};
}
int[] list = {11,10,9,8,7,6,5,4,3};
System.out.println();
System.out.println("------------------------------");
System.out.println(" Array after shell sort : ");
shellsorting.doShellSort(list);
shellsorting.printArray(list);
System.out.println("============================
==");
}
System.out.println("\t SHELL SORT");
System.out.println("============================
==");
ShellSort.java
SOURCE CODE:
package com.mycompany.excercise6;
int n = list.length;
public class ShellSort {
for(int gap = n/2; gap>0; gap/=2)
public void printArray(int list[]){
for(int i=gap; i<n;i+=1){
int n = list.length;
System.out.print(" ");
int temp = list[i];
for (int i = 0; i < n; ++i){
int j;
for (j = i; j >=gap && list [j-gap]>temp;j-=gap)
System.out.print(list[i] + " ");
list[j] = list[j-gap];
}
list[j] = temp;
System.out.println();
}
}
}
}
}
public void doShellSort(int list[]){
2. Perform the code below about the Heap Sort compile and show your sample output.
OUTPUT:
HeapSortTester.java
SOURCE CODE:
System.out.println(" Array before heap sort: ");
public class HeapSortTester {
System.out.print(" ");
public static void main(String[] args) {
heapSorting.prtintArray(list);
HeapSort heapSorting = new
HeapSort();
heapSorting.doHeapSort(list);
int[] list = {20,19,18,17,16,15,14,13};
System.out.println("============================
===");
System.out.println("\t HEAP SORT");
System.out.println();
System.out.println("------------------------------");
System.out.println(" Array after heap
sort: ");
System.out.print(" ");
System.out.println("============================
===");
heapSorting.prtintArray(list);
}
}
HeapSort.java
}
SOURCE CODE:
public void doHeapSort(int[] list) {
void heapify(int arr[], int n, int i) {
int n = list.length;
// Find largest among root, left child and right
// Build max heap
for (int i = n / 2 - 1; i >= 0; i--) {
child
int largest = i;
heapify(list, n, i);
int l = 2 * i + 1;
}
int r = 2 * i + 2;
// Heap sort
if (l < n && arr[l] > arr[largest])
for (int i = n - 1; i >= 0; i--) {
largest = l;
int temp = list[0];
if (r < n && arr[r] > arr[largest])
list[0] = list[i];
largest = r;
list[i] = temp;
// Heapify root element
// Swap and continue heapifying if root is not
largest
heapify(list, i, 0);
if (largest != i) {
}
int swap = arr[i];
}
arr[i] = arr[largest];
public void prtintArray(int[] list) {
arr[largest] = swap;
int n = list.length;
heapify(arr, n, largest);
for(int i = 0 ; i<n; i++) {
}
System.out.print(list[i] + "
}
");
}
}
3. Perform the code below about the Merge Sort compile and show your sample output.
OUTPUT:
System.out.println();
MergeSort.java
System.out.println("------------------------------");
SOURCE CODE:
System.out.println(" Array after
package com.mycompany.excercise6;
merge sort: ");
System.out.print(" ");
for(int i = 0 ; i<list.length; i++) {
System.out.print(list[i] + "
public class MergeSort {
");
public static void main(String[] args) {
}
}
int[] list = {40,39,38,37,36,35,34,33};
private static void doMergeSort(int[] list) {
System.out.println("============================
===");
if(list.length>1) {
int [] firstHalf = new
System.out.println("\t MERGE SORT");
int[list.length/2];
System.out.println("============================
===");
System.arraycopy(list, 0,
firstHalf, 0, list.length/2);
doMergeSort(firstHalf);
System.out.println(" Array before
merge sort: ");
System.out.print(" ");
for(int i = 0 ; i<list.length; i++) {
int secondHalfLength =
list.length - list.length/2;
int [] secondHalf = new
System.out.print(list[i] + "
int[secondHalfLength];
");
}
doMergeSort(list);
System.arraycopy(list,
list.length/2, secondHalf, 0, secondHalfLength);
doMergeSort(secondHalf);
}
merge(firstHalf,
else
secondHalf, list);
temp[curr3++] =
list2[curr2++];
}
}
while(curr1 < list1.length)
}
temp[curr3++] =
private static void merge(int[] list1, int[] list2,
int[] temp) {
list1[curr1++];
int curr1 = 0;
while(curr2 < list2.length)
int curr2 = 0;
temp[curr3++] =
list2[curr2++];
int curr3 = 0;
}
}
while(curr1 <list1.length && curr2 <
list2.length) {
if(list1[curr1] < list2[curr2])
{
temp[curr3++] =
list1[curr1++];
Task 2
Create a Java class that will dynamically accept array elements using an array.
Create another class that will accept the elements of the array created in item 1. The accepted elements must
be stored in an ArrayList.
Create a class that has methods that will perform the following sorting algorithms on the elements of the
ArrayList.
Array
ArrayList
Shell Sort
Merge Sort
Heap Sort
OUTPUT:
Challenge.java
System.out.println("\t [2] ARRAY LIST");
SOURCE CODE:
System.out.println("\t [3] HEAP SORT");
package com.mycompany.challenge;
System.out.println("\t [4] MERGE SORT");
System.out.println("\t [5] SHELL SORT");
import java.util.Scanner;
System.out.println("\t [0] EXIT");
System.out.println("============================
===");
public class Challenge {
System.out.print("Enter Choice: ");
public static void main(String[] args) {
choice = scan.nextInt();
switch (choice) {
Scanner scan = new Scanner(System.in);
case 1 -> {
int size;
System.out.println("-------------------------------");
int choice;
System.out.println("
int data[];
System.out.println("-------------------------------");
System.out.println("============================
===");
ARRAY");
System.out.print(" [ | ");
for (int i = 0; i < size; i++) {
System.out.print(data[i]+" | ");
System.out.println("\tEXCERCISE 6 SORT");
}
System.out.println("============================
===");
System.out.print("ARRAY SIZE: ");
size = scan.nextInt();
System.out.print("]");}
case 2 ->
System.out.print("""
-------------------------------
System.out.println("-------------------------------");
ARRAY LIST ELEMENT
data = new int[size];
-------------------------------
for (int i = 0; i < size; i++) {
"""+"
"+ARRLIST.getdata()+"\n-------------------------------");
System.out.print("ENTER ARRAY ELEMENTS : ");
data[i] = scan.nextInt();
}
Arraylist ARRLIST = new Arraylist();
ARRLIST.setdata(data);
case 3 -> {
SortingMethods hs = new
SortingMethods(ARRLIST);
int[] arrayList1 = ARRLIST.toArray();
hs.HeapSort(arrayList1);
System.out.println("-------------------------------");
while (true) {
System.out.println();
System.out.println("
HEAP SORT");
System.out.println("-------------------------------");
System.out.print(" [ | ");
System.out.println("============================
===");
for (int i = 0; i < size; i++) {
System.out.print(arrayList1[i]+" | ");
System.out.println("\t [1] ARRAY");
}
}
case 4 -> {
System.out.println("
int[] arrayList2 = ARRLIST.toArray();
System.out.println("-------------------------------");
SortingMethods.MergeSort(arrayList2);
System.out.print(" [ | ");
System.out.println("-------------------------------");
System.out.println("
SHELL SORT");
for (int i = 0; i < size; i++) {
MERGE SORT");
System.out.print(arrayList3[i]+" | ");
System.out.println("-------------------------------");
}
}
System.out.print(" [ | ");
for (int i = 0; i < size; i++) {
System.out.print(arrayList2[i]+" | ");
}
}
case 0 -> System.exit(0);
}
}
}
case 5 -> {
}
SortingMethods ss = new
SortingMethods(ARRLIST);
int[] arrayList3 = ARRLIST.toArray();
ss.ShellSort(arrayList3);
System.out.println("-------------------------------");
if(list.length>1) {
SortingMethods.java
int [] firstHalf = new
SOURCE CODE:
package com.mycompany.challenge;
int[list.length/2];
System.arraycopy(list, 0,
firstHalf, 0, list.length/2);
public class SortingMethods {
MergeSort(firstHalf);
int secondHalfLength =
public SortingMethods(Arraylist al){
list.length - list.length/2;
int [] secondHalf = new
int[secondHalfLength];
al.getdata();
}
/** The method for sorting the numbers
System.arraycopy(list,
list.length/2, secondHalf, 0, secondHalfLength);
MergeSort(secondHalf);
* @param list */
merge(firstHalf,
/** Merge two sorted list
secondHalf, list);
* @param list*/
}
public static void MergeSort(int[] list) {
}
private static void merge(int[] list1, int[] list2,
int[] temp) {
heapify(list, i, 0);
}
int curr1 = 0;
}
int curr2 = 0;
int curr3 = 0;
public void prtintArray(int[] list) {
int n = list.length;
while(curr1 <list1.length && curr2 <
list2.length) {
for(int i = 0 ; i<n; i++) {
if(list1[curr1] < list2[curr2])
{
System.out.print(list[i] + "
temp[curr3++] =
");
list1[curr1++];
}
}
else
temp[curr3++] =
}
list2[curr2++];
}
void heapify(int arr[], int n, int i) {
while(curr1 < list1.length)
temp[curr3++] =
list1[curr1++];
// Find largest among root, left child and right
child
int largest = i;
int l = 2 * i + 1;
while(curr2 < list2.length)
int r = 2 * i + 2;
temp[curr3++] =
list2[curr2++];
}
if (l < n && arr[l] > arr[largest])
largest = l;
public void HeapSort(int[] list) {
if (r < n && arr[r] > arr[largest])
int n = list.length;
largest = r;
// Build max heap
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(list, n, i);
}
// Swap and continue heapifying if root is not
largest
if (largest != i) {
int swap = arr[i];
arr[i] = arr[largest];
// Heap sort
arr[largest] = swap;
for (int i = n - 1; i >= 0; i--) {
int temp = list[0];
list[0] = list[i];
list[i] = temp;
// Heapify root element
heapify(arr, n, largest);
}
}
public void ShellSort(int list[]){
int n = list.length;
for (j = i; j >=gap && list [j-gap]>temp;j-=gap)
list[j] = list[j-gap];
for(int gap = n/2; gap>0; gap/=2){
list[j] = temp;
}
for(int i=gap; i<n;i+=1){
}
}
}
int temp = list[i];
int j;
Arraylist.java
SOURCE CODE:
package com.mycompany.challenge;
public class Arraylist {
private final java.util.ArrayList<Integer> ar;
public Arraylist() {
ar = new java.util.ArrayList<>();
}
public void setdata(int[] arr) {
for (Integer element : arr) {
ar.add(element);
}
}
public int[] toArray() {
int[] arr = ar.stream().mapToInt(i -> i).toArray();
return arr;
}
public java.util.ArrayList<Integer> getdata() {
return this.ar;
}
}
5. Research and discuss the complexity of Shell, Merge and Heap. One paragraph each.
SHELLSORT
An in-place comparison sort is Shellsort, commonly referred to as Shell sort or Shell's approach. It can be viewed as
a generalization of either sorting by insertion or sorting by exchange (bubble sort) (insertion sort). Starting with pairs
of items that are far off from one another, the approach gradually closes the distance between the elements to be
compared. It can put some out-of-place elements into position faster than a basic closest neighbor swap by starting
with far apart pieces.
MERGESORT
An effective sorting algorithm called Merge Sort has an average, best-case, and worst-case time complexity of O. (n
log n). With its default implementation, Merge Sort has an additional space complexity of O(n). In the majority of
implementations, the order of equal elements is the same in the input and output, leading to a stable sort. Merge sort
is a divide-and-conquer strategy.
HEAPSORT
The average, best-case, and worst-case time complexity of the sorting method heapsort is O. (n log n). Because
Heapsort is slower than Quicksort and Merge Sort, it is less frequently used in actual applications. Heapsort is a
comparison-based sorting algorithm. In that it constantly shrinks the size of the unsorted zone by removing the
biggest element from it and placing it into the sorted region, heapsort can be compared to an improved version of
selection sort. Heapsort divides its input into sorted and unsorted zones, just like selection sort. Heap sort keeps the
unsorted region in a heap data structure so that it may find the largest element in each step more quickly than
selection sort, which wastes time with a linear-time scan of the unsorted region.
Download