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) && .
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
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;
}
}
}
}