Solution :
I have taken your example in my output.
a)HEAP SORT:
Heap Sort Algorithm :
1. Build max heap from input data.
2. Now,the largest item is present at root of the
heap. Replace it with the last node of heap then reduce size of
heap by Then,heapify the root of tree.
3. Repeat above steps while size of heap is
greater than 1.
Screenshot:
Output:
Code :
import java.util.Scanner;
public class Main{
static void heapify(int a[],int n,int i){
int largest=i; // Initialize
largest as root
int l=2*i+1; // left =
2*i + 1
int r=2*i+2; // right =
2*i + 2
if(l<n &&
a[l]>a[largest]) // If left child is larger than root
largest=l;
if(r<n &&
a[r]>a[largest]) // If right child is larger than largest so
far
largest=r;
if(largest!=i){ // If largest is
not root
int
swap=a[i];
a[i]=a[largest];
a[largest]=swap;
heapify(a,n,largest); // Recursively heapify the affected
sub-tree
}
}
static void sort(int a[]){
int n=a.length;
int i;
for(i=n/2-1;i>= 0;i--) // Build
heap
heapify(a,n,i);
for(i=n-1;i>=0;i--){ // One by
one extract an element from heap then Move current root to
end
int
temp=a[0];
a[0]=a[i];
a[i]=temp;
heapify(a,i,0);
// call max heapify on the reduced heap
}
}
static void print_array(int a[]){
int n=a.length;
for (int i=0;i<n;++i)
System.out.print(a[i]+" ");
System.out.println();
}
public static void main(String args[]){
int n;
Scanner sc=new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n=sc.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
sort(a);
System.out.print("sorted array :
");
print_array(a);
}
}
-------------------------------------------------------------------------------
b)QUICK SORT :
Screenshot :
Output :
Code :
import java.util.Scanner;
class Main{
static int partition(int a[],int low,int high){
int j;
int pivot=a[high];
int i=low-1;
for(j=low;j<high;j++){
if(a[j]<pivot){
i++;
int temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
int temp=a[i+1];
a[i+1]=a[high];
a[high]=temp;
return i+1;
}
static void sort(int a[],int low,int high){
if(low<high){
int
pi=partition(a,low,high);
sort(a,low,pi-1);
sort(a,pi+1,high);
}
}
static void print_array(int a[]){
int i;
int n=a.length;
for(i=0;i<n;++i)
System.out.print(a[i]+" ");
System.out.println();
}
public static void main(String args[]){
int n;
Scanner sc=new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n=sc.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
sort(a, 0, n-1);
System.out.print("sorted array :
");
print_array(a);
}
}
------------------------------------------------------------------------------------
c)RADIX SORT :
Screenshot:
OUTPUT:
CODE :
import java.io.*;
import java.util.*;
class Main{
static int getMax(int a[],int n){ //function to get
maximum value in array
int max=a[0];
int i;
for(i=1;i<n;i++)
if(a[i]>max)
max=a[i];
return max;
}
static void countSort(int a[],int n,int exp){
//function to do counting sort of array
int output[]=new int[n]; // output
array
int i;
int count[]=new int[10];
Arrays.fill(count,0);
for(i=0;i<n;i++) // Store count
of occurrences in count[]
count[(a[i]/exp)%10]++;
//Change count[i] so that count[i]
contains actual position of digit in output[]
for(i=1;i<10;i++)
count[i]+=count[i-1];
for (i = n - 1; i >= 0; i--){ //
Build the output array
output[count[
(a[i]/exp)%10 ] - 1] = a[i];
count[
(a[i]/exp)%10 ]--;
}
for(i=0;i<n;i++) //Copy the
output array to arr[]
a[i]=output[i];
}
static void sort(int a[],int n){
int m=getMax(a,n); //Find maximum
to know number of digits
for (int exp = 1; m/exp > 0; exp
*= 10) // Do counting sort for every digit
countSort(a,n,exp);
}
static void print_array(int a[]){
int n = a.length;
for(int i=0; i<n; i++)
System.out.print(a[i]+" ");
}
public static void main(String args[]){
int n;
Scanner sc=new Scanner(System.in);
System.out.print("Enter no. of elements you want in array:");
n=sc.nextInt();
int a[] = new int[n];
System.out.println("Enter all the elements:");
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
sort(a,n);
System.out.print("sorted array :
");
print_array(a);
}
}
-------------------------------------------------------------
PLEASE GIVE YOUR VALUABLE FEEDBACK AND ALSO RATE MY EFFORTS.
THANK YOU
Implement the following sorting algorithms using Java: a. Heap Sort. b. Quick Sort. c. Radix Sort....
Implement and compare sorting algorithms. The task is to sort a list of integers using 5 sorting algorithms: selection sort insertion sort merge sort heap sort quicksort Your program should include 5 separate sorting methods, though it is fine for them to call some common methods (like "swap") if needed. Each sorting method should also count the number of comparison operations and assignment operations on the array elements during the sorting process. In the main program, two types of array...
Which sorting algorithms can be employed for external sorting? Merge Sort, Heap Sort, Quick Sort Illustrate with reasons The answer is merge sort but I don't know how to explain with reason
Objective: Implement a sorting algorithm. Description: Implement a radix sort in a Java class named RadixSort.java. Your program should receive its input from a file named "input.txt", which contains one integer per line. It should produce a sorted output file named "output.txt". Include a main method which demonstrates that your algorithm works.
Sorting Sort the following array using the quick sort algorithm: (4 Marks) a. 12 26 8 9 7 0 4 Pivot selection is defined to be the first element of each sub-list. Show the array before and after each quicksort round (when the array is partitioned after placing the pivot at its correct position). Also, clearly highlight the pivot in each partition b. Consider an unsorted array of integers of size n. Write a Java program to arrange the array...
Part 1: Extend your sorting framework with two advanced sorting methods of your choice: (Shell Sort OR Radix Sort) AND (Merge Sort OR Quick Sort). If you choose Shell Sort, experiment with different incremental sequences to see how they affect the algorithm's run time efficiency (count the number of comparisons and exchanges). If you choose to implement Radix Sort, answer the following question as well: Can you write a version of Radix Sort to work with real numbers? If yes,...
. Shell sort is a sorting algorithm similar to insertion sort. Research shell sort and apply that to the following array. Show your work in Detail. [15 points] 45 20 50 10 80 30 60 70 40 90 2. Is Shell sort a stable sorting algorithm? Answer this with an example. [10 points] 3. Apply Merge Sort to sort the following list. Show your work in Detail. [15 Points] 45 20 50 10 80 30 60 70 40 90 4....
8 Sorting Algorithms: Bubble, selection, insertion, quick, merge, bucket, radix, counting. 1. A..Which of the above sorting algorithms does TimSort use? 2. Which of the above algorithms sort a REVERSE ORDER list in O(n2 ) (worst case)? 3. Which of the above algorithms sort a REVERSE ORDER list in O(nlogn) (worst case)? 4. Which of the above algorithms sort an ordered list , a reverse ordered list, and a random list (all three) in 0(nlogn) (worst case)? 5. Which of...
please do by java P14.6 Implement the radix sort algorithm described in Exercise R14.22 (below) to sort arrays of numbers between 0 and 999. P14.7 Implement the radix sort algorithm described in Exercise R14.22 (below) to sort arrays of numbers between 0 and 999. However, use a single auxiliary array, not ten. .P14.8 Implement the radix sort algorithm described in Exercise R14.22 (below) to sort arbitrary int values (positive or negative
Java, Please implement the way the interface specifies. Part A:Radix Sort Implement a radix sort as described in the last section of Chapter 7 in your text. It should handle variable amounts of data and variable numbers of digits in the key values. Use testing to ensure radix sort works for at least three examples of different input sizes and various max digit length. I need to implement the radix sort using the below java interface. /** * <h1><LeastSignificantDigit Radix...
Inal Examination 17. Which of the sorting algorithms listed below has the time fastest best case run (a) Heap sort (b) Merge sort (c) Quick sort (d) Insertion sort 18. Which statement below is false: (a) Quick uick sort and merge sort are divide and conquer algorithte (b) Counting sort is a linear time sorting algorithm. (e) Insertion sort and quicksort have similar best case (d) Generic minimum spanning tree algorithm is 19. Counting sort and radix sort are linked...