SlideShare a Scribd company logo
1 of 4
Download to read offline
Merge Sort java with a few details, please include comments if possible Generate an array with
100 random integers between 0 and 1000 Print the original array to the screen Use your merge
sort algorithm to sort the array from lowest to highest Print the sorted array to the screen
Solution
/* Class name MergeSortRandomNumber.java */
import java.util.Random;
public class MergeSortRandomNumber{
private int[] array;
private int[] tempMergArr;
private int length;
public static void main(String[] args) {
int inputArr[] =new int[10];// Storing array elements default 10 we can modify depends on
requirement
Random r = new Random();//Create Object for Random Predefined class
System.out.println("Random Numbers");//Printing Random Numbers
for(int i=0;i<10;i++)
{
int Low = 0;//Start from 0
int High = 100;//Up to 100
int Result = r.nextInt(High-Low) + Low;
System.out.print(Result);//Prining on the console random numbers
System.out.print(" ");
inputArr[i]=Result;
}
System.out.println("");
System.out.println("Input Array");//Printing input array on the console start
for(int i=0;i<10;i++)
{
System.out.print(inputArr[i]);//
System.out.print(" ");
} //Printing input array on the console end
System.out.println("");
System.out.println("After Merge sort from low to high");
//merge Sort Main logic started
RandomNumber mms = new RandomNumber();
mms.sort(inputArr);//Passing our input array calling sort() method
for(int i:inputArr){
System.out.print(i);//printing Final values from low to high
System.out.print(" ");
}
}
public void sort(int inputArr[]) {
this.array = inputArr;
this.length = inputArr.length;
this.tempMergArr = new int[length];
doMergeSort(0, length - 1);//Calling Merge sort main logic
}
private void doMergeSort(int lowerIndex, int higherIndex) {
if (lowerIndex < higherIndex) {
int middle = lowerIndex + (higherIndex - lowerIndex) / 2;
// Below step sorts the left side of the array
doMergeSort(lowerIndex, middle);
// Below step sorts the right side of the array
doMergeSort(middle + 1, higherIndex);
// Now merge both sides
mergeParts(lowerIndex, middle, higherIndex);
}
}
private void mergeParts(int lowerIndex, int middle, int higherIndex) {
//Compare elements at First and Second,
//and move smaller element at Merged array
for (int i = lowerIndex; i <= higherIndex; i++) {
tempMergArr[i] = array[i];
}
int i = lowerIndex;
int j = middle + 1;
int k = lowerIndex;
while (i <= middle && j <= higherIndex) {
if (tempMergArr[i] <= tempMergArr[j]) {
array[k] = tempMergArr[i];
i++;
} else {
array[k] = tempMergArr[j];
j++;
}
k++;
}
while (i <= middle) {
array[k] = tempMergArr[i];
k++;
i++;
}
}
}
=============================
Expected Out put
Random Numbers
19 95 97 32 86 36 48 34 46 71
Input Array
19 95 97 32 86 36 48 34 46 71
After Merge sort from low to high
19 32 34 36 46 48 71 86 95 97

More Related Content

Similar to Merge Sort java with a few details, please include comments if possi.pdf

I need help with the 2nd TODO comment and the other comments Ill.pdf
I need help with the 2nd TODO comment and the other comments Ill.pdfI need help with the 2nd TODO comment and the other comments Ill.pdf
I need help with the 2nd TODO comment and the other comments Ill.pdfEye2eyeopticians10
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfravikapoorindia
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxamrit47
 
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdf
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdfPart 1)#include stdio.h #include stdlib.h #include pthrea.pdf
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdfmohammadirfan136964
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory QA Auotmation Java programs,theory
QA Auotmation Java programs,theory archana singh
 
Here is what I got so far, I dont know how to write union, interse.pdf
Here is what I got so far, I dont know how to write union, interse.pdfHere is what I got so far, I dont know how to write union, interse.pdf
Here is what I got so far, I dont know how to write union, interse.pdfarihantpatna
 
Below is the assignment description and the file I have written..pdf
Below is the assignment description and the file I have written..pdfBelow is the assignment description and the file I have written..pdf
Below is the assignment description and the file I have written..pdfinfo673628
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfyamew16788
 
Required to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docxRequired to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docxdebishakespeare
 
Step 1 Implement the getSortedRunLength() methodImplement the get.pdf
Step 1 Implement the getSortedRunLength() methodImplement the get.pdfStep 1 Implement the getSortedRunLength() methodImplement the get.pdf
Step 1 Implement the getSortedRunLength() methodImplement the get.pdfaloeplusint
 
Chapter 7.1
Chapter 7.1Chapter 7.1
Chapter 7.1sotlsoc
 

