SlideShare a Scribd company logo
1 of 6
Download to read offline
Write a program (any language) 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: a) Insertion Sort b)
Merge Sort c) Quick Sort d) Heap Sort Print out the time each algorithm takes to sort the above
numbers
Solution
import java.io.*;
publicclass randomNo{
publicstaticvoid main(String[] args) throws NumberFormatException, IOException {
int n1,n2,ch;
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int count = 0;
do
{
System.out.println(" ");
System.out.println("Menu:");
System.out.println("1.Insertion Sort");
System.out.println("2.Heap Sort.");
System.out.println("3.Quick SOrt");
System.out.println("4.Merge SOrt");
System.out.println("5.Exit");
System.out.println("Enter your choice: ");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("Enter range to generate random numbers--Enter n1 & n2::");
n1=Integer.parseInt(br.readLine());
n2=Integer.parseInt(br.readLine());
int[] arr=newint[n2];
for (int i = 0; i < 10 ; i++)
{
int Random = (n1) + (int)(Math.random()* ((n2-n1)+1));
arr[i]=Random;
System.out.println(Random);
count++;
}
insertionSort(arr,count);
break;
case 2:
System.out.println("Enter range to generate random numbers--Enter n1 & n2::");
n1=Integer.parseInt(br.readLine());
n2=Integer.parseInt(br.readLine());
int[] arr1=newint[20];
for (int i = 0; i < 10 ; i++)
{
int Random = (n1) + (int)(Math.random()* ((n2-n1)+1));
arr1[i]=Random;
System.out.println(Random);
count++;
}
System.out.println(" Before Sorting");
for(int i=0; i1; i--) {
HeapSort(arr1, i - 1);
}
System.out.println(" Ascending Order");
for(int i=0; i=0; i--) {
System.out.print(" " + arr1[i]);
}
break;
case 3:
System.out.println("Enter range to generate random numbers--Enter n1 & n2::");
n1=Integer.parseInt(br.readLine());
n2=Integer.parseInt(br.readLine());
int[] arr3=newint[20];
for (int i = 0; i < 10 ; i++)
{
int Random = (n1) + (int)(Math.random()* ((n2-n1)+1));
arr3[i]=Random;
System.out.println(Random);
count++;
}
QuickSort(arr3, 0, count-1);
System.out.println("  After Sorting");
System.out.println(" Ascending Order");
for(int i=0; i=0; i--) {
System.out.print(" " + arr3[i]);
}
break;
case 4:
System.out.println("Enter range to generate random numbers--Enter n1 & n2::");
n1=Integer.parseInt(br.readLine());
n2=Integer.parseInt(br.readLine());
int[] arr2=newint[20];
for (int i = 0; i < 10 ; i++)
{
int Random = (n1) + (int)(Math.random()* ((n2-n1)+1));
arr2[i]=Random;
System.out.println(Random);
count++;
}
System.out.println(" Before Sorting");
for(int i=0; i=0; i--) {
System.out.print(" " + arr2[i]);
}
break;
case 5:
System.out.println("Invalid");
default:
break;
}
}while(ch!=5);
}
//Insertion Sort
publicstaticvoid insertionSort(int arr[],int n){
System.out.println(" Before Sorting :");
for(int i=0; i 0) && (arr[j-1] > B)) {
arr[j] = arr[j-1];
j--;
}
arr[j] = B;
}
/* Insertion Sort Code End */
System.out.println("  After Sorting");
System.out.println(" Ascending Order :");
for(int i=0; i=0; i--) {
System.out.print(" " + arr[i]);
}
}
//Heap Sort
privatestaticvoid HeapSort(int[] arr, int bound) {
// TODO Auto-generated method stub
int left, right, middle, root, temp;
root = (bound-1) / 2;
for(int i=root; i>=0; i--) {
for(int j=root; j>=0; j--) {
left = (2*j) + 1;
right = (2*j) + 2;
if((left <= bound) && (right <= bound)) {
if(arr[right] >= arr[left])
middle = right;
else
middle = left;
}
else {
if(right > bound)
middle = left;
else
middle = right;
}
if(arr[j] < arr[middle]) {
temp = arr[j];
arr[j] = arr[middle];
arr[middle] = temp;
}
}
}
temp = arr[0];
arr[0] = arr[bound];
arr[bound] = temp;
}
//Merge Sort
privatestaticvoid MergeSort(int[] num, int i, int j) {
int low = i;
int high = j;
if (low >= high) {
return;
}
int middle = (low + high) / 2;
MergeSort(num, low, middle);
MergeSort(num, middle + 1, high);
int end_low = middle;
int start_high = middle + 1;
while ((low <= end_low) && (start_high <= high)) {
if (num[low] < num[start_high]) {
low++;
}
else {
int Temp = num[start_high];
for (int k = start_high- 1; k >= low; k--) {
num[k+1] = num[k];
}
num[low] = Temp;
low ++;
end_low ++;
start_high ++;
}
}
}
privatestaticvoid QuickSort(int[] num, int i, int j) {
int low = i;
int high = j;
if (low >= j) {
return;
}
int mid = num[(low + high) / 2];
while (low < high) {
while (low mid) {
high--;
}
if (low < high) {
int T = num[low];
num[low] = num[high];
num[high] = T;
}
}
}
}

