SlideShare a Scribd company logo
Please help with this JAVA Assignment and show output if you can please
Complete required scripts based on eclipse and troubleshoot the scripts until the tasks are done.
1. Implement linear-sort, merge-sort and quick-sort (write the code for them).
Solution
Linear or Insertion Sort
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Scanner;
class LinearSort
{
public static void sort( int values[] )
{
int N = values.length;
int i, j, temp;
for (i = 1; i< N; i++)
{
j = i;
temp = values[i];
while (j > 0 && temp < values[j-1])
{
values[j] = values[j-1];
j = j-1;
}
values[j] = temp;
}
}
public static void main(String[] args)
{
Scanner scan = new Scanner( System.in );
int n, i;
System.out.println("Enter number of elements");
n = scan.nextInt();
int values[] = new int[n];
System.out.println(" Enter elements");
for (i = 0; i < n; i++)
values[i] = scan.nextInt();
sort(values);
System.out.println(" Sorted Elements");
for (i = 0; i < n; i++)
System.out.print(values[i]+" ");
System.out.println();
}
}
Output:
Enter number of elements
6
Enter elements
34 56 12 8 9 23
Sorted Elements
8 9 12 23 34 56
Merge Sort
import java.util.Scanner;
public class Mergesort
{
public static void mergesort(int[] a, int low, int high)
{
int N = high - low;
if (N <= 1)
return;
int mid = low + N/2;
mergesort(a, low, mid);
mergesort(a, mid, high);
int[] temp = new int[N];
int i = low, j = mid;
for (int k = 0; k < N; k++)
{
if (i == mid)
temp[k] = a[j++];
else if (j == high)
temp[k] = a[i++];
else if (a[j] pivot)
j--;
if (i <= j)
{
temp = values[i];
values[i] = values[j];
values[j] = temp;
i++;
j--;
}
}
if (low < j)
quickquicksort(values, low, j);
if (i < high)
quickquicksort(values, i, high);
}
public static void main(String[] args)
{
Scanner scan = new Scanner( System.in );
int n, i;
System.out.println("Enter number of elements");
n = scan.nextInt();
int values[] = new int[ n ];
System.out.println(" Enter elements");
for (i = 0; i < n; i++)
values[i] = scan.nextInt();
quicksort(values);
System.out.println(" qSorted Elements ");
for (i = 0; i < n; i++)
System.out.print(values[i]+" ");
System.out.println();
}
}
Output:
Enter number of elements
8
Enter elements
89 23 90 56 13 71 46 13
Sorted Elements
13 13 23 46 56 71 89 90

More Related Content

PDF
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
PPTX
Sorting techniques
PDF
Step 1You need to run the JAVA programs in sections 3.3 and 3.5 for.pdf
PPTX
A Comprehensive Comparative Study and Performance Evaluation
PPT
Sorting algos > Data Structures & Algorithums
PDF
Sorting Algorithms and their implementations
PDF
Merge Sort java with a few details, please include comments if possi.pdf
DOCX
Assignment 2 data structures National University Of Modern Languages
Java AssignmentWrite a program using sortingsorting bubble,sele.pdf
Sorting techniques
Step 1You need to run the JAVA programs in sections 3.3 and 3.5 for.pdf
A Comprehensive Comparative Study and Performance Evaluation
Sorting algos > Data Structures & Algorithums
Sorting Algorithms and their implementations
Merge Sort java with a few details, please include comments if possi.pdf
Assignment 2 data structures National University Of Modern Languages

Similar to Please help with this JAVA Assignment and show output if you can ple.pdf (20)

PDF
Ds sorting
PDF
Merge sort
DOC
Ds program-print
PDF
CS253: Divide & Conquer Sort (2019)
PDF
Refer to my progress on this assignment belowIn this problem you w.pdf
DOCX
DAA Lab File C Programs
PPTX
Sortings .pptx
DOC
Sorting programs
PDF
Please I want a detailed complete answers for each part.Then make.pdf
PDF
Create a menu-driven program that will accept a collection of non-ne.pdf
PPT
Data Structure Sorting
PDF
Quicksort AlgorithmQuicksort is a divide and conquer algorithm. Q.pdf
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
PPTX
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
PPT
quicksort (1).ppt
PDF
Write a program (any language) to randomly generate the following se.pdf
PDF
Quick sort
PDF
Lecture12,13,14.pdf
PPTX
Weak 11-12 Sorting update.pptxbhjiiuuuuu
Ds sorting
Merge sort
Ds program-print
CS253: Divide & Conquer Sort (2019)
Refer to my progress on this assignment belowIn this problem you w.pdf
DAA Lab File C Programs
Sortings .pptx
Sorting programs
Please I want a detailed complete answers for each part.Then make.pdf
Create a menu-driven program that will accept a collection of non-ne.pdf
Data Structure Sorting
Quicksort AlgorithmQuicksort is a divide and conquer algorithm. Q.pdf
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
UNIT V Searching Sorting Hashing Techniques [Autosaved].pptx
quicksort (1).ppt
Write a program (any language) to randomly generate the following se.pdf
Quick sort
Lecture12,13,14.pdf
Weak 11-12 Sorting update.pptxbhjiiuuuuu

More from aroramobiles1 (20)