Similar to Merge Sort java with a few details, please include comments if possi.pdf (15)

I need help with the 2nd TODO comment and the other comments Ill.pdf
I need help with the 2nd TODO comment and the other comments Ill.pdfI need help with the 2nd TODO comment and the other comments Ill.pdf
I need help with the 2nd TODO comment and the other comments Ill.pdf
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdf
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
 
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdf
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdfPart 1)#include stdio.h #include stdlib.h #include pthrea.pdf
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdf
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
 
Chap1 array
Chap1 arrayChap1 array
Chap1 array
 
Hw3
Hw3Hw3
Hw3
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory QA Auotmation Java programs,theory
QA Auotmation Java programs,theory
 
Here is what I got so far, I dont know how to write union, interse.pdf
Here is what I got so far, I dont know how to write union, interse.pdfHere is what I got so far, I dont know how to write union, interse.pdf
Here is what I got so far, I dont know how to write union, interse.pdf
 
SlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdfSlideSet_4_Arraysnew.pdf
SlideSet_4_Arraysnew.pdf
 
Below is the assignment description and the file I have written..pdf
Below is the assignment description and the file I have written..pdfBelow is the assignment description and the file I have written..pdf
Below is the assignment description and the file I have written..pdf
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
 
Required to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docxRequired to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docx
 
Step 1 Implement the getSortedRunLength() methodImplement the get.pdf
Step 1 Implement the getSortedRunLength() methodImplement the get.pdfStep 1 Implement the getSortedRunLength() methodImplement the get.pdf
Step 1 Implement the getSortedRunLength() methodImplement the get.pdf
 
Chapter 7.1
Chapter 7.1Chapter 7.1
Chapter 7.1
 

More from feelinggifts

Chapter 22. Problem 3BCP0 Bookmark Show all steps ON Problem Eminent.pdf
Chapter 22. Problem 3BCP0 Bookmark Show all steps ON Problem Eminent.pdfChapter 22. Problem 3BCP0 Bookmark Show all steps ON Problem Eminent.pdf
Chapter 22. Problem 3BCP0 Bookmark Show all steps ON Problem Eminent.pdffeelinggifts
 
D Question 11 1 pts A fundamental feature of money that BitCoin fulfi.pdf
D Question 11 1 pts A fundamental feature of money that BitCoin fulfi.pdfD Question 11 1 pts A fundamental feature of money that BitCoin fulfi.pdf
D Question 11 1 pts A fundamental feature of money that BitCoin fulfi.pdffeelinggifts
 
connect instructions I helg dule 7 Connect seve & Exit ..pdf
connect instructions I helg dule 7 Connect seve & Exit ..pdfconnect instructions I helg dule 7 Connect seve & Exit ..pdf
connect instructions I helg dule 7 Connect seve & Exit ..pdffeelinggifts
 
COMPUTER ETHICSDiscuss the local and global impact of malware and .pdf
COMPUTER ETHICSDiscuss the local and global impact of malware and .pdfCOMPUTER ETHICSDiscuss the local and global impact of malware and .pdf
COMPUTER ETHICSDiscuss the local and global impact of malware and .pdffeelinggifts
 
You’re helping a group of ethnographers analyze some oral history da.pdf
You’re helping a group of ethnographers analyze some oral history da.pdfYou’re helping a group of ethnographers analyze some oral history da.pdf
You’re helping a group of ethnographers analyze some oral history da.pdffeelinggifts
 
A man with type B blood type claims to be biological father of a typ.pdf
A man with type B blood type claims to be biological father of a typ.pdfA man with type B blood type claims to be biological father of a typ.pdf
A man with type B blood type claims to be biological father of a typ.pdffeelinggifts
 
why should filtrate be used to transfer the residue when the desired.pdf
why should filtrate be used to transfer the residue when the desired.pdfwhy should filtrate be used to transfer the residue when the desired.pdf
why should filtrate be used to transfer the residue when the desired.pdffeelinggifts
 
Winch of the following is NOT one of the reasons an IO Exception is t.pdf
Winch of the following is NOT one of the reasons an IO Exception is t.pdfWinch of the following is NOT one of the reasons an IO Exception is t.pdf
Winch of the following is NOT one of the reasons an IO Exception is t.pdffeelinggifts
 
Why is attending to the full scope of drivers critical in implementi.pdf
Why is attending to the full scope of drivers critical in implementi.pdfWhy is attending to the full scope of drivers critical in implementi.pdf
Why is attending to the full scope of drivers critical in implementi.pdffeelinggifts
 