More Related Content

Similar to Write a program (any language) to randomly generate the following se.pdf

Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4Abed Bukhari
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfRahul04August
 
Rootkit on Linux X86 v2.6
Rootkit on Linux X86 v2.6Rootkit on Linux X86 v2.6
Rootkit on Linux X86 v2.6fisher.w.y
 
Please observe the the code and validations of user inputs.import .pdf
Please observe the the code and validations of user inputs.import .pdfPlease observe the the code and validations of user inputs.import .pdf
Please observe the the code and validations of user inputs.import .pdfapexjaipur
 
Rootkit on linux_x86_v2.6
Rootkit on linux_x86_v2.6Rootkit on linux_x86_v2.6
Rootkit on linux_x86_v2.6scuhurricane
 
Review Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdfReview Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdfmayorothenguyenhob69
 
DS LAB RECORD.docx
DS LAB RECORD.docxDS LAB RECORD.docx
DS LAB RECORD.docxdavinci54
 
import java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfimport java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfoptokunal1
 
MagicSquareTest.java import java.util.Scanner;public class Mag.pdf
MagicSquareTest.java import java.util.Scanner;public class Mag.pdfMagicSquareTest.java import java.util.Scanner;public class Mag.pdf
MagicSquareTest.java import java.util.Scanner;public class Mag.pdfanjanacottonmills
 
Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in cgkgaur1987
 
Network lap pgms 7th semester
Network lap pgms 7th semesterNetwork lap pgms 7th semester
Network lap pgms 7th semesterDOSONKA Group
 
Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
 Problem1 java codeimport java.util.Scanner; Java code to pr.pdf Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
Problem1 java codeimport java.util.Scanner; Java code to pr.pdfanupamfootwear
 
Java_Programming_by_Example_6th_Edition.pdf
Java_Programming_by_Example_6th_Edition.pdfJava_Programming_by_Example_6th_Edition.pdf
Java_Programming_by_Example_6th_Edition.pdfJayveeCultivo
 
Computer java programs
Computer java programsComputer java programs
Computer java programsADITYA BHARTI
 
Assignment no39
Assignment no39Assignment no39
Assignment no39Jay Patel
 

Similar to Write a program (any language) to randomly generate the following se.pdf (20)

Whats new in_csharp4
Whats new in_csharp4Whats new in_csharp4
Whats new in_csharp4
 
Java file
Java fileJava file
Java file
 
Java file
Java fileJava file
Java file
 
Java Unit 1 Project
Java Unit 1 ProjectJava Unit 1 Project
Java Unit 1 Project
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdf
 
Rootkit on Linux X86 v2.6
Rootkit on Linux X86 v2.6Rootkit on Linux X86 v2.6
Rootkit on Linux X86 v2.6
 
Please observe the the code and validations of user inputs.import .pdf
Please observe the the code and validations of user inputs.import .pdfPlease observe the the code and validations of user inputs.import .pdf
Please observe the the code and validations of user inputs.import .pdf
 
Rootkit on linux_x86_v2.6
Rootkit on linux_x86_v2.6Rootkit on linux_x86_v2.6
Rootkit on linux_x86_v2.6
 
Review Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdfReview Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdf
 
DS LAB RECORD.docx
DS LAB RECORD.docxDS LAB RECORD.docx
DS LAB RECORD.docx
 
Why Learn Python?
Why Learn Python?Why Learn Python?
Why Learn Python?
 
import java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfimport java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdf
 
MagicSquareTest.java import java.util.Scanner;public class Mag.pdf
MagicSquareTest.java import java.util.Scanner;public class Mag.pdfMagicSquareTest.java import java.util.Scanner;public class Mag.pdf
MagicSquareTest.java import java.util.Scanner;public class Mag.pdf
 
