SlideShare a Scribd company logo
1 of 8
Download to read offline
Write a java program to randomly generate the following sets of data:
1.) 10 numbers
2.) 1,000 numbers
3.) 100,000 numbers
4.) 1,000,000 numbers
5.) 10,000,000 numbers
Your program must sort the above sets of numbers using the following algorithms:
1.) Insertion Sort
2.) Merge Sort
3.) Quick Sort
4.) Heap Sort
Print out the time each algorithm takes to sort the above numbers
Solution
import java.util.Random;
public class Sort {
private int[] array;
private int size ;
public Sort(int n){
array = new int[n];
size = n;
Random rand = new Random();
for (int i = 0 ; i < n ; i++ ) {
array[i] = rand.nextInt(100000) ;
}
}
public void print(){
for (int i = 0; i < size ; i++ ) {
System.out.print(array[i] + " ");
}
System.out.println("");
}
void insertion_sort()
{
for (int i=1; i=0 && array[j] > key)
{
array[j+1] = array[j];
j = j-1;
}
array[j+1] = key;
}
}
void merge(int arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
int L[] = new int [n1];
int R[] = new int [n2];
/*Copy data to temp arrays*/
for (int i=0; i Array to be sorted,
low --> Starting index,
high --> Ending index */
void quick_sort(int low, int high)
{
if (low < high)
{
/* pi is partitioning index, arr[pi] is
now at right place */
int pi = partition(array, low, high);
// Recursively sort elements before
// partition and after partition
quick_sort(low, pi-1);
quick_sort(pi+1, high);
}
}
public void heap_sort()
{
int n = array.length;
// Build heap (rearrange array)
for (int i = n / 2 - 1; i >= 0; i--)
heapify(array, n, i);
// One by one extract an element from heap
for (int i=n-1; i>=0; i--)
{
// Move current root to end
int temp = array[0];
array[0] = array[i];
array[i] = temp;
// call max heapify on the reduced heap
heapify(array, i, 0);
}
}
// To heapify a subtree rooted with node i which is
// an index in arr[]. n is size of heap
void heapify(int arr[], 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 left child is larger than root
if (l < n && arr[l] > arr[largest])
largest = l;
// If right child is larger than largest so far
if (r < n && arr[r] > arr[largest])
largest = r;
// If largest is not root
if (largest != i)
{
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
// Recursively heapify the affected sub-tree
heapify(arr, n, largest);
}
}
public static void main(String[] args) {
Sort sort1 = new Sort(10);
long start = System.currentTimeMillis();
sort1.insertion_sort();
long time = System.currentTimeMillis() - start;
//sort1.print();
System.out.println("insertion_sort for 10 numbers take " + time + " ms ");
sort1 = new Sort(10);
start = System.currentTimeMillis();
sort1.merge_sort(0,9);
time = System.currentTimeMillis() - start;
//sort1.print();
System.out.println("merge_sort for 10 numbers take " + time + " ms ");
sort1 = new Sort(10);
start = System.currentTimeMillis();
sort1.quick_sort(0,9);
time = System.currentTimeMillis() - start;
//sort1.print();
System.out.println("quick_sort for 10 numbers take " + time + " ms ");
sort1 = new Sort(10);
start = System.currentTimeMillis();
sort1.heap_sort();
time = System.currentTimeMillis() - start;
//sort1.print();
System.out.println("heap_sort for 10 numbers take " + time + " ms ");
////////////////////////////////////////////////////////////////////////
System.out.println("");System.out.println("");
sort1 = new Sort(1000);
start = System.currentTimeMillis();
sort1.insertion_sort();
time = System.currentTimeMillis() - start;
System.out.println("insertion_sort for 1000 numbers take " + time + " ms ");
sort1 = new Sort(1000);
start = System.currentTimeMillis();
sort1.merge_sort(0,9);
time = System.currentTimeMillis() - start;
System.out.println("merge_sort for 1000 numbers take " + time + " ms ");
sort1 = new Sort(1000);
start = System.currentTimeMillis();
sort1.quick_sort(0,9);
time = System.currentTimeMillis() - start;
System.out.println("quick_sort for 1000 numbers take " + time + " ms ");
sort1 = new Sort(1000);
start = System.currentTimeMillis();
sort1.heap_sort();
time = System.currentTimeMillis() - start;
System.out.println("heap_sort for 1000 numbers take " + time + " ms ");
////////////////////////////////////////////////////////////////////////
System.out.println("");System.out.println("");
sort1 = new Sort(100000);
start = System.currentTimeMillis();
sort1.insertion_sort();
time = System.currentTimeMillis() - start;
System.out.println("insertion_sort for 100000 numbers take " + time + " ms ");
sort1 = new Sort(100000);
start = System.currentTimeMillis();
sort1.merge_sort(0,9);
time = System.currentTimeMillis() - start;
System.out.println("merge_sort for 100000 numbers take " + time + " ms ");
sort1 = new Sort(100000);
start = System.currentTimeMillis();
sort1.quick_sort(0,9);
time = System.currentTimeMillis() - start;
System.out.println("quick_sort for 100000 numbers take " + time + " ms ");
sort1 = new Sort(100000);
start = System.currentTimeMillis();
sort1.heap_sort();
time = System.currentTimeMillis() - start;
System.out.println("heap sort for 100000 numbers take " + time + " ms ");
////////////////////////////////////////////////////////////////////////
System.out.println("");System.out.println("");
sort1 = new Sort(1000000);
start = System.currentTimeMillis();
sort1.insertion_sort();
time = System.currentTimeMillis() - start;
System.out.println("insertion_sort for 1000000 numbers take " + time + " ms ");
sort1 = new Sort(1000000);
start = System.currentTimeMillis();
sort1.merge_sort(0,9);
time = System.currentTimeMillis() - start;
System.out.println("merge_sort for 1000000 numbers take " + time + " ms ");
sort1 = new Sort(1000000);
start = System.currentTimeMillis();
sort1.quick_sort(0,9);
time = System.currentTimeMillis() - start;
System.out.println("quick_sort for 1000000 numbers take " + time + " ms ");
sort1 = new Sort(1000000);
start = System.currentTimeMillis();
sort1.heap_sort();
time = System.currentTimeMillis() - start;
System.out.println("heap_sort for 1000000 numbers take " + time + " ms ");
////////////////////////////////////////////////////////////////////////
System.out.println("");System.out.println("");
sort1 = new Sort(10000000);
start = System.currentTimeMillis();
sort1.insertion_sort();
time = System.currentTimeMillis() - start;
System.out.println("insertion_sort for 10000000 numbers take " + time + " ms ");
sort1 = new Sort(10000000);
start = System.currentTimeMillis();
sort1.merge_sort(0,9);
time = System.currentTimeMillis() - start;
System.out.println("merge_sort for 10000000 numbers take " + time + " ms ");
sort1 = new Sort(10000000);
start = System.currentTimeMillis();
sort1.quick_sort(0,9);
time = System.currentTimeMillis() - start;
System.out.println("quick_sort for 10000000 numbers take " + time + " ms ");
sort1 = new Sort(10000000);
start = System.currentTimeMillis();
sort1.heap_sort();
time = System.currentTimeMillis() - start;
}
}
Sample Output
insertion_sort for 10 numbers take 0 ms
merge_sort for 10 numbers take 0 ms
quick_sort for 10 numbers take 0 ms
heap_sort for 10 numbers take 0 ms
insertion_sort for 1000 numbers take 5 ms
merge_sort for 1000 numbers take 0 ms
quick_sort for 1000 numbers take 0 ms
heap_sort for 1000 numbers take 6 ms
insertion_sort for 100000 numbers take 1239 ms
merge_sort for 100000 numbers take 0 ms
quick_sort for 100000 numbers take 0 ms
heap sort for 100000 numbers take 21 ms
insertion_sort for 1000000 numbers take 217971 ms
merge_sort for 1000000 numbers take 0 ms
quick_sort for 1000000 numbers take 0 ms
heap_sort for 1000000 numbers take 261 ms

More Related Content

Similar to Write a java program to randomly generate the following sets of data.pdf

import java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdf
import java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdfimport java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdf
import java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdf
adhityalapcare
 
please help finish sorting methods- import java-util-Arrays- import ja.pdf
please help finish sorting methods- import java-util-Arrays- import ja.pdfplease help finish sorting methods- import java-util-Arrays- import ja.pdf
please help finish sorting methods- import java-util-Arrays- import ja.pdf
anfenterprises
 
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
Sunil Kumar Gunasekaran
 
Merge Sort java with a few details, please include comments if possi.pdf
Merge Sort java with a few details, please include comments if possi.pdfMerge Sort java with a few details, please include comments if possi.pdf
Merge Sort java with a few details, please include comments if possi.pdf
feelinggifts
 
The purpose of this C++ programming project is to allow the student .pdf
The purpose of this C++ programming project is to allow the student .pdfThe purpose of this C++ programming project is to allow the student .pdf
The purpose of this C++ programming project is to allow the student .pdf
Rahul04August
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
Abed Bukhari
 
Create a menu-driven program that will accept a collection of non-ne.pdf
Create a menu-driven program that will accept a collection of non-ne.pdfCreate a menu-driven program that will accept a collection of non-ne.pdf
Create a menu-driven program that will accept a collection of non-ne.pdf
rajeshjangid1865
 
To write a program that implements the following C++ concepts 1. Dat.pdf
To write a program that implements the following C++ concepts 1. Dat.pdfTo write a program that implements the following C++ concepts 1. Dat.pdf
To write a program that implements the following C++ concepts 1. Dat.pdf
SANDEEPARIHANT
 
The solution is as belowEmployeeDemo.javaimport java.util.Scann.pdf
The solution is as belowEmployeeDemo.javaimport java.util.Scann.pdfThe solution is as belowEmployeeDemo.javaimport java.util.Scann.pdf
The solution is as belowEmployeeDemo.javaimport java.util.Scann.pdf
aparnatiwari291
 
Can you give an example of a binary heap programCan you give an .pdf
Can you give an example of a binary heap programCan you give an .pdfCan you give an example of a binary heap programCan you give an .pdf
Can you give an example of a binary heap programCan you give an .pdf
arorasales234
 

Similar to Write a java program to randomly generate the following sets of data.pdf (13)

import java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdf
import java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdfimport java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdf
import java-util-Arrays- import java-io-PrintWriter- import java-io-Fi.pdf
 
please help finish sorting methods- import java-util-Arrays- import ja.pdf
please help finish sorting methods- import java-util-Arrays- import ja.pdfplease help finish sorting methods- import java-util-Arrays- import ja.pdf
please help finish sorting methods- import java-util-Arrays- import ja.pdf
 
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
 
Merge Sort java with a few details, please include comments if possi.pdf
Merge Sort java with a few details, please include comments if possi.pdfMerge Sort java with a few details, please include comments if possi.pdf
Merge Sort java with a few details, please include comments if possi.pdf
 
ch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.pptch05-program-logic-indefinite-loops.ppt
ch05-program-logic-indefinite-loops.ppt
 
The purpose of this C++ programming project is to allow the student .pdf
The purpose of this C++ programming project is to allow the student .pdfThe purpose of this C++ programming project is to allow the student .pdf
The purpose of this C++ programming project is to allow the student .pdf
 
Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
 
Create a menu-driven program that will accept a collection of non-ne.pdf
Create a menu-driven program that will accept a collection of non-ne.pdfCreate a menu-driven program that will accept a collection of non-ne.pdf
Create a menu-driven program that will accept a collection of non-ne.pdf
 
QA Auotmation Java programs,theory
QA Auotmation Java programs,theory QA Auotmation Java programs,theory
QA Auotmation Java programs,theory
 
To write a program that implements the following C++ concepts 1. Dat.pdf
To write a program that implements the following C++ concepts 1. Dat.pdfTo write a program that implements the following C++ concepts 1. Dat.pdf
To write a program that implements the following C++ concepts 1. Dat.pdf
 
How to tune a query - ODTUG 2012
How to tune a query - ODTUG 2012How to tune a query - ODTUG 2012
How to tune a query - ODTUG 2012
 
The solution is as belowEmployeeDemo.javaimport java.util.Scann.pdf
The solution is as belowEmployeeDemo.javaimport java.util.Scann.pdfThe solution is as belowEmployeeDemo.javaimport java.util.Scann.pdf
The solution is as belowEmployeeDemo.javaimport java.util.Scann.pdf
 
Can you give an example of a binary heap programCan you give an .pdf
Can you give an example of a binary heap programCan you give an .pdfCan you give an example of a binary heap programCan you give an .pdf
Can you give an example of a binary heap programCan you give an .pdf
 

More from arshin9

Discuss the impact that the tonnage of sulfur or sulfuric acid recov.pdf
Discuss the impact that the tonnage of sulfur or sulfuric acid recov.pdfDiscuss the impact that the tonnage of sulfur or sulfuric acid recov.pdf
Discuss the impact that the tonnage of sulfur or sulfuric acid recov.pdf
arshin9
 
You have decided to go to graduate school for Molecular Biology (yay!.pdf
You have decided to go to graduate school for Molecular Biology (yay!.pdfYou have decided to go to graduate school for Molecular Biology (yay!.pdf
You have decided to go to graduate school for Molecular Biology (yay!.pdf
arshin9
 
What is the between facial action unit and facial landmark differenc.pdf
What is the between facial action unit and facial landmark differenc.pdfWhat is the between facial action unit and facial landmark differenc.pdf
What is the between facial action unit and facial landmark differenc.pdf
arshin9
 
Significance of Discoveries in Genetics and DNA Our understandin.pdf
Significance of Discoveries in Genetics and DNA Our understandin.pdfSignificance of Discoveries in Genetics and DNA Our understandin.pdf
Significance of Discoveries in Genetics and DNA Our understandin.pdf
arshin9
 
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
arshin9
 

More from arshin9 (20)

Bats rely on sound localization to be able to fly during the night. O.pdf
Bats rely on sound localization to be able to fly during the night. O.pdfBats rely on sound localization to be able to fly during the night. O.pdf
Bats rely on sound localization to be able to fly during the night. O.pdf
 
A layer of dirt must be spread over a circular area 13.5 feet in dia.pdf
A layer of dirt must be spread over a circular area 13.5 feet in dia.pdfA layer of dirt must be spread over a circular area 13.5 feet in dia.pdf
A layer of dirt must be spread over a circular area 13.5 feet in dia.pdf
 
Discuss the impact that the tonnage of sulfur or sulfuric acid recov.pdf
Discuss the impact that the tonnage of sulfur or sulfuric acid recov.pdfDiscuss the impact that the tonnage of sulfur or sulfuric acid recov.pdf
Discuss the impact that the tonnage of sulfur or sulfuric acid recov.pdf
 
You have decided to go to graduate school for Molecular Biology (yay!.pdf
You have decided to go to graduate school for Molecular Biology (yay!.pdfYou have decided to go to graduate school for Molecular Biology (yay!.pdf
You have decided to go to graduate school for Molecular Biology (yay!.pdf
 
write the HTML to associate a web page with an external style sheet .pdf
write the HTML to associate a web page with an external style sheet .pdfwrite the HTML to associate a web page with an external style sheet .pdf
write the HTML to associate a web page with an external style sheet .pdf
 
Why was any carbon-containing molecule originally called organic.pdf
Why was any carbon-containing molecule originally called organic.pdfWhy was any carbon-containing molecule originally called organic.pdf
Why was any carbon-containing molecule originally called organic.pdf
 
Which of the following is true about indexes and index design a. in.pdf
Which of the following is true about indexes and index design  a. in.pdfWhich of the following is true about indexes and index design  a. in.pdf
Which of the following is true about indexes and index design a. in.pdf
 
What kind of organisms likely perished due to the increase of oxygen .pdf
What kind of organisms likely perished due to the increase of oxygen .pdfWhat kind of organisms likely perished due to the increase of oxygen .pdf
What kind of organisms likely perished due to the increase of oxygen .pdf
 
what is the HTTP protocol used for What are its major parts.pdf
what is the HTTP protocol used for What are its major parts.pdfwhat is the HTTP protocol used for What are its major parts.pdf
what is the HTTP protocol used for What are its major parts.pdf
 
What is the between facial action unit and facial landmark differenc.pdf
What is the between facial action unit and facial landmark differenc.pdfWhat is the between facial action unit and facial landmark differenc.pdf
What is the between facial action unit and facial landmark differenc.pdf
 
What are contemporary management challenges and opportunitiesSo.pdf
What are contemporary management challenges and opportunitiesSo.pdfWhat are contemporary management challenges and opportunitiesSo.pdf
What are contemporary management challenges and opportunitiesSo.pdf
 
Valinomycin is a potassium ionophore. What would be its effect on in.pdf
Valinomycin is a potassium ionophore. What would be its effect on in.pdfValinomycin is a potassium ionophore. What would be its effect on in.pdf
Valinomycin is a potassium ionophore. What would be its effect on in.pdf
 
The retrovirus genome is made of the nucleic acid before the virus be.pdf
The retrovirus genome is made of the nucleic acid before the virus be.pdfThe retrovirus genome is made of the nucleic acid before the virus be.pdf
The retrovirus genome is made of the nucleic acid before the virus be.pdf
 
the probability that it is raining is .25. the orbability that it is.pdf
the probability that it is raining is .25. the orbability that it is.pdfthe probability that it is raining is .25. the orbability that it is.pdf
the probability that it is raining is .25. the orbability that it is.pdf
 
2)In presentation software, you can use a(n) ____ to add movement to.pdf
2)In presentation software, you can use a(n) ____ to add movement to.pdf2)In presentation software, you can use a(n) ____ to add movement to.pdf
2)In presentation software, you can use a(n) ____ to add movement to.pdf
 
Significance of Discoveries in Genetics and DNA Our understandin.pdf
Significance of Discoveries in Genetics and DNA Our understandin.pdfSignificance of Discoveries in Genetics and DNA Our understandin.pdf
Significance of Discoveries in Genetics and DNA Our understandin.pdf
 
Scope is much less complicated if functions cannot contain other func.pdf
Scope is much less complicated if functions cannot contain other func.pdfScope is much less complicated if functions cannot contain other func.pdf
Scope is much less complicated if functions cannot contain other func.pdf
 
Refer to the drawings in Figure 13.2 of a single pair of homologous c.pdf
Refer to the drawings in Figure 13.2 of a single pair of homologous c.pdfRefer to the drawings in Figure 13.2 of a single pair of homologous c.pdf
Refer to the drawings in Figure 13.2 of a single pair of homologous c.pdf
 
Prions are viruses that cause proteins to misfold. Select one True.pdf
Prions are viruses that cause proteins to misfold.  Select one  True.pdfPrions are viruses that cause proteins to misfold.  Select one  True.pdf
Prions are viruses that cause proteins to misfold. Select one True.pdf
 
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf2.(Sorted list array implementation)This sorted list ADT discussed .pdf
2.(Sorted list array implementation)This sorted list ADT discussed .pdf
 

Recently uploaded

Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
heathfieldcps1
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 

Recently uploaded (20)

Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
SECOND SEMESTER TOPIC COVERAGE SY 2023-2024 Trends, Networks, and Critical Th...
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 

Write a java program to randomly generate the following sets of data.pdf

  • 1. Write a java program to randomly generate the following sets of data: 1.) 10 numbers 2.) 1,000 numbers 3.) 100,000 numbers 4.) 1,000,000 numbers 5.) 10,000,000 numbers Your program must sort the above sets of numbers using the following algorithms: 1.) Insertion Sort 2.) Merge Sort 3.) Quick Sort 4.) Heap Sort Print out the time each algorithm takes to sort the above numbers Solution import java.util.Random; public class Sort { private int[] array; private int size ; public Sort(int n){ array = new int[n]; size = n; Random rand = new Random(); for (int i = 0 ; i < n ; i++ ) { array[i] = rand.nextInt(100000) ; } } public void print(){ for (int i = 0; i < size ; i++ ) { System.out.print(array[i] + " "); } System.out.println("");
  • 2. } void insertion_sort() { for (int i=1; i=0 && array[j] > key) { array[j+1] = array[j]; j = j-1; } array[j+1] = key; } } void merge(int arr[], int l, int m, int r) { // Find sizes of two subarrays to be merged int n1 = m - l + 1; int n2 = r - m; /* Create temp arrays */ int L[] = new int [n1]; int R[] = new int [n2]; /*Copy data to temp arrays*/ for (int i=0; i Array to be sorted, low --> Starting index, high --> Ending index */ void quick_sort(int low, int high) { if (low < high) { /* pi is partitioning index, arr[pi] is now at right place */ int pi = partition(array, low, high); // Recursively sort elements before // partition and after partition
  • 3. quick_sort(low, pi-1); quick_sort(pi+1, high); } } public void heap_sort() { int n = array.length; // Build heap (rearrange array) for (int i = n / 2 - 1; i >= 0; i--) heapify(array, n, i); // One by one extract an element from heap for (int i=n-1; i>=0; i--) { // Move current root to end int temp = array[0]; array[0] = array[i]; array[i] = temp; // call max heapify on the reduced heap heapify(array, i, 0); } } // To heapify a subtree rooted with node i which is // an index in arr[]. n is size of heap void heapify(int arr[], 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 left child is larger than root if (l < n && arr[l] > arr[largest])
  • 4. largest = l; // If right child is larger than largest so far if (r < n && arr[r] > arr[largest]) largest = r; // If largest is not root if (largest != i) { int swap = arr[i]; arr[i] = arr[largest]; arr[largest] = swap; // Recursively heapify the affected sub-tree heapify(arr, n, largest); } } public static void main(String[] args) { Sort sort1 = new Sort(10); long start = System.currentTimeMillis(); sort1.insertion_sort(); long time = System.currentTimeMillis() - start; //sort1.print(); System.out.println("insertion_sort for 10 numbers take " + time + " ms "); sort1 = new Sort(10); start = System.currentTimeMillis(); sort1.merge_sort(0,9); time = System.currentTimeMillis() - start; //sort1.print(); System.out.println("merge_sort for 10 numbers take " + time + " ms "); sort1 = new Sort(10); start = System.currentTimeMillis(); sort1.quick_sort(0,9); time = System.currentTimeMillis() - start; //sort1.print();
  • 5. System.out.println("quick_sort for 10 numbers take " + time + " ms "); sort1 = new Sort(10); start = System.currentTimeMillis(); sort1.heap_sort(); time = System.currentTimeMillis() - start; //sort1.print(); System.out.println("heap_sort for 10 numbers take " + time + " ms "); //////////////////////////////////////////////////////////////////////// System.out.println("");System.out.println(""); sort1 = new Sort(1000); start = System.currentTimeMillis(); sort1.insertion_sort(); time = System.currentTimeMillis() - start; System.out.println("insertion_sort for 1000 numbers take " + time + " ms "); sort1 = new Sort(1000); start = System.currentTimeMillis(); sort1.merge_sort(0,9); time = System.currentTimeMillis() - start; System.out.println("merge_sort for 1000 numbers take " + time + " ms "); sort1 = new Sort(1000); start = System.currentTimeMillis(); sort1.quick_sort(0,9); time = System.currentTimeMillis() - start; System.out.println("quick_sort for 1000 numbers take " + time + " ms "); sort1 = new Sort(1000); start = System.currentTimeMillis(); sort1.heap_sort(); time = System.currentTimeMillis() - start; System.out.println("heap_sort for 1000 numbers take " + time + " ms ");
  • 6. //////////////////////////////////////////////////////////////////////// System.out.println("");System.out.println(""); sort1 = new Sort(100000); start = System.currentTimeMillis(); sort1.insertion_sort(); time = System.currentTimeMillis() - start; System.out.println("insertion_sort for 100000 numbers take " + time + " ms "); sort1 = new Sort(100000); start = System.currentTimeMillis(); sort1.merge_sort(0,9); time = System.currentTimeMillis() - start; System.out.println("merge_sort for 100000 numbers take " + time + " ms "); sort1 = new Sort(100000); start = System.currentTimeMillis(); sort1.quick_sort(0,9); time = System.currentTimeMillis() - start; System.out.println("quick_sort for 100000 numbers take " + time + " ms "); sort1 = new Sort(100000); start = System.currentTimeMillis(); sort1.heap_sort(); time = System.currentTimeMillis() - start; System.out.println("heap sort for 100000 numbers take " + time + " ms "); //////////////////////////////////////////////////////////////////////// System.out.println("");System.out.println(""); sort1 = new Sort(1000000); start = System.currentTimeMillis(); sort1.insertion_sort(); time = System.currentTimeMillis() - start;
  • 7. System.out.println("insertion_sort for 1000000 numbers take " + time + " ms "); sort1 = new Sort(1000000); start = System.currentTimeMillis(); sort1.merge_sort(0,9); time = System.currentTimeMillis() - start; System.out.println("merge_sort for 1000000 numbers take " + time + " ms "); sort1 = new Sort(1000000); start = System.currentTimeMillis(); sort1.quick_sort(0,9); time = System.currentTimeMillis() - start; System.out.println("quick_sort for 1000000 numbers take " + time + " ms "); sort1 = new Sort(1000000); start = System.currentTimeMillis(); sort1.heap_sort(); time = System.currentTimeMillis() - start; System.out.println("heap_sort for 1000000 numbers take " + time + " ms "); //////////////////////////////////////////////////////////////////////// System.out.println("");System.out.println(""); sort1 = new Sort(10000000); start = System.currentTimeMillis(); sort1.insertion_sort(); time = System.currentTimeMillis() - start; System.out.println("insertion_sort for 10000000 numbers take " + time + " ms "); sort1 = new Sort(10000000); start = System.currentTimeMillis(); sort1.merge_sort(0,9); time = System.currentTimeMillis() - start; System.out.println("merge_sort for 10000000 numbers take " + time + " ms "); sort1 = new Sort(10000000);
  • 8. start = System.currentTimeMillis(); sort1.quick_sort(0,9); time = System.currentTimeMillis() - start; System.out.println("quick_sort for 10000000 numbers take " + time + " ms "); sort1 = new Sort(10000000); start = System.currentTimeMillis(); sort1.heap_sort(); time = System.currentTimeMillis() - start; } } Sample Output insertion_sort for 10 numbers take 0 ms merge_sort for 10 numbers take 0 ms quick_sort for 10 numbers take 0 ms heap_sort for 10 numbers take 0 ms insertion_sort for 1000 numbers take 5 ms merge_sort for 1000 numbers take 0 ms quick_sort for 1000 numbers take 0 ms heap_sort for 1000 numbers take 6 ms insertion_sort for 100000 numbers take 1239 ms merge_sort for 100000 numbers take 0 ms quick_sort for 100000 numbers take 0 ms heap sort for 100000 numbers take 21 ms insertion_sort for 1000000 numbers take 217971 ms merge_sort for 1000000 numbers take 0 ms quick_sort for 1000000 numbers take 0 ms heap_sort for 1000000 numbers take 261 ms