Which of the following is not a function of interest groups A. to i.pdf
Which of the following is not a function of interest groups A. to i.pdfWhich of the following is not a function of interest groups A. to i.pdf
Which of the following is not a function of interest groups A. to i.pdffeelinggifts
 
When explaining civil law one would say that it is concemed with disp.pdf
When explaining civil law one would say that it is concemed with disp.pdfWhen explaining civil law one would say that it is concemed with disp.pdf
When explaining civil law one would say that it is concemed with disp.pdffeelinggifts
 
What is the difference between a short and long branchSolution.pdf
What is the difference between a short and long branchSolution.pdfWhat is the difference between a short and long branchSolution.pdf
What is the difference between a short and long branchSolution.pdffeelinggifts
 
What is the maximum file size in a FAT32 systemSolutionFirst .pdf
What is the maximum file size in a FAT32 systemSolutionFirst .pdfWhat is the maximum file size in a FAT32 systemSolutionFirst .pdf
What is the maximum file size in a FAT32 systemSolutionFirst .pdffeelinggifts
 
View transaction list Journal entry worksheet The company paid $510 c.pdf
View transaction list Journal entry worksheet The company paid $510 c.pdfView transaction list Journal entry worksheet The company paid $510 c.pdf
View transaction list Journal entry worksheet The company paid $510 c.pdffeelinggifts
 
The numeric data types in C++ can be broken into two general categori.pdf
The numeric data types in C++ can be broken into two general categori.pdfThe numeric data types in C++ can be broken into two general categori.pdf
The numeric data types in C++ can be broken into two general categori.pdffeelinggifts
 
The capillaries at the alveoli Form a respiratory membrane Are spe.pdf
The capillaries at the alveoli  Form a respiratory membrane  Are spe.pdfThe capillaries at the alveoli  Form a respiratory membrane  Are spe.pdf
The capillaries at the alveoli Form a respiratory membrane Are spe.pdffeelinggifts
 
Select the word or phrase which best matches the description of it..pdf
Select the word or phrase which best matches the description of it..pdfSelect the word or phrase which best matches the description of it..pdf
Select the word or phrase which best matches the description of it..pdffeelinggifts
 
Robin Hood has hired you as his new Strategic Consultant to help him.pdf
Robin Hood has hired you as his new Strategic Consultant to help him.pdfRobin Hood has hired you as his new Strategic Consultant to help him.pdf
Robin Hood has hired you as his new Strategic Consultant to help him.pdffeelinggifts
 
Research indicates that online news seekers tend to access topics of.pdf
Research indicates that online news seekers tend to access topics of.pdfResearch indicates that online news seekers tend to access topics of.pdf
Research indicates that online news seekers tend to access topics of.pdffeelinggifts
 
Read Case Study Room 406 and answer the following questions in 2.pdf
Read Case Study Room 406 and answer the following questions in 2.pdfRead Case Study Room 406 and answer the following questions in 2.pdf
Read Case Study Room 406 and answer the following questions in 2.pdffeelinggifts
 

More from feelinggifts (20)

Chapter 22. Problem 3BCP0 Bookmark Show all steps ON Problem Eminent.pdf
Chapter 22. Problem 3BCP0 Bookmark Show all steps ON Problem Eminent.pdfChapter 22. Problem 3BCP0 Bookmark Show all steps ON Problem Eminent.pdf
Chapter 22. Problem 3BCP0 Bookmark Show all steps ON Problem Eminent.pdf
 
D Question 11 1 pts A fundamental feature of money that BitCoin fulfi.pdf
D Question 11 1 pts A fundamental feature of money that BitCoin fulfi.pdfD Question 11 1 pts A fundamental feature of money that BitCoin fulfi.pdf
D Question 11 1 pts A fundamental feature of money that BitCoin fulfi.pdf
 
connect instructions I helg dule 7 Connect seve & Exit ..pdf
connect instructions I helg dule 7 Connect seve & Exit ..pdfconnect instructions I helg dule 7 Connect seve & Exit ..pdf
connect instructions I helg dule 7 Connect seve & Exit ..pdf
 
COMPUTER ETHICSDiscuss the local and global impact of malware and .pdf
COMPUTER ETHICSDiscuss the local and global impact of malware and .pdfCOMPUTER ETHICSDiscuss the local and global impact of malware and .pdf
COMPUTER ETHICSDiscuss the local and global impact of malware and .pdf
 
You’re helping a group of ethnographers analyze some oral history da.pdf
You’re helping a group of ethnographers analyze some oral history da.pdfYou’re helping a group of ethnographers analyze some oral history da.pdf
You’re helping a group of ethnographers analyze some oral history da.pdf
 