Mouse programming in c
Mouse programming in cMouse programming in c
Mouse programming in c
 
Network lap pgms 7th semester
Network lap pgms 7th semesterNetwork lap pgms 7th semester
Network lap pgms 7th semester
 
Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
 Problem1 java codeimport java.util.Scanner; Java code to pr.pdf Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
 
.net progrmming part2
.net progrmming part2.net progrmming part2
.net progrmming part2
 
Java_Programming_by_Example_6th_Edition.pdf
Java_Programming_by_Example_6th_Edition.pdfJava_Programming_by_Example_6th_Edition.pdf
Java_Programming_by_Example_6th_Edition.pdf
 
Computer java programs
Computer java programsComputer java programs
Computer java programs
 
Assignment no39
Assignment no39Assignment no39
Assignment no39
 

More from archanaemporium

Identify a article about a communicable or noncommunicable disease i.pdf
Identify a article about a communicable or noncommunicable disease i.pdfIdentify a article about a communicable or noncommunicable disease i.pdf
Identify a article about a communicable or noncommunicable disease i.pdfarchanaemporium
 
i safari File Edit View History Bookmarks Window Help 67 D. Thu 101.pdf
i safari File Edit View History Bookmarks Window Help 67 D. Thu 101.pdfi safari File Edit View History Bookmarks Window Help 67 D. Thu 101.pdf
i safari File Edit View History Bookmarks Window Help 67 D. Thu 101.pdfarchanaemporium
 
I dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdfI dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdfarchanaemporium
 
I know that water molecules attract each other, but why do water mol.pdf
I know that water molecules attract each other, but why do water mol.pdfI know that water molecules attract each other, but why do water mol.pdf
I know that water molecules attract each other, but why do water mol.pdfarchanaemporium
 
How can Internet technologies be involved in improving a process in .pdf
How can Internet technologies be involved in improving a process in .pdfHow can Internet technologies be involved in improving a process in .pdf
How can Internet technologies be involved in improving a process in .pdfarchanaemporium
 
How many paths are there in a tree with n nodesHow many paths a.pdf
How many paths are there in a tree with n nodesHow many paths a.pdfHow many paths are there in a tree with n nodesHow many paths a.pdf
How many paths are there in a tree with n nodesHow many paths a.pdfarchanaemporium
 
Hypothesize a way for a virus to evade a host defense & then devise .pdf
Hypothesize a way for a virus to evade a host defense & then devise .pdfHypothesize a way for a virus to evade a host defense & then devise .pdf
Hypothesize a way for a virus to evade a host defense & then devise .pdfarchanaemporium
 
How many ways can letters of the word SINGAPORE be arranged such that.pdf
How many ways can letters of the word SINGAPORE be arranged such that.pdfHow many ways can letters of the word SINGAPORE be arranged such that.pdf
How many ways can letters of the word SINGAPORE be arranged such that.pdfarchanaemporium
 
Haploid. After is complete, the resulting gametes are haploid. This m.pdf
Haploid. After is complete, the resulting gametes are haploid. This m.pdfHaploid. After is complete, the resulting gametes are haploid. This m.pdf
Haploid. After is complete, the resulting gametes are haploid. This m.pdfarchanaemporium
 
FTP and TFTP are primarily file transfer protocols. What is the main.pdf
FTP and TFTP are primarily file transfer protocols. What is the main.pdfFTP and TFTP are primarily file transfer protocols. What is the main.pdf
FTP and TFTP are primarily file transfer protocols. What is the main.pdfarchanaemporium
 
Given Starter Fileimport java.util.Arrays; Encapsulates.pdf
Given Starter Fileimport java.util.Arrays;   Encapsulates.pdfGiven Starter Fileimport java.util.Arrays;   Encapsulates.pdf
Given Starter Fileimport java.util.Arrays; Encapsulates.pdfarchanaemporium
 
Express the verbal representation for the function f symbolically. M.pdf
Express the verbal representation for the function f symbolically.  M.pdfExpress the verbal representation for the function f symbolically.  M.pdf
Express the verbal representation for the function f symbolically. M.pdfarchanaemporium
 
Economic resources have a price above zero because...A. there are .pdf
Economic resources have a price above zero because...A. there are .pdfEconomic resources have a price above zero because...A. there are .pdf
Economic resources have a price above zero because...A. there are .pdfarchanaemporium
 
