SlideShare a Scribd company logo
Problem 1: Show the comparison of runtime of linear search and binary search using line chart
and table. Execute both algorithms 6 times on same data(use random integer generators), where
input data size are: 50000, 100000, 150000, 200000, 250000, and 300000. Please report worst
case runtimes so that the search item is not found in the input data.
Problem 2: Show the comparison of runtime of bubble sort and merge sort using line chart and
table. Execute both algorithms 6 times on same data(use random integer generators), where data
size are: 50000, 100000, 150000, 200000, 250000, and 300000. In each execution, sort the data
in ascending order.
Solution
Search & sort comparison for differnt sort & search algorithms.
Problem1: Java Code:
import java.util.Random;
public class SearchComparison {
public static void main(String[] args) {
//Compare linear & binary search for
//random data 6 times & print its execution time
System.out.println("--------------------------------");
//50000
searchComparison(50000);
System.out.println("--------------------------------");
//100000
searchComparison(100000);
System.out.println("--------------------------------");
//150000
searchComparison(150000);
System.out.println("--------------------------------");
//200000
searchComparison(200000);
System.out.println("--------------------------------");
//250000
searchComparison(250000);
System.out.println("--------------------------------");
//and 300000
searchComparison(300000);
System.out.println("--------------------------------");
}
/**
* searchComparison
* @param size
*/
public static void searchComparison(int size){
System.out.println("Input size:"+size);
//Generate random numbers list for given size
Random randomGenerator = new Random();
int list[]=new int[size];
for (int index = 0; index < size; ++index){
list[index]=randomGenerator.nextInt(size);
}
//For worst case key must be last number list[size-1]
long startTime=System.nanoTime();
linearSearch(list, list[size-1]);
long endTime=System.nanoTime();
long totalTime=(endTime-startTime); //time in nanoseconds
System.out.println("Linear search: input size="+size+" - time="+totalTime);
startTime=System.nanoTime();
binarySearch(list, list[size-1]);
endTime=System.nanoTime();
totalTime=(endTime-startTime); //time in milliseconds
System.out.println("Binary search: input size="+size+" - time="+totalTime);
}
/**
* Linearly search key in list[]. If key is present then return the index,
* otherwise return -1
*
* @param list
* @param key
* @return
*/
public static int linearSearch(int list[], int key) {
for (int index = 0; index < list.length; index++) {
if (list[index] == key)
return index;
}
return -1;
}
/**
* BinarySearch search key in list[]. If key is present then return the
* index, otherwise return -1
*
* @param list
* @param key
* @return
*/
public static int binarySearch(int list[], int key) {
int lo = 0;
int hi = list.length - 1;
while (lo <= hi) {
// Key is in list[lo..hi] or not present.
int mid = lo + (hi - lo) / 2;
if (key < list[mid])
hi = mid - 1;
else if (key > list[mid])
lo = mid + 1;
else
return mid;
}
return -1;
}
}
Steps to compile & run the program:
javac SearchComparison.java
java SearchComparison
Output:
--------------------------------
Input size:50000
Linear search: input size=50000 - time=2124844
Binary search: input size=50000 - time=16627
--------------------------------
Input size:100000
Linear search: input size=100000 - time=340572
Binary search: input size=100000 - time=18489
--------------------------------
Input size:150000
Linear search: input size=150000 - time=3344970
Binary search: input size=150000 - time=4584
--------------------------------
Input size:200000
Linear search: input size=200000 - time=668294
Binary search: input size=200000 - time=4557
--------------------------------
Input size:250000
Linear search: input size=250000 - time=77522
Binary search: input size=250000 - time=19226
--------------------------------
Input size:300000
Linear search: input size=300000 - time=164177
Binary search: input size=300000 - time=3978
--------------------------------
Problem 2: Java Code-
public class SortComparison {
public static void main(String[] args) {
// Compare linear & binary search for
// random data 6 times & print its execution time
System.out.println("--------------------------------");
// 50000
sortComparison(50000);
System.out.println("--------------------------------");
// 100000
sortComparison(100000);
System.out.println("--------------------------------");
// 150000
sortComparison(150000);
System.out.println("--------------------------------");
// 200000
sortComparison(200000);
System.out.println("--------------------------------");
// 250000
sortComparison(250000);
System.out.println("--------------------------------");
// and 300000
sortComparison(300000);
System.out.println("--------------------------------");
}
public static void sortComparison(int size) {
System.out.println("Input size:" + size);
// Generate random numbers list for given size
Random randomGenerator = new Random();
int list[] = new int[size];
for (int index = 0; index < size; ++index) {
list[index] = randomGenerator.nextInt(size);
}
long startTime = System.nanoTime();
bubbleSort(list);
long endTime = System.nanoTime();
long totalTime = (endTime - startTime); // time in nanoseconds
System.out.println("Bubble sort: input size=" + size + " - time=" + totalTime);
startTime = System.nanoTime();
mergesort(list);
endTime = System.nanoTime();
totalTime = (endTime - startTime); // time in milliseconds
System.out.println("Merge sort: input size=" + size + " - time=" + totalTime);
}
/**
* Method use bubble sort algorithm to sort list ascending order
*
* @param list
*/
public static void bubbleSort(int list[]) {
int temp = 0;
for (int index1 = 0; index1 < (list.length - 1); index1++) {
for (int index2 = 0; index2 < list.length - index1 - 1; index2++) {
if (list[index2] > list[index2 + 1]) {
temp = list[index2];
list[index2] = list[index2 + 1];
list[index2 + 1] = temp;
}
}
}
}
/**
* Method use merge sort algorithm to sort list ascending order
*
* @param list
* @return
*/
public static int[] mergesort(int[] list) {
int size = list.length;
if (size <= 1)
return list;
int[] temp1 = new int[size / 2];
int[] temp2 = new int[size - size / 2];
for (int index = 0; index < temp1.length; index++)
temp1[index] = list[index];
for (int index = 0; index < temp2.length; index++)
temp2[index] = list[index + size / 2];
return merge(mergesort(temp1), mergesort(temp2));
}
private static int[] merge(int[] temp1, int[] temp2) {
int[] temp3 = new int[temp1.length + temp2.length];
int index1 = 0, index2 = 0;
for (int index3 = 0; index3 < temp3.length; index3++) {
if (index1 >= temp1.length)
temp3[index3] = temp2[index2++];
else if (index2 >= temp2.length)
temp3[index3] = temp1[index1++];
else if (temp1[index1] <= temp2[index2])
temp3[index3] = temp1[index1++];
else
temp3[index3] = temp2[index2++];
}
return temp3;
}
}
Steps to compile & run the program:
javac SortComparison.java
java SortComparison
Output:
--------------------------------
Input size:50000
Bubble sort: input size=50000 - time=6442721760
Merge sort: input size=50000 - time=33285535
--------------------------------
Input size:100000
Bubble sort: input size=100000 - time=25841668982
Merge sort: input size=100000 - time=24046898
--------------------------------
Input size:150000
Bubble sort: input size=150000 - time=58446233927
Merge sort: input size=150000 - time=29973618
--------------------------------
Input size:200000
Bubble sort: input size=200000 - time=104861524269
Merge sort: input size=200000 - time=39586920
--------------------------------
Input size:250000
Bubble sort: input size=250000 - time=160468980476
Merge sort: input size=250000 - time=45671299
--------------------------------
Input size:300000
Bubble sort: input size=300000 - time=229789831510
Merge sort: input size=300000 - time=54097226
--------------------------------
Please draw line chart & table using above output data of search & sort comparison for differnt
sort & search algorithms. Let me know in case any query in comment section.
Ad