A man with type B blood type claims to be biological father of a typ.pdf
A man with type B blood type claims to be biological father of a typ.pdfA man with type B blood type claims to be biological father of a typ.pdf
A man with type B blood type claims to be biological father of a typ.pdf
 
why should filtrate be used to transfer the residue when the desired.pdf
why should filtrate be used to transfer the residue when the desired.pdfwhy should filtrate be used to transfer the residue when the desired.pdf
why should filtrate be used to transfer the residue when the desired.pdf
 
Winch of the following is NOT one of the reasons an IO Exception is t.pdf
Winch of the following is NOT one of the reasons an IO Exception is t.pdfWinch of the following is NOT one of the reasons an IO Exception is t.pdf
Winch of the following is NOT one of the reasons an IO Exception is t.pdf
 
Why is attending to the full scope of drivers critical in implementi.pdf
Why is attending to the full scope of drivers critical in implementi.pdfWhy is attending to the full scope of drivers critical in implementi.pdf
Why is attending to the full scope of drivers critical in implementi.pdf
 
Which of the following is not a function of interest groups A. to i.pdf
Which of the following is not a function of interest groups A. to i.pdfWhich of the following is not a function of interest groups A. to i.pdf
Which of the following is not a function of interest groups A. to i.pdf
 
When explaining civil law one would say that it is concemed with disp.pdf
When explaining civil law one would say that it is concemed with disp.pdfWhen explaining civil law one would say that it is concemed with disp.pdf
When explaining civil law one would say that it is concemed with disp.pdf
 
What is the difference between a short and long branchSolution.pdf
What is the difference between a short and long branchSolution.pdfWhat is the difference between a short and long branchSolution.pdf
What is the difference between a short and long branchSolution.pdf
 
What is the maximum file size in a FAT32 systemSolutionFirst .pdf
What is the maximum file size in a FAT32 systemSolutionFirst .pdfWhat is the maximum file size in a FAT32 systemSolutionFirst .pdf
What is the maximum file size in a FAT32 systemSolutionFirst .pdf
 
View transaction list Journal entry worksheet The company paid $510 c.pdf
View transaction list Journal entry worksheet The company paid $510 c.pdfView transaction list Journal entry worksheet The company paid $510 c.pdf
View transaction list Journal entry worksheet The company paid $510 c.pdf
 
The numeric data types in C++ can be broken into two general categori.pdf
The numeric data types in C++ can be broken into two general categori.pdfThe numeric data types in C++ can be broken into two general categori.pdf
The numeric data types in C++ can be broken into two general categori.pdf
 
The capillaries at the alveoli Form a respiratory membrane Are spe.pdf
The capillaries at the alveoli  Form a respiratory membrane  Are spe.pdfThe capillaries at the alveoli  Form a respiratory membrane  Are spe.pdf
The capillaries at the alveoli Form a respiratory membrane Are spe.pdf
 
Select the word or phrase which best matches the description of it..pdf
Select the word or phrase which best matches the description of it..pdfSelect the word or phrase which best matches the description of it..pdf
Select the word or phrase which best matches the description of it..pdf
 
Robin Hood has hired you as his new Strategic Consultant to help him.pdf
Robin Hood has hired you as his new Strategic Consultant to help him.pdfRobin Hood has hired you as his new Strategic Consultant to help him.pdf
Robin Hood has hired you as his new Strategic Consultant to help him.pdf
 
Research indicates that online news seekers tend to access topics of.pdf
Research indicates that online news seekers tend to access topics of.pdfResearch indicates that online news seekers tend to access topics of.pdf
Research indicates that online news seekers tend to access topics of.pdf
 
Read Case Study Room 406 and answer the following questions in 2.pdf
Read Case Study Room 406 and answer the following questions in 2.pdfRead Case Study Room 406 and answer the following questions in 2.pdf
Read Case Study Room 406 and answer the following questions in 2.pdf
 

Recently uploaded

When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...Gary Wood
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesPooky Knightsmith
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaEADTU
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17Celine George
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptNishitharanjan Rout
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismDabee Kamal
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnershipsexpandedwebsite
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMELOISARIVERA8
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxLimon Prince
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...Nguyen Thanh Tu Collection
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...Nguyen Thanh Tu Collection
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptxPoojaSen20
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjMohammed Sikander
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxCeline George
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxAdelaideRefugio
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFVivekanand Anglo Vedic Academy
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................MirzaAbrarBaig5
 

