Dear experts, Coding in Java. Please modify below program in order to print Stri
ID: 3780038 • Letter: D
Question
Dear experts,
Coding in Java. Please modify below program in order to print String value such as O,N.M,P and sorted become M,N,O,P,Q. Thanks.
public class heap_Sort{
public static void main(String a[]){
int i;
int arr[] = {1,3,4,5,2};
System.out.println(" Heap Sort --------------- ");
System.out.println(" Unsorted Array ");
for (i = 0; i < arr.length; i++)
System.out.print(" "+arr[i]);
for(i=arr.length; i>1; i--){
fnSortHeap(arr, i - 1);
}
System.out.println(" Sorted array --------------- ");
for (i = 0; i < arr.length; i++)
System.out.print(" "+arr[i]);
}
public static void fnSortHeap(int array[], int arr_ubound){
int i, o;
int lChild, rChild, mChild, root, temp;
root = (arr_ubound-1)/2;
for(o = root; o >= 0; o--){
for(i=root;i>=0;i--){
lChild = (2*i)+1;
rChild = (2*i)+2;
if((lChild <= arr_ubound) && (rChild <= arr_ubound)){
if(array[rChild] >= array[lChild])
mChild = rChild;
else
mChild = lChild;
}
else{
if(rChild > arr_ubound)
mChild = lChild;
else
mChild = rChild;
}
if(array[i] < array[mChild]){
temp = array[i];
array[i] = array[mChild];
array[mChild] = temp;
}
}
}
temp = array[0];
array[0] = array[arr_ubound];
array[arr_ubound] = temp;
return;
}
}
Output of the example:
C:rraysorting>Javac heap_Sort.java
C:rraysorting>java heap_Sort
Heap Sort
---------------
Unsorted Array
1 3 4 5 2
Sorted array
---------------
1 2 3 4 5
C:rraysorting>_
C:rraysorting>Javac heap_Sort.java
C:rraysorting>java heap_Sort
Heap Sort
---------------
Unsorted Array
1 3 4 5 2
Sorted array
---------------
1 2 3 4 5
C:rraysorting>_
Explanation / Answer
// Heap_Sort.java
public class Heap_Sort
{
public static void main(String a[])
{
int i;
String str = "O,N,M,Q,P";
String arr[] = str.split(",");
System.out.println("Heap Sort --------------- ");
System.out.println("Unsorted Array ");
for (i = 0; i < arr.length; i++)
System.out.print(" "+arr[i]);
for(i=arr.length; i>1; i--)
{
fnSortHeap(arr, i - 1);
}
System.out.println(" Sorted array --------------- ");
for (i = 0; i < arr.length; i++)
System.out.print(" "+arr[i]);
System.out.println();
}
public static void fnSortHeap(String array[], int arr_ubound)
{
int i, o;
int lChild, rChild, mChild, root;
String temp;
root = (arr_ubound-1)/2;
for(o = root; o >= 0; o--)
{
for(i=root;i>=0;i--)
{
lChild = (2*i)+1;
rChild = (2*i)+2;
char r = array[rChild].charAt(0);
char l = array[lChild].charAt(0);
if((lChild <= arr_ubound) && (rChild <= arr_ubound))
{
if(r >= l)
mChild = rChild;
else
mChild = lChild;
}
else
{
if(rChild > arr_ubound)
mChild = lChild;
else
mChild = rChild;
}
char m = array[mChild].charAt(0);
char c = array[i].charAt(0);
if(c < m)
{
temp = array[i];
array[i] = array[mChild];
array[mChild] = temp;
}
}
}
temp = array[0];
array[0] = array[arr_ubound];
array[arr_ubound] = temp;
return;
}
}
/*
output:
Heap Sort
---------------
Unsorted Array
O N M Q P
Sorted array
---------------
M N O P Q
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.