PDF
ooo T-Mobile LTE 820 PM courses.apexlearning.com Question 34 of 42 M.pdf
PDF
ood evening people. Ive been working on this code that sends a bur.pdf
PDF
Multiply. 0 072(10,000) Multiply. 317.02 middot 0.01 Write the nu.pdf
PDF
Mitochondria and chloroplasts have small genomes becauseQuestion .pdf
PDF
Meta-population theory and island biogeography theory are similar in .pdf
PDF
Java Programpublic class Fraction {   instance variablesin.pdf
PDF
Kim’s revenue one week ago were $251 less than three times Janes rev.pdf
PDF
I am having a hard time with this problem, can you help me #5. .pdf
PDF
How has television influenced the political process, specifically th.pdf
PDF
HISTORY Why is it called American revolutionSolutionThe .pdf
PDF
Explain what #include does in a source codeSolution Th.pdf
PDF
dNdS ratios reflect patterns of genetic divergence that have accumu.pdf
PDF
Discuss the complexity of problem definition and the importance of a.pdf
PDF
Define the following terms i. Hydraulic gradientii. Seepageiii. Cr.pdf
PDF
Describe how the principle of consistency can be applied to interfac.pdf
PDF
Chapter 8 was tough for me. When determining the lumber needs of the.pdf
PDF
{public int idata;data item (key) public double ddata;data item p.pdf
PDF
You have been given a file that contains fields relating to CD infor.pdf
PDF
Write a java method named flipLines that accepts as its parameter a .pdf
PDF
Which of the following isare true regarding router operationA. R.pdf
ooo T-Mobile LTE 820 PM courses.apexlearning.com Question 34 of 42 M.pdf
ood evening people. Ive been working on this code that sends a bur.pdf
Multiply. 0 072(10,000) Multiply. 317.02 middot 0.01 Write the nu.pdf
Mitochondria and chloroplasts have small genomes becauseQuestion .pdf
Meta-population theory and island biogeography theory are similar in .pdf
Java Programpublic class Fraction {   instance variablesin.pdf
Kim’s revenue one week ago were $251 less than three times Janes rev.pdf
I am having a hard time with this problem, can you help me #5. .pdf
How has television influenced the political process, specifically th.pdf
HISTORY Why is it called American revolutionSolutionThe .pdf
Explain what #include does in a source codeSolution Th.pdf
dNdS ratios reflect patterns of genetic divergence that have accumu.pdf
Discuss the complexity of problem definition and the importance of a.pdf
Define the following terms i. Hydraulic gradientii. Seepageiii. Cr.pdf
Describe how the principle of consistency can be applied to interfac.pdf
Chapter 8 was tough for me. When determining the lumber needs of the.pdf
{public int idata;data item (key) public double ddata;data item p.pdf
You have been given a file that contains fields relating to CD infor.pdf
Write a java method named flipLines that accepts as its parameter a .pdf
Which of the following isare true regarding router operationA. R.pdf

Recently uploaded (20)

PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Lesson notes of climatology university.
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
Computing-Curriculum for Schools in Ghana
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Cell Structure & Organelles in detailed.
PDF
Classroom Observation Tools for Teachers
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Microbial disease of the cardiovascular and lymphatic systems
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
FourierSeries-QuestionsWithAnswers(Part-A).pdf
human mycosis Human fungal infections are called human mycosis..pptx
Lesson notes of climatology university.
Microbial diseases, their pathogenesis and prophylaxis
Supply Chain Operations Speaking Notes -ICLT Program
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
Computing-Curriculum for Schools in Ghana
O5-L3 Freight Transport Ops (International) V1.pdf
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Cell Structure & Organelles in detailed.
Classroom Observation Tools for Teachers
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
school management -TNTEU- B.Ed., Semester II Unit 1.pptx

Please help with this JAVA Assignment and show output if you can ple.pdf

  • 1. Please help with this JAVA Assignment and show output if you can please Complete required scripts based on eclipse and troubleshoot the scripts until the tasks are done. 1. Implement linear-sort, merge-sort and quick-sort (write the code for them). Solution Linear or Insertion Sort import java.util.*; import java.lang.*; import java.io.*; import java.util.Scanner; class LinearSort { public static void sort( int values[] ) { int N = values.length; int i, j, temp; for (i = 1; i< N; i++) { j = i; temp = values[i]; while (j > 0 && temp < values[j-1]) { values[j] = values[j-1]; j = j-1; } values[j] = temp; } } public static void main(String[] args) { Scanner scan = new Scanner( System.in ); int n, i; System.out.println("Enter number of elements"); n = scan.nextInt();
  • 2. int values[] = new int[n]; System.out.println(" Enter elements"); for (i = 0; i < n; i++) values[i] = scan.nextInt(); sort(values); System.out.println(" Sorted Elements"); for (i = 0; i < n; i++) System.out.print(values[i]+" "); System.out.println(); } } Output: Enter number of elements 6 Enter elements 34 56 12 8 9 23 Sorted Elements 8 9 12 23 34 56 Merge Sort import java.util.Scanner; public class Mergesort { public static void mergesort(int[] a, int low, int high) { int N = high - low; if (N <= 1) return; int mid = low + N/2; mergesort(a, low, mid); mergesort(a, mid, high); int[] temp = new int[N]; int i = low, j = mid; for (int k = 0; k < N; k++) { if (i == mid) temp[k] = a[j++];
  • 3. else if (j == high) temp[k] = a[i++]; else if (a[j] pivot) j--; if (i <= j) { temp = values[i]; values[i] = values[j]; values[j] = temp; i++; j--; } } if (low < j) quickquicksort(values, low, j); if (i < high) quickquicksort(values, i, high); } public static void main(String[] args) { Scanner scan = new Scanner( System.in ); int n, i; System.out.println("Enter number of elements"); n = scan.nextInt(); int values[] = new int[ n ]; System.out.println(" Enter elements"); for (i = 0; i < n; i++) values[i] = scan.nextInt(); quicksort(values); System.out.println(" qSorted Elements "); for (i = 0; i < n; i++) System.out.print(values[i]+" "); System.out.println(); } } Output:
  • 4. Enter number of elements 8 Enter elements 89 23 90 56 13 71 46 13 Sorted Elements 13 13 23 46 56 71 89 90