I've been studying Java for a bit and trying out arrays.
This is the current one I've been trying. My goal is to output "match" if arrElements match the intended index which is indexArray after arrElements get sorted using indexArray or "not match" if it doesn't.
import java.util.Arrays;
import java.util.Scanner;
public class Copytest {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arrElements[] = new int[n];
int indexArray[] = new int[n];
for (int i = 0 ; i < n ; i ){
arrElements[i] = sc.nextInt();
}
for (int i = 0 ; i < n ; i ){
indexArray[i] = sc.nextInt();
}
for (int i = 0; i < indexArray.length; i ) {
for (int j = i 1; j < indexArray.length; j ) {
if (indexArray[i] > indexArray[j]) {
int tempInd = indexArray[i];
int tempName = arrElements[i];
indexArray[i] = indexArray[j];
arrElements[i] = arrElements[j];
indexArray[j] = tempInd;
arrElements[j] = tempName;
}
}
}
System.out.println("");
System.out.println(Arrays.toString(arrElements));
System.out.println(Arrays.toString(indexArray));
}
This is what I have so far. (the output at the end is just temporary to see if it gets sorted properly)
I don't know how to do the output I intended to do.
as an example I wanted to do it like this:
Input:
5
10 5 20 25 15
1 2 0 3 4
Output
"match"
CodePudding user response:
if you wish check if two arrays contains same items (in same order), you can use Arrays.equals(array1, array2)
Documentation says:
Returns true if the two specified arrays of ints are equal to one another. Two arrays are considered equal if both arrays contain the same number of elements, and all corresponding pairs of elements in the two arrays are equal. In other words, two arrays are equal if they contain the same elements in the same order. Also, two array references are considered equal if both are null.
Btw. maybe using Arrays.sort(array)
could be usefull for array sorting.
Documentation says:
Sorts the specified array into ascending numerical order.
Implementation note: The sorting algorithm is a Dual-Pivot Quicksort by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm offers O(n log(n)) performance on many data sets that cause other quicksorts to degrade to quadratic performance, and is typically faster than traditional (one-pivot) Quicksort implementations.
So, my suggested solution:
int arrElements[] = new int[n];
int indexArray[] = new int[n];
// here fill the arrays
// ...
// sort the arrays
Arrays.sort(arrElements);
Arrays.sort(indexArray);
// compare
if (Arrays.equals(arrElements, indexArray)) {
System.out.println("Match");
} else {
System.out.println("Not match");
}
I hope this help you.
EDIT
If you wish sort first array using indexes in the second:
int arrElements[] = new int[n];
int indexArray[] = new int[n];
// here fill the arrays
// ...
if (arrElements.length != indexArray.length) {
System.out.println("Not match");
return;
}
int sorted[] = new int[indexArray.length];
for (int i = 0; i < indexArray.length; i ) {
if (indexArray[i] >= indexArray.length) {
// indexArray[i] is not existing index in arrEelements
System.out.println("Not match");
return;
}
sorted[i] = arrElements[indexArray[i]];
}
System.out.println("Match");