DOES NOT NEED TO BE ANSWERED UNTIL NOV 13thWords AssignmentRober.pdf
DOES NOT NEED TO BE ANSWERED UNTIL NOV 13thWords AssignmentRober.pdfDOES NOT NEED TO BE ANSWERED UNTIL NOV 13thWords AssignmentRober.pdf
DOES NOT NEED TO BE ANSWERED UNTIL NOV 13thWords AssignmentRober.pdfarchanaemporium
 
Discuss in detail how two different Progressive reformers tackled th.pdf
Discuss in detail how two different Progressive reformers tackled th.pdfDiscuss in detail how two different Progressive reformers tackled th.pdf
Discuss in detail how two different Progressive reformers tackled th.pdfarchanaemporium
 
Describe the major parts of the nervous system and their functions..pdf
Describe the major parts of the nervous system and their functions..pdfDescribe the major parts of the nervous system and their functions..pdf
Describe the major parts of the nervous system and their functions..pdfarchanaemporium
 
DEFINE gene enhancer and gene promoter (3-4 sentences each).Sol.pdf
DEFINE gene enhancer and gene promoter (3-4 sentences each).Sol.pdfDEFINE gene enhancer and gene promoter (3-4 sentences each).Sol.pdf
DEFINE gene enhancer and gene promoter (3-4 sentences each).Sol.pdfarchanaemporium
 
Cisco Systems, Inc. offers a switching technology known as Multi-Lay.pdf
Cisco Systems, Inc. offers a switching technology known as Multi-Lay.pdfCisco Systems, Inc. offers a switching technology known as Multi-Lay.pdf
Cisco Systems, Inc. offers a switching technology known as Multi-Lay.pdfarchanaemporium
 
C# ProgramingHow a derived class that inherits attributes and beha.pdf
C# ProgramingHow a derived class that inherits attributes and beha.pdfC# ProgramingHow a derived class that inherits attributes and beha.pdf
C# ProgramingHow a derived class that inherits attributes and beha.pdfarchanaemporium
 
Assume that x and y are already defined as being of type int . Write.pdf
Assume that x and y are already defined as being of type int . Write.pdfAssume that x and y are already defined as being of type int . Write.pdf
Assume that x and y are already defined as being of type int . Write.pdfarchanaemporium
 

More from archanaemporium (20)

Identify a article about a communicable or noncommunicable disease i.pdf
Identify a article about a communicable or noncommunicable disease i.pdfIdentify a article about a communicable or noncommunicable disease i.pdf
Identify a article about a communicable or noncommunicable disease i.pdf
 
i safari File Edit View History Bookmarks Window Help 67 D. Thu 101.pdf
i safari File Edit View History Bookmarks Window Help 67 D. Thu 101.pdfi safari File Edit View History Bookmarks Window Help 67 D. Thu 101.pdf
i safari File Edit View History Bookmarks Window Help 67 D. Thu 101.pdf
 
I dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdfI dont know what is wrong with this roulette program I cant seem.pdf
I dont know what is wrong with this roulette program I cant seem.pdf
 
I know that water molecules attract each other, but why do water mol.pdf
I know that water molecules attract each other, but why do water mol.pdfI know that water molecules attract each other, but why do water mol.pdf
I know that water molecules attract each other, but why do water mol.pdf
 
How can Internet technologies be involved in improving a process in .pdf
How can Internet technologies be involved in improving a process in .pdfHow can Internet technologies be involved in improving a process in .pdf
How can Internet technologies be involved in improving a process in .pdf
 
How many paths are there in a tree with n nodesHow many paths a.pdf
How many paths are there in a tree with n nodesHow many paths a.pdfHow many paths are there in a tree with n nodesHow many paths a.pdf
How many paths are there in a tree with n nodesHow many paths a.pdf
 
Hypothesize a way for a virus to evade a host defense & then devise .pdf
Hypothesize a way for a virus to evade a host defense & then devise .pdfHypothesize a way for a virus to evade a host defense & then devise .pdf
Hypothesize a way for a virus to evade a host defense & then devise .pdf
 
How many ways can letters of the word SINGAPORE be arranged such that.pdf
How many ways can letters of the word SINGAPORE be arranged such that.pdfHow many ways can letters of the word SINGAPORE be arranged such that.pdf
How many ways can letters of the word SINGAPORE be arranged such that.pdf
 
Haploid. After is complete, the resulting gametes are haploid. This m.pdf
Haploid. After is complete, the resulting gametes are haploid. This m.pdfHaploid. After is complete, the resulting gametes are haploid. This m.pdf
Haploid. After is complete, the resulting gametes are haploid. This m.pdf
 