More Related Content

Similar to Problem 1 Show the comparison of runtime of linear search and binar.pdf (20)

Below is a given ArrayList class and Main class Your Dreams Our Mission/tuto...
Below is a given ArrayList class and Main class  Your Dreams Our Mission/tuto...Below is a given ArrayList class and Main class  Your Dreams Our Mission/tuto...
Below is a given ArrayList class and Main class Your Dreams Our Mission/tuto...
davidwarner122
 
Python Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, ExceptionsPython Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, Exceptions
Sreedhar Chowdam
 
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
Rahul04August
 
Arrays and function basic c programming notes
Arrays and function basic c programming notesArrays and function basic c programming notes
Arrays and function basic c programming notes
GOKULKANNANMMECLECTC
 
16-sorting.ppt
16-sorting.ppt16-sorting.ppt
16-sorting.ppt
18Gunaalanpg
 
Javascript
JavascriptJavascript
Javascript
Vlad Ifrim
 
Object Oriented Programming Using C++: C++ STL Programming.pptx
Object Oriented Programming Using C++: C++ STL Programming.pptxObject Oriented Programming Using C++: C++ STL Programming.pptx
Object Oriented Programming Using C++: C++ STL Programming.pptx
RashidFaridChishti
 
Program 1 (Practicing an example of function using call by referenc.pdf
Program 1 (Practicing an example of function using call by referenc.pdfProgram 1 (Practicing an example of function using call by referenc.pdf
Program 1 (Practicing an example of function using call by referenc.pdf
ezhilvizhiyan
 
Data structure and algorithm first chapter
Data structure and algorithm first chapterData structure and algorithm first chapter
Data structure and algorithm first chapter
amiyapal2408
 
Hitchhiker's Guide to Functional Programming
Hitchhiker's Guide to Functional ProgrammingHitchhiker's Guide to Functional Programming
Hitchhiker's Guide to Functional Programming
Sergey Shishkin
 
ch07-arrays.ppt
ch07-arrays.pptch07-arrays.ppt
ch07-arrays.ppt
Mahyuddin8
 
object oriented programming java lectures
object oriented programming java lecturesobject oriented programming java lectures
object oriented programming java lectures
MSohaib24
 
Flink Batch Processing and Iterations
Flink Batch Processing and IterationsFlink Batch Processing and Iterations
Flink Batch Processing and Iterations
Sameer Wadkar
 
import java.util.LinkedList; import java.util.Random; import jav.pdf
import java.util.LinkedList; import java.util.Random; import jav.pdfimport java.util.LinkedList; import java.util.Random; import jav.pdf
import java.util.LinkedList; import java.util.Random; import jav.pdf
aquastore223
 
Functional Programming in Swift
Functional Programming in SwiftFunctional Programming in Swift
Functional Programming in Swift
Saugat Gautam
 
関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)
riue
 
DS LAB RECORD.docx
DS LAB RECORD.docxDS LAB RECORD.docx
DS LAB RECORD.docx
davinci54
 
Python list tuple dictionary .pptx
Python list tuple dictionary       .pptxPython list tuple dictionary       .pptx
Python list tuple dictionary .pptx
miteshchaudhari4466
 
Evolving with Java - How to remain Relevant and Effective
Evolving with Java - How to remain Relevant and EffectiveEvolving with Java - How to remain Relevant and Effective
Evolving with Java - How to remain Relevant and Effective
Naresha K
 
Begin with Python
Begin with PythonBegin with Python
Begin with Python
Narong Intiruk
 
Below is a given ArrayList class and Main class Your Dreams Our Mission/tuto...
Below is a given ArrayList class and Main class  Your Dreams Our Mission/tuto...Below is a given ArrayList class and Main class  Your Dreams Our Mission/tuto...
Below is a given ArrayList class and Main class Your Dreams Our Mission/tuto...
davidwarner122
 
Python Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, ExceptionsPython Programming: Lists, Modules, Exceptions
Python Programming: Lists, Modules, Exceptions
Sreedhar Chowdam
 
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
Rahul04August
 
Arrays and function basic c programming notes
Arrays and function basic c programming notesArrays and function basic c programming notes
Arrays and function basic c programming notes
GOKULKANNANMMECLECTC
 
Object Oriented Programming Using C++: C++ STL Programming.pptx
Object Oriented Programming Using C++: C++ STL Programming.pptxObject Oriented Programming Using C++: C++ STL Programming.pptx
Object Oriented Programming Using C++: C++ STL Programming.pptx
RashidFaridChishti
 
Program 1 (Practicing an example of function using call by referenc.pdf
Program 1 (Practicing an example of function using call by referenc.pdfProgram 1 (Practicing an example of function using call by referenc.pdf
Program 1 (Practicing an example of function using call by referenc.pdf
ezhilvizhiyan
 
Data structure and algorithm first chapter
Data structure and algorithm first chapterData structure and algorithm first chapter
Data structure and algorithm first chapter
amiyapal2408
 
Hitchhiker's Guide to Functional Programming
Hitchhiker's Guide to Functional ProgrammingHitchhiker's Guide to Functional Programming
Hitchhiker's Guide to Functional Programming
Sergey Shishkin
 
ch07-arrays.ppt
ch07-arrays.pptch07-arrays.ppt
ch07-arrays.ppt
Mahyuddin8
 
object oriented programming java lectures
object oriented programming java lecturesobject oriented programming java lectures
object oriented programming java lectures
MSohaib24
 
Flink Batch Processing and Iterations
Flink Batch Processing and IterationsFlink Batch Processing and Iterations
Flink Batch Processing and Iterations
Sameer Wadkar
 
import java.util.LinkedList; import java.util.Random; import jav.pdf
import java.util.LinkedList; import java.util.Random; import jav.pdfimport java.util.LinkedList; import java.util.Random; import jav.pdf
import java.util.LinkedList; import java.util.Random; import jav.pdf
aquastore223
 
Functional Programming in Swift
Functional Programming in SwiftFunctional Programming in Swift
Functional Programming in Swift
Saugat Gautam
 
関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)
riue
 
DS LAB RECORD.docx
DS LAB RECORD.docxDS LAB RECORD.docx
DS LAB RECORD.docx
davinci54
 
Python list tuple dictionary .pptx
Python list tuple dictionary       .pptxPython list tuple dictionary       .pptx
Python list tuple dictionary .pptx
miteshchaudhari4466
 
Evolving with Java - How to remain Relevant and Effective
Evolving with Java - How to remain Relevant and EffectiveEvolving with Java - How to remain Relevant and Effective
Evolving with Java - How to remain Relevant and Effective
Naresha K
 

More from ebrahimbadushata00 (20)

irktors (lcloding his accoueting instructor thmivenity c. All student.pdf
irktors (lcloding his accoueting instructor thmivenity c. All student.pdfirktors (lcloding his accoueting instructor thmivenity c. All student.pdf
irktors (lcloding his accoueting instructor thmivenity c. All student.pdf
ebrahimbadushata00
 
Is there a solution manual to group dynamics for team (fourth Editio.pdf
Is there a solution manual to group dynamics for team (fourth Editio.pdfIs there a solution manual to group dynamics for team (fourth Editio.pdf
Is there a solution manual to group dynamics for team (fourth Editio.pdf
ebrahimbadushata00
 
IntroductionFor this program, you will implement an interface that.pdf
IntroductionFor this program, you will implement an interface that.pdfIntroductionFor this program, you will implement an interface that.pdf
IntroductionFor this program, you will implement an interface that.pdf
ebrahimbadushata00
 
In Python,Create a program that asks the user for a number and the.pdf
In Python,Create a program that asks the user for a number and the.pdfIn Python,Create a program that asks the user for a number and the.pdf
In Python,Create a program that asks the user for a number and the.pdf
ebrahimbadushata00
 
In contrast to sexual reproduction in animals, sexually-reproducing .pdf
In contrast to sexual reproduction in animals, sexually-reproducing .pdfIn contrast to sexual reproduction in animals, sexually-reproducing .pdf
In contrast to sexual reproduction in animals, sexually-reproducing .pdf
ebrahimbadushata00
 
Ignore what I have written because Im pretty sure its wrong. Thank.pdf
Ignore what I have written because Im pretty sure its wrong. Thank.pdfIgnore what I have written because Im pretty sure its wrong. Thank.pdf
Ignore what I have written because Im pretty sure its wrong. Thank.pdf
ebrahimbadushata00
 
How can crisis leadership be learnedSolutionAn organization n.pdf
How can crisis leadership be learnedSolutionAn organization n.pdfHow can crisis leadership be learnedSolutionAn organization n.pdf
How can crisis leadership be learnedSolutionAn organization n.pdf
ebrahimbadushata00
 
Given the following information on a project develop early and la.pdf
Given the following information on a project develop early and la.pdfGiven the following information on a project develop early and la.pdf
Given the following information on a project develop early and la.pdf
ebrahimbadushata00
 
Global Economy, National Economies, and CompetitionIn the first pa.pdf
Global Economy, National Economies, and CompetitionIn the first pa.pdfGlobal Economy, National Economies, and CompetitionIn the first pa.pdf
Global Economy, National Economies, and CompetitionIn the first pa.pdf
ebrahimbadushata00
 
Explain why owners equity includes common stock as a liability eve.pdf
Explain why owners equity includes common stock as a liability eve.pdfExplain why owners equity includes common stock as a liability eve.pdf
Explain why owners equity includes common stock as a liability eve.pdf
ebrahimbadushata00
 
Evaluate the statements below and determine which is the best reason.pdf
Evaluate the statements below and determine which is the best reason.pdfEvaluate the statements below and determine which is the best reason.pdf
Evaluate the statements below and determine which is the best reason.pdf
ebrahimbadushata00
 
Discuss the Economic Benefits from Immigration.SolutionImmigra.pdf
Discuss the Economic Benefits from Immigration.SolutionImmigra.pdfDiscuss the Economic Benefits from Immigration.SolutionImmigra.pdf
Discuss the Economic Benefits from Immigration.SolutionImmigra.pdf
ebrahimbadushata00
 
Conclusion Phases of Oxidative Phosphorylation Focus your attention.pdf
Conclusion Phases of Oxidative Phosphorylation  Focus your attention.pdfConclusion Phases of Oxidative Phosphorylation  Focus your attention.pdf
Conclusion Phases of Oxidative Phosphorylation Focus your attention.pdf
ebrahimbadushata00
 
Computer Forensics Process Please respond to the followingThe.pdf
Computer Forensics Process Please respond to the followingThe.pdfComputer Forensics Process Please respond to the followingThe.pdf
Computer Forensics Process Please respond to the followingThe.pdf
ebrahimbadushata00
 
ArticleHinduism and Caste Systemby Jayaram VHinduism is a univ.pdf
ArticleHinduism and Caste Systemby Jayaram VHinduism is a univ.pdfArticleHinduism and Caste Systemby Jayaram VHinduism is a univ.pdf
ArticleHinduism and Caste Systemby Jayaram VHinduism is a univ.pdf
ebrahimbadushata00
 
Can someone solveexplain this I thought I was understanding this, .pdf
Can someone solveexplain this I thought I was understanding this, .pdfCan someone solveexplain this I thought I was understanding this, .pdf
Can someone solveexplain this I thought I was understanding this, .pdf
ebrahimbadushata00
 
C The ame compound componda with F Souls . E Difluut eoupou ds with.pdf
C The ame compound componda with F Souls . E  Difluut eoupou ds with.pdfC The ame compound componda with F Souls . E  Difluut eoupou ds with.pdf
C The ame compound componda with F Souls . E Difluut eoupou ds with.pdf
ebrahimbadushata00
 
Background Sometimes the standard C libraries (stdio.h, stdlib.h, e.pdf
Background Sometimes the standard C libraries (stdio.h, stdlib.h, e.pdfBackground Sometimes the standard C libraries (stdio.h, stdlib.h, e.pdf
Background Sometimes the standard C libraries (stdio.h, stdlib.h, e.pdf
ebrahimbadushata00
 
a. Modify the C program ex.9 so that it simulates the Unix pipe comm.pdf
a. Modify the C program ex.9 so that it simulates the Unix pipe comm.pdfa. Modify the C program ex.9 so that it simulates the Unix pipe comm.pdf
a. Modify the C program ex.9 so that it simulates the Unix pipe comm.pdf
ebrahimbadushata00
 
A severe B12 deficiency can cause megaloblastic anemia but in severe .pdf
A severe B12 deficiency can cause megaloblastic anemia but in severe .pdfA severe B12 deficiency can cause megaloblastic anemia but in severe .pdf
A severe B12 deficiency can cause megaloblastic anemia but in severe .pdf
ebrahimbadushata00
 
irktors (lcloding his accoueting instructor thmivenity c. All student.pdf
irktors (lcloding his accoueting instructor thmivenity c. All student.pdfirktors (lcloding his accoueting instructor thmivenity c. All student.pdf
irktors (lcloding his accoueting instructor thmivenity c. All student.pdf
ebrahimbadushata00
 
Is there a solution manual to group dynamics for team (fourth Editio.pdf
Is there a solution manual to group dynamics for team (fourth Editio.pdfIs there a solution manual to group dynamics for team (fourth Editio.pdf
Is there a solution manual to group dynamics for team (fourth Editio.pdf
ebrahimbadushata00
 
IntroductionFor this program, you will implement an interface that.pdf
IntroductionFor this program, you will implement an interface that.pdfIntroductionFor this program, you will implement an interface that.pdf
IntroductionFor this program, you will implement an interface that.pdf
ebrahimbadushata00
 
In Python,Create a program that asks the user for a number and the.pdf
In Python,Create a program that asks the user for a number and the.pdfIn Python,Create a program that asks the user for a number and the.pdf
In Python,Create a program that asks the user for a number and the.pdf
ebrahimbadushata00
 
In contrast to sexual reproduction in animals, sexually-reproducing .pdf
In contrast to sexual reproduction in animals, sexually-reproducing .pdfIn contrast to sexual reproduction in animals, sexually-reproducing .pdf
In contrast to sexual reproduction in animals, sexually-reproducing .pdf
ebrahimbadushata00
 
Ignore what I have written because Im pretty sure its wrong. Thank.pdf
Ignore what I have written because Im pretty sure its wrong. Thank.pdfIgnore what I have written because Im pretty sure its wrong. Thank.pdf
Ignore what I have written because Im pretty sure its wrong. Thank.pdf
ebrahimbadushata00
 
How can crisis leadership be learnedSolutionAn organization n.pdf
How can crisis leadership be learnedSolutionAn organization n.pdfHow can crisis leadership be learnedSolutionAn organization n.pdf
How can crisis leadership be learnedSolutionAn organization n.pdf
ebrahimbadushata00
 
Given the following information on a project develop early and la.pdf
Given the following information on a project develop early and la.pdfGiven the following information on a project develop early and la.pdf
Given the following information on a project develop early and la.pdf
ebrahimbadushata00
 
Global Economy, National Economies, and CompetitionIn the first pa.pdf
Global Economy, National Economies, and CompetitionIn the first pa.pdfGlobal Economy, National Economies, and CompetitionIn the first pa.pdf
Global Economy, National Economies, and CompetitionIn the first pa.pdf
ebrahimbadushata00
 
Explain why owners equity includes common stock as a liability eve.pdf
Explain why owners equity includes common stock as a liability eve.pdfExplain why owners equity includes common stock as a liability eve.pdf
Explain why owners equity includes common stock as a liability eve.pdf
ebrahimbadushata00
 
Evaluate the statements below and determine which is the best reason.pdf
Evaluate the statements below and determine which is the best reason.pdfEvaluate the statements below and determine which is the best reason.pdf
Evaluate the statements below and determine which is the best reason.pdf
ebrahimbadushata00
 
Discuss the Economic Benefits from Immigration.SolutionImmigra.pdf
Discuss the Economic Benefits from Immigration.SolutionImmigra.pdfDiscuss the Economic Benefits from Immigration.SolutionImmigra.pdf
Discuss the Economic Benefits from Immigration.SolutionImmigra.pdf
ebrahimbadushata00
 
Conclusion Phases of Oxidative Phosphorylation Focus your attention.pdf
Conclusion Phases of Oxidative Phosphorylation  Focus your attention.pdfConclusion Phases of Oxidative Phosphorylation  Focus your attention.pdf
Conclusion Phases of Oxidative Phosphorylation Focus your attention.pdf
ebrahimbadushata00
 
Computer Forensics Process Please respond to the followingThe.pdf
Computer Forensics Process Please respond to the followingThe.pdfComputer Forensics Process Please respond to the followingThe.pdf
Computer Forensics Process Please respond to the followingThe.pdf
ebrahimbadushata00
 
ArticleHinduism and Caste Systemby Jayaram VHinduism is a univ.pdf
ArticleHinduism and Caste Systemby Jayaram VHinduism is a univ.pdfArticleHinduism and Caste Systemby Jayaram VHinduism is a univ.pdf
ArticleHinduism and Caste Systemby Jayaram VHinduism is a univ.pdf
ebrahimbadushata00
 
Can someone solveexplain this I thought I was understanding this, .pdf
Can someone solveexplain this I thought I was understanding this, .pdfCan someone solveexplain this I thought I was understanding this, .pdf
Can someone solveexplain this I thought I was understanding this, .pdf
ebrahimbadushata00
 
C The ame compound componda with F Souls . E Difluut eoupou ds with.pdf
C The ame compound componda with F Souls . E  Difluut eoupou ds with.pdfC The ame compound componda with F Souls . E  Difluut eoupou ds with.pdf
C The ame compound componda with F Souls . E Difluut eoupou ds with.pdf
ebrahimbadushata00
 
Background Sometimes the standard C libraries (stdio.h, stdlib.h, e.pdf
Background Sometimes the standard C libraries (stdio.h, stdlib.h, e.pdfBackground Sometimes the standard C libraries (stdio.h, stdlib.h, e.pdf
Background Sometimes the standard C libraries (stdio.h, stdlib.h, e.pdf
ebrahimbadushata00
 
a. Modify the C program ex.9 so that it simulates the Unix pipe comm.pdf
a. Modify the C program ex.9 so that it simulates the Unix pipe comm.pdfa. Modify the C program ex.9 so that it simulates the Unix pipe comm.pdf
a. Modify the C program ex.9 so that it simulates the Unix pipe comm.pdf
ebrahimbadushata00
 
A severe B12 deficiency can cause megaloblastic anemia but in severe .pdf
A severe B12 deficiency can cause megaloblastic anemia but in severe .pdfA severe B12 deficiency can cause megaloblastic anemia but in severe .pdf
A severe B12 deficiency can cause megaloblastic anemia but in severe .pdf
ebrahimbadushata00
 
Ad

Recently uploaded (20)

Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and GuestsLDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDM Mia eStudios
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
The History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptxThe History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
Ajanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of HistoryAjanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of History
Virag Sontakke
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
*"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"**"Sensing the World: Insect Sensory Systems"*
*"Sensing the World: Insect Sensory Systems"*
Arshad Shaikh
 
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and GuestsLDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDM Mia eStudios
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
How to Clean Your Contacts Using the Deduplication Menu in Odoo 18
Celine George
 
Cultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptxCultivation Practice of Garlic in Nepal.pptx
Cultivation Practice of Garlic in Nepal.pptx
UmeshTimilsina1
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
Ajanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of HistoryAjanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of History
Virag Sontakke
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
Pope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptxPope Leo XIV, the first Pope from North America.pptx
Pope Leo XIV, the first Pope from North America.pptx
Martin M Flynn
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
How to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo SlidesHow to Create Kanban View in Odoo 18 - Odoo Slides
How to Create Kanban View in Odoo 18 - Odoo Slides
Celine George
 
Ad

Problem 1 Show the comparison of runtime of linear search and binar.pdf

  • 1. Problem 1: Show the comparison of runtime of linear search and binary search using line chart and table. Execute both algorithms 6 times on same data(use random integer generators), where input data size are: 50000, 100000, 150000, 200000, 250000, and 300000. Please report worst case runtimes so that the search item is not found in the input data. Problem 2: Show the comparison of runtime of bubble sort and merge sort using line chart and table. Execute both algorithms 6 times on same data(use random integer generators), where data size are: 50000, 100000, 150000, 200000, 250000, and 300000. In each execution, sort the data in ascending order. Solution Search & sort comparison for differnt sort & search algorithms. Problem1: Java Code: import java.util.Random; public class SearchComparison { public static void main(String[] args) { //Compare linear & binary search for //random data 6 times & print its execution time System.out.println("--------------------------------"); //50000 searchComparison(50000); System.out.println("--------------------------------"); //100000 searchComparison(100000); System.out.println("--------------------------------"); //150000 searchComparison(150000); System.out.println("--------------------------------"); //200000 searchComparison(200000); System.out.println("--------------------------------"); //250000 searchComparison(250000); System.out.println("--------------------------------"); //and 300000
  • 2. searchComparison(300000); System.out.println("--------------------------------"); } /** * searchComparison * @param size */ public static void searchComparison(int size){ System.out.println("Input size:"+size); //Generate random numbers list for given size Random randomGenerator = new Random(); int list[]=new int[size]; for (int index = 0; index < size; ++index){ list[index]=randomGenerator.nextInt(size); } //For worst case key must be last number list[size-1] long startTime=System.nanoTime(); linearSearch(list, list[size-1]); long endTime=System.nanoTime(); long totalTime=(endTime-startTime); //time in nanoseconds System.out.println("Linear search: input size="+size+" - time="+totalTime); startTime=System.nanoTime(); binarySearch(list, list[size-1]); endTime=System.nanoTime(); totalTime=(endTime-startTime); //time in milliseconds System.out.println("Binary search: input size="+size+" - time="+totalTime); } /** * Linearly search key in list[]. If key is present then return the index, * otherwise return -1 * * @param list * @param key * @return */
  • 3. public static int linearSearch(int list[], int key) { for (int index = 0; index < list.length; index++) { if (list[index] == key) return index; } return -1; } /** * BinarySearch search key in list[]. If key is present then return the * index, otherwise return -1 * * @param list * @param key * @return */ public static int binarySearch(int list[], int key) { int lo = 0; int hi = list.length - 1; while (lo <= hi) { // Key is in list[lo..hi] or not present. int mid = lo + (hi - lo) / 2; if (key < list[mid]) hi = mid - 1; else if (key > list[mid]) lo = mid + 1; else return mid; } return -1; } } Steps to compile & run the program: javac SearchComparison.java java SearchComparison Output: --------------------------------
  • 4. Input size:50000 Linear search: input size=50000 - time=2124844 Binary search: input size=50000 - time=16627 -------------------------------- Input size:100000 Linear search: input size=100000 - time=340572 Binary search: input size=100000 - time=18489 -------------------------------- Input size:150000 Linear search: input size=150000 - time=3344970 Binary search: input size=150000 - time=4584 -------------------------------- Input size:200000 Linear search: input size=200000 - time=668294 Binary search: input size=200000 - time=4557 -------------------------------- Input size:250000 Linear search: input size=250000 - time=77522 Binary search: input size=250000 - time=19226 -------------------------------- Input size:300000 Linear search: input size=300000 - time=164177 Binary search: input size=300000 - time=3978 -------------------------------- Problem 2: Java Code- public class SortComparison { public static void main(String[] args) { // Compare linear & binary search for // random data 6 times & print its execution time System.out.println("--------------------------------"); // 50000 sortComparison(50000); System.out.println("--------------------------------"); // 100000 sortComparison(100000); System.out.println("--------------------------------");
  • 5. // 150000 sortComparison(150000); System.out.println("--------------------------------"); // 200000 sortComparison(200000); System.out.println("--------------------------------"); // 250000 sortComparison(250000); System.out.println("--------------------------------"); // and 300000 sortComparison(300000); System.out.println("--------------------------------"); } public static void sortComparison(int size) { System.out.println("Input size:" + size); // Generate random numbers list for given size Random randomGenerator = new Random(); int list[] = new int[size]; for (int index = 0; index < size; ++index) { list[index] = randomGenerator.nextInt(size); } long startTime = System.nanoTime(); bubbleSort(list); long endTime = System.nanoTime(); long totalTime = (endTime - startTime); // time in nanoseconds System.out.println("Bubble sort: input size=" + size + " - time=" + totalTime); startTime = System.nanoTime(); mergesort(list); endTime = System.nanoTime(); totalTime = (endTime - startTime); // time in milliseconds System.out.println("Merge sort: input size=" + size + " - time=" + totalTime); } /** * Method use bubble sort algorithm to sort list ascending order * * @param list
  • 6. */ public static void bubbleSort(int list[]) { int temp = 0; for (int index1 = 0; index1 < (list.length - 1); index1++) { for (int index2 = 0; index2 < list.length - index1 - 1; index2++) { if (list[index2] > list[index2 + 1]) { temp = list[index2]; list[index2] = list[index2 + 1]; list[index2 + 1] = temp; } } } } /** * Method use merge sort algorithm to sort list ascending order * * @param list * @return */ public static int[] mergesort(int[] list) { int size = list.length; if (size <= 1) return list; int[] temp1 = new int[size / 2]; int[] temp2 = new int[size - size / 2]; for (int index = 0; index < temp1.length; index++) temp1[index] = list[index]; for (int index = 0; index < temp2.length; index++) temp2[index] = list[index + size / 2]; return merge(mergesort(temp1), mergesort(temp2)); } private static int[] merge(int[] temp1, int[] temp2) { int[] temp3 = new int[temp1.length + temp2.length]; int index1 = 0, index2 = 0; for (int index3 = 0; index3 < temp3.length; index3++) { if (index1 >= temp1.length)
  • 7. temp3[index3] = temp2[index2++]; else if (index2 >= temp2.length) temp3[index3] = temp1[index1++]; else if (temp1[index1] <= temp2[index2]) temp3[index3] = temp1[index1++]; else temp3[index3] = temp2[index2++]; } return temp3; } } Steps to compile & run the program: javac SortComparison.java java SortComparison Output: -------------------------------- Input size:50000 Bubble sort: input size=50000 - time=6442721760 Merge sort: input size=50000 - time=33285535 -------------------------------- Input size:100000 Bubble sort: input size=100000 - time=25841668982 Merge sort: input size=100000 - time=24046898 -------------------------------- Input size:150000 Bubble sort: input size=150000 - time=58446233927 Merge sort: input size=150000 - time=29973618 -------------------------------- Input size:200000 Bubble sort: input size=200000 - time=104861524269 Merge sort: input size=200000 - time=39586920 -------------------------------- Input size:250000 Bubble sort: input size=250000 - time=160468980476 Merge sort: input size=250000 - time=45671299 --------------------------------
  • 8. Input size:300000 Bubble sort: input size=300000 - time=229789831510 Merge sort: input size=300000 - time=54097226 -------------------------------- Please draw line chart & table using above output data of search & sort comparison for differnt sort & search algorithms. Let me know in case any query in comment section.
  翻译: