SlideShare ist ein Scribd-Unternehmen logo
1 von 16
Below is a given ArrayList class and Main class in search
algorithms. Please modify the existing program so it can
time the sequential search
To Purchase This Material Click below Link
http://www.tutorialoutlet.com/all-miscellaneous/below-is-
a-given-arraylist-class-and-main-class-in-search-algorithms-
please-modify-the-existing-program-so-it-can-time-the-
sequential-search
FOR MORE CLASSES VISIT
www.tutorialoutlet.com
Below is a given ArrayList class and Main class in search algorithms.
Please modify the existing program so it can time the sequential
search and the binary search methods several times each for randomly
generated values, and record the results in a table. Do not time
individual searches, but groups of them. For example, time 100
searches together or 1,000 searches together. Compare the running
times of these two search methods that are obtained during the
experiment.
Regarding the efficiency of both search methods, what conclusion can
be reached from this experiment?
Array list below:
/**
* Class implementing an array based list. Sequential search, sorted
search, and
* binary search algorithms are implemented also.
*/
public class ArrayList
{
/**
* Default constructor. Sets length to 0, initializing the list as an empty
* list. Default size of array is 20.
*/
public ArrayList()
{
SIZE = 20;
list = new int[SIZE];
length = 0;
}
/**
* Determines whether the list is empty
*
* @return true if the list is empty, false otherwise
*/
public boolean isEmpty()
{
return length == 0;
}
/**
* Prints the list elements.
*/
public void display()
{
for (int i = 0; i < length; i++)
System.out.print(list[i] + " ");
System.out.println();
}
/**
* Adds the element x to the end of the list. List length is increased by
1.
*
* @param x element to be added to the list
*/
public void add(int x)
{
if (length == SIZE)
System.out.println("Insertion Error: list is full");
else
{
list[length] = x;
length++;
}
}
/**
* Removes the element at the given location from the list. List length
is
* decreased by 1.
*
* @param pos location of the item to be removed
*/
public void removeAt(int pos)
{
for (int i = pos; i < length - 1; i++)
list[i] = list[i + 1];
length--;
}
//Implementation of methods in the lab exercise
/**
* Non default constructor. Sets length to 0, initializing the list as an
* empty list. Size of array is passed as a parameter.
*
* @param size size of the array list
*/
public ArrayList(int size)
{
SIZE = size;
list = new int[SIZE];
length = 0;
}
/**
* Returns the number of items in the list (accessor method).
*
* @return the number of items in the list.
*/
public int getLength()
{
return length;
}
/**
* Returns the size of the list (accessor method).
*
* @return the size of the array
*/
public int getSize()
{
return SIZE;
}
/**
* Removes all of the items from the list. After this operation, the
length
* of the list is zero.
*/
public void clear()
{
length = 0;
}
/**
* Replaces the item in the list at the position specified by location.
*
* @param location location of the element to be replaced
* @param item value that will replace the value at location
*/
public void replace(int location, int item)
{
if (location < 0 || location >= length)
System.out.println("Error: invalid location");
else
list[location] = item;
}
/**
* Adds an item to the list at the position specified by location.
*
* @param location location where item will be added.
* @param item item to be added to the list.
*/
public void add(int location, int item)
{
if (location < 0 || location >= length)
System.out.println("Error: invalid position");
else if (length == SIZE)
System.out.println("Error: Array is full");
else
{
for (int i = length; i > location; i--)
list[ i] = list[ i - 1];
list[location] = item;
length++;
}
}
/**
* Deletes an item from the list. All occurrences of item in the list will
* be removed.
*
* @param item element to be removed.
*/
public void remove(int item)
{
for (int i = 0; i < length; i++)
if (list[i] == item)
{
removeAt(i);
i--; //onsecutive values won't be all removed; that's why i-- is here
}
}
/**
* Returns the element at location
*
* @param location position in the list of the item to be returned
* @return element at location
*/
public int get(int location)
{
int x = -1;
if (location < 0 || location >= length)
System.out.println("Error: invalid location");
else
x = list[location];
return x;
}
/**
* Makes a deep copy to another ArrayList object.
*
* @return Copy of this ArrayList
*/
public ArrayList copy()
{
ArrayList newList = new ArrayList(this.SIZE);
newList.length = this.length;
for (int i = 0; i < length; i++)
newList.list[i] = this.list[i];
return newList;
}
/**
* Bubble-sorts this ArrayList
*/
public void bubbleSort()
{
for (int i = 0; i < length - 1; i++)
for (int j = 0; j < length - i - 1; j++)
if (list[j] > list[j + 1])
{
//swap list[j] and list[j+1]
int temp = list[j];
list[j] = list[j + 1];
list[j + 1] = temp;
}
}
/**
* Quick-sorts this ArrayList.
*/
public void quicksort()
{
quicksort(0, length - 1);
}
/**
* Recursive quicksort algorithm.
*
* @param begin initial index of sublist to be quick-sorted.
* @param end last index of sublist to be quick-sorted.
*/
private void quicksort(int begin, int end)
{
int temp;
int pivot = findPivotLocation(begin, end);
// swap list[pivot] and list[end]
temp = list[pivot];
list[pivot] = list[end];
list[end] = temp;
pivot = end;
int i = begin,
j = end - 1;
boolean iterationCompleted = false;
while (!iterationCompleted)
{
while (list[i] < list[pivot])
i++;
while ((j >= 0) && (list[pivot] < list[j]))
j--;
if (i < j)
{
//swap list[i] and list[j]
temp = list[i];
list[i] = list[j];
list[j] = temp;
i++;
j--;
} else
iterationCompleted = true;
}
//swap list[i] and list[pivot]
temp = list[i];
list[i] = list[pivot];
list[pivot] = temp;
if (begin < i - 1)
quicksort(begin, i - 1);
if (i + 1 < end)
quicksort(i + 1, end);
}
/*
* Computes the pivot location.
*/
private int findPivotLocation(int b, int e)
{
return (b + e) / 2;
}
/*The methods listed below are new additions to the ArrayList class
* of Week 4*/
/**
* This method returns a string representation of the array list
elements.
* Classes with this method implemented can get its objects displayed
in a
* simple way using System.out.println. For example, if list is an
ArrayList
* object, it can be printed by using
*
* System.out.println(list);
*
* @return a string representation of the array list elements.
*/
public String toString()
{
String s = "";
for (int i = 0; i < length; i++)
s += list[i] + " ";
return s;
}
/**
* Determines if an item exists in the array list using sequential (linear)
* search.
*
* @param x item to be found.
* @return true if x is found in the list, false otherwise.
*/
public boolean sequentialSearch(int x)
{
for (int i = 0; i < length; i++)
if (list[i] == x)
return true;
return false;
}
/**
* Determines if an item exists in the array list using sorted search.
List
* must be sorted.
*
* @param x item to be found.
* @return true if x is found in the list, false otherwise.
*/
public boolean sortedSearch(int x)
{
//The list must ne sorted to invoke this method!
int i = 0;
while (i < length && list[i] < x)
i++;

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Python Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, DictionaryPython Variable Types, List, Tuple, Dictionary
Python Variable Types, List, Tuple, Dictionary
 
Chapter 17 Tuples
Chapter 17 TuplesChapter 17 Tuples
Chapter 17 Tuples
 
Chapter14
Chapter14Chapter14
Chapter14
 
Python Workshop Part 2. LUG Maniapl
Python Workshop Part 2. LUG ManiaplPython Workshop Part 2. LUG Maniapl
Python Workshop Part 2. LUG Maniapl
 
Sets in python
Sets in pythonSets in python
Sets in python
 
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | Edureka
 
Python set
Python setPython set
Python set
 
Data Structures in Python
Data Structures in PythonData Structures in Python
Data Structures in Python
 
Python Programming Essentials - M12 - Lists
Python Programming Essentials - M12 - ListsPython Programming Essentials - M12 - Lists
Python Programming Essentials - M12 - Lists
 
1. python
1. python1. python
1. python
 
Dictionaries and Sets in Python
Dictionaries and Sets in PythonDictionaries and Sets in Python
Dictionaries and Sets in Python
 
Python programming : Arrays
Python programming : ArraysPython programming : Arrays
Python programming : Arrays
 
Python list
Python listPython list
Python list
 
Python lists
Python listsPython lists
Python lists
 
Python Collections
Python CollectionsPython Collections
Python Collections
 
Basic Sorting algorithms csharp
Basic Sorting algorithms csharpBasic Sorting algorithms csharp
Basic Sorting algorithms csharp
 
Arrays in python
Arrays in pythonArrays in python
Arrays in python
 
Python list
Python listPython list
Python list
 
LIST IN PYTHON
LIST IN PYTHONLIST IN PYTHON
LIST IN PYTHON
 

Ähnlich wie Below is a given ArrayList class and Main class Your Dreams Our Mission/tutorialoutletdotcom

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 .pdfarshin9
 
All code should be in C++Using the UnsortedList class (UnsortedLis.pdf
All code should be in C++Using the UnsortedList class (UnsortedLis.pdfAll code should be in C++Using the UnsortedList class (UnsortedLis.pdf
All code should be in C++Using the UnsortedList class (UnsortedLis.pdfakashenterprises93
 
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdfJAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdfarpaqindia
 
This class maintains a list of 4 integers. This list .docx
 This class maintains a list of 4 integers.   This list .docx This class maintains a list of 4 integers.   This list .docx
This class maintains a list of 4 integers. This list .docxKomlin1
 
Hi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdfHi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdfannaelctronics
 
Everything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdfEverything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdffirstchoiceajmer
 
we using java code DynamicArrayjava Replace all .pdf
we using java code   DynamicArrayjava   Replace all .pdfwe using java code   DynamicArrayjava   Replace all .pdf
we using java code DynamicArrayjava Replace all .pdfgudduraza28
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfravikapoorindia
 
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docxweek4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docxalanfhall8953
 
Write a program to find the number of comparisons using the binary se.docx
 Write a program to find the number of comparisons using the binary se.docx Write a program to find the number of comparisons using the binary se.docx
Write a program to find the number of comparisons using the binary se.docxajoy21
 
helpInstructionsAdd the function max as an abstract function to .pdf
helpInstructionsAdd the function max as an abstract function to .pdfhelpInstructionsAdd the function max as an abstract function to .pdf
helpInstructionsAdd the function max as an abstract function to .pdfalmonardfans
 
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
Note- Can someone help me with the public boolean isEmpty()- public bo.pdfNote- Can someone help me with the public boolean isEmpty()- public bo.pdf
Note- Can someone help me with the public boolean isEmpty()- public bo.pdfAugstore
 
Write a function which return a list of all of the n element subset .pdf
Write a function which return a list of all of the n element subset .pdfWrite a function which return a list of all of the n element subset .pdf
Write a function which return a list of all of the n element subset .pdfhardjasonoco14599
 
(Unordered Sets) As explained in this chapter, a set is a collection.pdf
(Unordered Sets) As explained in this chapter, a set is a collection.pdf(Unordered Sets) As explained in this chapter, a set is a collection.pdf
(Unordered Sets) As explained in this chapter, a set is a collection.pdfssuserc77a341
 
Please help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdfPlease help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdfseoagam1
 
Problem 1 Show the comparison of runtime of linear search and binar.pdf
Problem 1 Show the comparison of runtime of linear search and binar.pdfProblem 1 Show the comparison of runtime of linear search and binar.pdf
Problem 1 Show the comparison of runtime of linear search and binar.pdfebrahimbadushata00
 
you will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdfyou will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdfclearvisioneyecareno
 
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdfC++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdfcallawaycorb73779
 
please read below it will tell you what we are using L.pdf
please read below it will tell you what we are using   L.pdfplease read below it will tell you what we are using   L.pdf
please read below it will tell you what we are using L.pdfankit11134
 
#ifndef MYLIST_H_ #define MYLIST_H_#includeiostream #include.docx
#ifndef MYLIST_H_ #define MYLIST_H_#includeiostream #include.docx#ifndef MYLIST_H_ #define MYLIST_H_#includeiostream #include.docx
#ifndef MYLIST_H_ #define MYLIST_H_#includeiostream #include.docxajoy21
 

Ähnlich wie Below is a given ArrayList class and Main class Your Dreams Our Mission/tutorialoutletdotcom (20)

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
 
All code should be in C++Using the UnsortedList class (UnsortedLis.pdf
All code should be in C++Using the UnsortedList class (UnsortedLis.pdfAll code should be in C++Using the UnsortedList class (UnsortedLis.pdf
All code should be in C++Using the UnsortedList class (UnsortedLis.pdf
 
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdfJAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
JAVALAB #8 - ARRAY BASED LISTSThe next exercise is based on this.pdf
 
This class maintains a list of 4 integers. This list .docx
 This class maintains a list of 4 integers.   This list .docx This class maintains a list of 4 integers.   This list .docx
This class maintains a list of 4 integers. This list .docx
 
Hi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdfHi,I have added the methods and main class as per your requirement.pdf
Hi,I have added the methods and main class as per your requirement.pdf
 
Everything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdfEverything needs to be according to the instructions- thank you! SUPPO.pdf
Everything needs to be according to the instructions- thank you! SUPPO.pdf
 
we using java code DynamicArrayjava Replace all .pdf
we using java code   DynamicArrayjava   Replace all .pdfwe using java code   DynamicArrayjava   Replace all .pdf
we using java code DynamicArrayjava Replace all .pdf
 
ReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdfReversePoem.java ---------------------------------- public cl.pdf
ReversePoem.java ---------------------------------- public cl.pdf
 
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docxweek4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
week4_srcArrayMethods.javaweek4_srcArrayMethods.javapackage ed.docx
 
Write a program to find the number of comparisons using the binary se.docx
 Write a program to find the number of comparisons using the binary se.docx Write a program to find the number of comparisons using the binary se.docx
Write a program to find the number of comparisons using the binary se.docx
 
helpInstructionsAdd the function max as an abstract function to .pdf
helpInstructionsAdd the function max as an abstract function to .pdfhelpInstructionsAdd the function max as an abstract function to .pdf
helpInstructionsAdd the function max as an abstract function to .pdf
 
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
Note- Can someone help me with the public boolean isEmpty()- public bo.pdfNote- Can someone help me with the public boolean isEmpty()- public bo.pdf
Note- Can someone help me with the public boolean isEmpty()- public bo.pdf
 
Write a function which return a list of all of the n element subset .pdf
Write a function which return a list of all of the n element subset .pdfWrite a function which return a list of all of the n element subset .pdf
Write a function which return a list of all of the n element subset .pdf
 
(Unordered Sets) As explained in this chapter, a set is a collection.pdf
(Unordered Sets) As explained in this chapter, a set is a collection.pdf(Unordered Sets) As explained in this chapter, a set is a collection.pdf
(Unordered Sets) As explained in this chapter, a set is a collection.pdf
 
Please help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdfPlease help me to make a programming project I have to sue them today- (1).pdf
Please help me to make a programming project I have to sue them today- (1).pdf
 
Problem 1 Show the comparison of runtime of linear search and binar.pdf
Problem 1 Show the comparison of runtime of linear search and binar.pdfProblem 1 Show the comparison of runtime of linear search and binar.pdf
Problem 1 Show the comparison of runtime of linear search and binar.pdf
 
you will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdfyou will implement some sorting algorithms for arrays and linked lis.pdf
you will implement some sorting algorithms for arrays and linked lis.pdf
 
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdfC++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
C++ problemPart 1 Recursive Print (40 pts)Please write the recu.pdf
 
please read below it will tell you what we are using L.pdf
please read below it will tell you what we are using   L.pdfplease read below it will tell you what we are using   L.pdf
please read below it will tell you what we are using L.pdf
 
#ifndef MYLIST_H_ #define MYLIST_H_#includeiostream #include.docx
#ifndef MYLIST_H_ #define MYLIST_H_#includeiostream #include.docx#ifndef MYLIST_H_ #define MYLIST_H_#includeiostream #include.docx
#ifndef MYLIST_H_ #define MYLIST_H_#includeiostream #include.docx
 

Kürzlich hochgeladen

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
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
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
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
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
 
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
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpinRaunakKeshri1
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
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
 
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 GraphThiyagu K
 
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 ModeThiyagu K
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...anjaliyadav012327
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 

Kürzlich hochgeladen (20)

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...
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
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...
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
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
 
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
 
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"
 
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
 
Student login on Anyboli platform.helpin
Student login on Anyboli platform.helpinStudent login on Anyboli platform.helpin
Student login on Anyboli platform.helpin
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
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 ...
 
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
 
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
 
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
JAPAN: ORGANISATION OF PMDA, PHARMACEUTICAL LAWS & REGULATIONS, TYPES OF REGI...
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 

Below is a given ArrayList class and Main class Your Dreams Our Mission/tutorialoutletdotcom

  • 1. Below is a given ArrayList class and Main class in search algorithms. Please modify the existing program so it can time the sequential search To Purchase This Material Click below Link http://www.tutorialoutlet.com/all-miscellaneous/below-is- a-given-arraylist-class-and-main-class-in-search-algorithms- please-modify-the-existing-program-so-it-can-time-the- sequential-search FOR MORE CLASSES VISIT www.tutorialoutlet.com Below is a given ArrayList class and Main class in search algorithms. Please modify the existing program so it can time the sequential search and the binary search methods several times each for randomly generated values, and record the results in a table. Do not time individual searches, but groups of them. For example, time 100 searches together or 1,000 searches together. Compare the running times of these two search methods that are obtained during the experiment. Regarding the efficiency of both search methods, what conclusion can be reached from this experiment? Array list below: /**
  • 2. * Class implementing an array based list. Sequential search, sorted search, and * binary search algorithms are implemented also. */ public class ArrayList { /** * Default constructor. Sets length to 0, initializing the list as an empty * list. Default size of array is 20. */ public ArrayList() { SIZE = 20; list = new int[SIZE]; length = 0; } /** * Determines whether the list is empty * * @return true if the list is empty, false otherwise */ public boolean isEmpty()
  • 3. { return length == 0; } /** * Prints the list elements. */ public void display() { for (int i = 0; i < length; i++) System.out.print(list[i] + " "); System.out.println(); } /** * Adds the element x to the end of the list. List length is increased by 1. * * @param x element to be added to the list */ public void add(int x) { if (length == SIZE)
  • 4. System.out.println("Insertion Error: list is full"); else { list[length] = x; length++; } } /** * Removes the element at the given location from the list. List length is * decreased by 1. * * @param pos location of the item to be removed */ public void removeAt(int pos) { for (int i = pos; i < length - 1; i++) list[i] = list[i + 1]; length--; } //Implementation of methods in the lab exercise /**
  • 5. * Non default constructor. Sets length to 0, initializing the list as an * empty list. Size of array is passed as a parameter. * * @param size size of the array list */ public ArrayList(int size) { SIZE = size; list = new int[SIZE]; length = 0; } /** * Returns the number of items in the list (accessor method). * * @return the number of items in the list. */ public int getLength() { return length; } /**
  • 6. * Returns the size of the list (accessor method). * * @return the size of the array */ public int getSize() { return SIZE; } /** * Removes all of the items from the list. After this operation, the length * of the list is zero. */ public void clear() { length = 0; } /** * Replaces the item in the list at the position specified by location. * * @param location location of the element to be replaced * @param item value that will replace the value at location
  • 7. */ public void replace(int location, int item) { if (location < 0 || location >= length) System.out.println("Error: invalid location"); else list[location] = item; } /** * Adds an item to the list at the position specified by location. * * @param location location where item will be added. * @param item item to be added to the list. */ public void add(int location, int item) { if (location < 0 || location >= length) System.out.println("Error: invalid position"); else if (length == SIZE) System.out.println("Error: Array is full"); else {
  • 8. for (int i = length; i > location; i--) list[ i] = list[ i - 1]; list[location] = item; length++; } } /** * Deletes an item from the list. All occurrences of item in the list will * be removed. * * @param item element to be removed. */ public void remove(int item) { for (int i = 0; i < length; i++) if (list[i] == item) { removeAt(i); i--; //onsecutive values won't be all removed; that's why i-- is here } }
  • 9. /** * Returns the element at location * * @param location position in the list of the item to be returned * @return element at location */ public int get(int location) { int x = -1; if (location < 0 || location >= length) System.out.println("Error: invalid location"); else x = list[location]; return x; } /** * Makes a deep copy to another ArrayList object. * * @return Copy of this ArrayList */
  • 10. public ArrayList copy() { ArrayList newList = new ArrayList(this.SIZE); newList.length = this.length; for (int i = 0; i < length; i++) newList.list[i] = this.list[i]; return newList; } /** * Bubble-sorts this ArrayList */ public void bubbleSort() { for (int i = 0; i < length - 1; i++) for (int j = 0; j < length - i - 1; j++) if (list[j] > list[j + 1]) { //swap list[j] and list[j+1] int temp = list[j];
  • 11. list[j] = list[j + 1]; list[j + 1] = temp; } } /** * Quick-sorts this ArrayList. */ public void quicksort() { quicksort(0, length - 1); } /** * Recursive quicksort algorithm. * * @param begin initial index of sublist to be quick-sorted. * @param end last index of sublist to be quick-sorted. */ private void quicksort(int begin, int end) { int temp; int pivot = findPivotLocation(begin, end);
  • 12. // swap list[pivot] and list[end] temp = list[pivot]; list[pivot] = list[end]; list[end] = temp; pivot = end; int i = begin, j = end - 1; boolean iterationCompleted = false; while (!iterationCompleted) { while (list[i] < list[pivot]) i++; while ((j >= 0) && (list[pivot] < list[j])) j--; if (i < j) { //swap list[i] and list[j] temp = list[i];
  • 13. list[i] = list[j]; list[j] = temp; i++; j--; } else iterationCompleted = true; } //swap list[i] and list[pivot] temp = list[i]; list[i] = list[pivot]; list[pivot] = temp; if (begin < i - 1) quicksort(begin, i - 1); if (i + 1 < end) quicksort(i + 1, end); } /* * Computes the pivot location. */
  • 14. private int findPivotLocation(int b, int e) { return (b + e) / 2; } /*The methods listed below are new additions to the ArrayList class * of Week 4*/ /** * This method returns a string representation of the array list elements. * Classes with this method implemented can get its objects displayed in a * simple way using System.out.println. For example, if list is an ArrayList * object, it can be printed by using * * System.out.println(list); * * @return a string representation of the array list elements. */ public String toString() { String s = ""; for (int i = 0; i < length; i++)
  • 15. s += list[i] + " "; return s; } /** * Determines if an item exists in the array list using sequential (linear) * search. * * @param x item to be found. * @return true if x is found in the list, false otherwise. */ public boolean sequentialSearch(int x) { for (int i = 0; i < length; i++) if (list[i] == x) return true; return false; } /** * Determines if an item exists in the array list using sorted search. List
  • 16. * must be sorted. * * @param x item to be found. * @return true if x is found in the list, false otherwise. */ public boolean sortedSearch(int x) { //The list must ne sorted to invoke this method! int i = 0; while (i < length && list[i] < x) i++;