For this lab you will implement several methods to determine if an array of inte
ID: 3882117 • Letter: F
Question
For this lab you will implement several methods to determine if an array of integers in a certain range contains duplicate elements. First, you need to write a method that receives two integers n and m and builds and returns an array of length n that contains random integers in the 0 to m - 1 range. Then implement the following methods to determine if the array contains duplicates 1. Compare every element in the array with every other element in the array using nested loops as follows: public static boolean hasDuplicates (int CJA) boolean duplicates = false for (int i-0;iExplanation / Answer
Solution:
2, 3, and 4 is solved, please repost others.
2)
public static boolean hasDuplicates(int []A)
{
boolean duplicates = false; //variable declaration
for(int i= 0; i<A.lenght; i++)
{
for(int j= 0; j<A.length; j++)
{
if (i==j)
break;//Avoding comparing element with itself
else if(A[i] == A[j])
duplicates = true;
break;//As soon as duplicate is found the method will terminate and return
return duplicates
}
}
}
3)
Selection Sort:
public static void selectionSort(String[] array) //Method definition and body
{
int current, indexSmallest, posToFill;
String temp;
for(posToFill = 0; posToFill < array.length - 1; posToFill++)
{
indexSmallest = posToFill; //Filling array with its indexes
for(current = posToFill + 1; current < array.length; current++)
{ //.compareTo??
if(array[current] < array[indexSmallest]) //if the current value is smaller than its index
{
indexSmallest = current; // assign the current to that index
}
}
temp = array[posToFill]; //hold temporaraly the value at PosToFill
array[posToFill] = array[indexSmallest]; //swap the values between smallest Index and PosToFill
array[indexSmallest] = temp;
}
System.out.print(Arrays.toString(array)); //Display the array
}
}
Finindng duplicate:
public static boolean hasDuplicates(int []A)
{
boolean duplicates = false
for(int i= 1; i<A.length; i++)
{
if(A[i-1]==A[i])
duplicates = true;
}
}
4)
Quick sort:
public static void quickSort(int[] a, int p, int r) //Fucntion definition and body
{
if(p<r) //if left is smaller than right
{
int q=partition(a,p,r); //partition of the array
quickSort(a,p,q);
quickSort(a,q+1,r);
}
}
private static int partition(int[] a, int p, int r) { //Partition method
int x = a[p]; //saving pivot in x
int i = p;
int j = r;
while (true) {
while (i < r && a[i] < x) //here pivot will get placed at its right position and the element smaller than the pivot will be on the left hand side and elements larger than the pivot will be placed in the right hand side.
i++;
while (j > p && a[j] > x)
j--;
if (i < j) {
swap(a, i, j);
i++;
j--;
} else {
return j;
}
}
}
private static void swap(int[] a, int i, int j) {
// TODO Auto-generated method stub
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
Finindng duplicate:
public static boolean hasDuplicates(int []A)
{
boolean duplicates = false
for(int i= 1; i<A.length; i++)
{
if(A[i-1]==A[i])
duplicates = true;
}
}
I hope this helps if you find any problem. Please comment below. Don't forget to give a thumbs up if you liked it. :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.