Recently uploaded (20)

Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...When Quality Assurance Meets Innovation in Higher Education - Report launch w...
When Quality Assurance Meets Innovation in Higher Education - Report launch w...
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes GuàrdiaPersonalisation of Education by AI and Big Data - Lourdes Guàrdia
Personalisation of Education by AI and Big Data - Lourdes Guàrdia
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
AIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.pptAIM of Education-Teachers Training-2024.ppt
AIM of Education-Teachers Training-2024.ppt
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
An overview of the various scriptures in Hinduism
An overview of the various scriptures in HinduismAn overview of the various scriptures in Hinduism
An overview of the various scriptures in Hinduism
 
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community PartnershipsSpring gala 2024 photo slideshow - Celebrating School-Community Partnerships
Spring gala 2024 photo slideshow - Celebrating School-Community Partnerships
 
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUMDEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
DEMONSTRATION LESSON IN ENGLISH 4 MATATAG CURRICULUM
 
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptxAnalyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
Analyzing and resolving a communication crisis in Dhaka textiles LTD.pptx
 
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
TỔNG HỢP HƠN 100 ĐỀ THI THỬ TỐT NGHIỆP THPT TOÁN 2024 - TỪ CÁC TRƯỜNG, TRƯỜNG...
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
MOOD STABLIZERS DRUGS.pptx
MOOD     STABLIZERS           DRUGS.pptxMOOD     STABLIZERS           DRUGS.pptx
MOOD STABLIZERS DRUGS.pptx
 
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjjStl Algorithms in C++ jjjjjjjjjjjjjjjjjj
Stl Algorithms in C++ jjjjjjjjjjjjjjjjjj
 
How to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptxHow to Manage Website in Odoo 17 Studio App.pptx
How to Manage Website in Odoo 17 Studio App.pptx
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
The Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDFThe Story of Village Palampur Class 9 Free Study Material PDF
The Story of Village Palampur Class 9 Free Study Material PDF
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 

Merge Sort java with a few details, please include comments if possi.pdf

  • 1. Merge Sort java with a few details, please include comments if possible Generate an array with 100 random integers between 0 and 1000 Print the original array to the screen Use your merge sort algorithm to sort the array from lowest to highest Print the sorted array to the screen Solution /* Class name MergeSortRandomNumber.java */ import java.util.Random; public class MergeSortRandomNumber{ private int[] array; private int[] tempMergArr; private int length; public static void main(String[] args) { int inputArr[] =new int[10];// Storing array elements default 10 we can modify depends on requirement Random r = new Random();//Create Object for Random Predefined class System.out.println("Random Numbers");//Printing Random Numbers for(int i=0;i<10;i++) { int Low = 0;//Start from 0 int High = 100;//Up to 100 int Result = r.nextInt(High-Low) + Low; System.out.print(Result);//Prining on the console random numbers System.out.print(" "); inputArr[i]=Result; } System.out.println(""); System.out.println("Input Array");//Printing input array on the console start for(int i=0;i<10;i++) { System.out.print(inputArr[i]);//
  • 2. System.out.print(" "); } //Printing input array on the console end System.out.println(""); System.out.println("After Merge sort from low to high"); //merge Sort Main logic started RandomNumber mms = new RandomNumber(); mms.sort(inputArr);//Passing our input array calling sort() method for(int i:inputArr){ System.out.print(i);//printing Final values from low to high System.out.print(" "); } } public void sort(int inputArr[]) { this.array = inputArr; this.length = inputArr.length; this.tempMergArr = new int[length]; doMergeSort(0, length - 1);//Calling Merge sort main logic } private void doMergeSort(int lowerIndex, int higherIndex) { if (lowerIndex < higherIndex) { int middle = lowerIndex + (higherIndex - lowerIndex) / 2; // Below step sorts the left side of the array doMergeSort(lowerIndex, middle); // Below step sorts the right side of the array doMergeSort(middle + 1, higherIndex); // Now merge both sides mergeParts(lowerIndex, middle, higherIndex); } }
  • 3. private void mergeParts(int lowerIndex, int middle, int higherIndex) { //Compare elements at First and Second, //and move smaller element at Merged array for (int i = lowerIndex; i <= higherIndex; i++) { tempMergArr[i] = array[i]; } int i = lowerIndex; int j = middle + 1; int k = lowerIndex; while (i <= middle && j <= higherIndex) { if (tempMergArr[i] <= tempMergArr[j]) { array[k] = tempMergArr[i]; i++; } else { array[k] = tempMergArr[j]; j++; } k++; } while (i <= middle) { array[k] = tempMergArr[i]; k++; i++; } } } ============================= Expected Out put Random Numbers 19 95 97 32 86 36 48 34 46 71 Input Array 19 95 97 32 86 36 48 34 46 71
  • 4. After Merge sort from low to high 19 32 34 36 46 48 71 86 95 97