FTP and TFTP are primarily file transfer protocols. What is the main.pdf
FTP and TFTP are primarily file transfer protocols. What is the main.pdfFTP and TFTP are primarily file transfer protocols. What is the main.pdf
FTP and TFTP are primarily file transfer protocols. What is the main.pdf
 
Given Starter Fileimport java.util.Arrays; Encapsulates.pdf
Given Starter Fileimport java.util.Arrays;   Encapsulates.pdfGiven Starter Fileimport java.util.Arrays;   Encapsulates.pdf
Given Starter Fileimport java.util.Arrays; Encapsulates.pdf
 
Express the verbal representation for the function f symbolically. M.pdf
Express the verbal representation for the function f symbolically.  M.pdfExpress the verbal representation for the function f symbolically.  M.pdf
Express the verbal representation for the function f symbolically. M.pdf
 
Economic resources have a price above zero because...A. there are .pdf
Economic resources have a price above zero because...A. there are .pdfEconomic resources have a price above zero because...A. there are .pdf
Economic resources have a price above zero because...A. there are .pdf
 
DOES NOT NEED TO BE ANSWERED UNTIL NOV 13thWords AssignmentRober.pdf
DOES NOT NEED TO BE ANSWERED UNTIL NOV 13thWords AssignmentRober.pdfDOES NOT NEED TO BE ANSWERED UNTIL NOV 13thWords AssignmentRober.pdf
DOES NOT NEED TO BE ANSWERED UNTIL NOV 13thWords AssignmentRober.pdf
 
Discuss in detail how two different Progressive reformers tackled th.pdf
Discuss in detail how two different Progressive reformers tackled th.pdfDiscuss in detail how two different Progressive reformers tackled th.pdf
Discuss in detail how two different Progressive reformers tackled th.pdf
 
Describe the major parts of the nervous system and their functions..pdf
Describe the major parts of the nervous system and their functions..pdfDescribe the major parts of the nervous system and their functions..pdf
Describe the major parts of the nervous system and their functions..pdf
 
DEFINE gene enhancer and gene promoter (3-4 sentences each).Sol.pdf
DEFINE gene enhancer and gene promoter (3-4 sentences each).Sol.pdfDEFINE gene enhancer and gene promoter (3-4 sentences each).Sol.pdf
DEFINE gene enhancer and gene promoter (3-4 sentences each).Sol.pdf
 
Cisco Systems, Inc. offers a switching technology known as Multi-Lay.pdf
Cisco Systems, Inc. offers a switching technology known as Multi-Lay.pdfCisco Systems, Inc. offers a switching technology known as Multi-Lay.pdf
Cisco Systems, Inc. offers a switching technology known as Multi-Lay.pdf
 
C# ProgramingHow a derived class that inherits attributes and beha.pdf
C# ProgramingHow a derived class that inherits attributes and beha.pdfC# ProgramingHow a derived class that inherits attributes and beha.pdf
C# ProgramingHow a derived class that inherits attributes and beha.pdf
 
Assume that x and y are already defined as being of type int . Write.pdf
Assume that x and y are already defined as being of type int . Write.pdfAssume that x and y are already defined as being of type int . Write.pdf
Assume that x and y are already defined as being of type int . Write.pdf
 

Recently uploaded

9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
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 SDThiyagu K
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
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.pptxheathfieldcps1
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityGeoBlogs
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAssociation for Project Management
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...fonyou31
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 

Recently uploaded (20)

9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
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
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
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
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
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"
 
APM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across SectorsAPM Welcome, APM North West Network Conference, Synergies Across Sectors
APM Welcome, APM North West Network Conference, Synergies Across Sectors
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
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"
 
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
 
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
Ecosystem Interactions Class Discussion Presentation in Blue Green Lined Styl...
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 

Write a program (any language) to randomly generate the following se.pdf

  • 1. Write a program (any language) 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: a) Insertion Sort b) Merge Sort c) Quick Sort d) Heap Sort Print out the time each algorithm takes to sort the above numbers Solution import java.io.*; publicclass randomNo{ publicstaticvoid main(String[] args) throws NumberFormatException, IOException { int n1,n2,ch; BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); int count = 0; do { System.out.println(" "); System.out.println("Menu:"); System.out.println("1.Insertion Sort"); System.out.println("2.Heap Sort."); System.out.println("3.Quick SOrt"); System.out.println("4.Merge SOrt"); System.out.println("5.Exit"); System.out.println("Enter your choice: "); ch=Integer.parseInt(br.readLine()); switch(ch) { case 1: System.out.println("Enter range to generate random numbers--Enter n1 & n2::"); n1=Integer.parseInt(br.readLine()); n2=Integer.parseInt(br.readLine()); int[] arr=newint[n2]; for (int i = 0; i < 10 ; i++) { int Random = (n1) + (int)(Math.random()* ((n2-n1)+1));
  • 2. arr[i]=Random; System.out.println(Random); count++; } insertionSort(arr,count); break; case 2: System.out.println("Enter range to generate random numbers--Enter n1 & n2::"); n1=Integer.parseInt(br.readLine()); n2=Integer.parseInt(br.readLine()); int[] arr1=newint[20]; for (int i = 0; i < 10 ; i++) { int Random = (n1) + (int)(Math.random()* ((n2-n1)+1)); arr1[i]=Random; System.out.println(Random); count++; } System.out.println(" Before Sorting"); for(int i=0; i1; i--) { HeapSort(arr1, i - 1); } System.out.println(" Ascending Order"); for(int i=0; i=0; i--) { System.out.print(" " + arr1[i]); } break; case 3: System.out.println("Enter range to generate random numbers--Enter n1 & n2::"); n1=Integer.parseInt(br.readLine()); n2=Integer.parseInt(br.readLine()); int[] arr3=newint[20]; for (int i = 0; i < 10 ; i++) { int Random = (n1) + (int)(Math.random()* ((n2-n1)+1)); arr3[i]=Random;
  • 3. System.out.println(Random); count++; } QuickSort(arr3, 0, count-1); System.out.println(" After Sorting"); System.out.println(" Ascending Order"); for(int i=0; i=0; i--) { System.out.print(" " + arr3[i]); } break; case 4: System.out.println("Enter range to generate random numbers--Enter n1 & n2::"); n1=Integer.parseInt(br.readLine()); n2=Integer.parseInt(br.readLine()); int[] arr2=newint[20]; for (int i = 0; i < 10 ; i++) { int Random = (n1) + (int)(Math.random()* ((n2-n1)+1)); arr2[i]=Random; System.out.println(Random); count++; } System.out.println(" Before Sorting"); for(int i=0; i=0; i--) { System.out.print(" " + arr2[i]); } break; case 5: System.out.println("Invalid"); default: break; } }while(ch!=5); } //Insertion Sort
  • 4. publicstaticvoid insertionSort(int arr[],int n){ System.out.println(" Before Sorting :"); for(int i=0; i 0) && (arr[j-1] > B)) { arr[j] = arr[j-1]; j--; } arr[j] = B; } /* Insertion Sort Code End */ System.out.println(" After Sorting"); System.out.println(" Ascending Order :"); for(int i=0; i=0; i--) { System.out.print(" " + arr[i]); } } //Heap Sort privatestaticvoid HeapSort(int[] arr, int bound) { // TODO Auto-generated method stub int left, right, middle, root, temp; root = (bound-1) / 2; for(int i=root; i>=0; i--) { for(int j=root; j>=0; j--) { left = (2*j) + 1; right = (2*j) + 2; if((left <= bound) && (right <= bound)) { if(arr[right] >= arr[left]) middle = right; else middle = left; } else { if(right > bound) middle = left; else middle = right; }
  • 5. if(arr[j] < arr[middle]) { temp = arr[j]; arr[j] = arr[middle]; arr[middle] = temp; } } } temp = arr[0]; arr[0] = arr[bound]; arr[bound] = temp; } //Merge Sort privatestaticvoid MergeSort(int[] num, int i, int j) { int low = i; int high = j; if (low >= high) { return; } int middle = (low + high) / 2; MergeSort(num, low, middle); MergeSort(num, middle + 1, high); int end_low = middle; int start_high = middle + 1; while ((low <= end_low) && (start_high <= high)) { if (num[low] < num[start_high]) { low++; } else { int Temp = num[start_high]; for (int k = start_high- 1; k >= low; k--) { num[k+1] = num[k]; } num[low] = Temp; low ++; end_low ++; start_high ++;
  • 6. } } } privatestaticvoid QuickSort(int[] num, int i, int j) { int low = i; int high = j; if (low >= j) { return; } int mid = num[(low + high) / 2]; while (low < high) { while (low mid) { high--; } if (low < high) { int T = num[low]; num[low] = num[high]; num[high] = T; } } } }