Java Merge Sorting Compare and Count - java

I have been having trouble with figuring this problem out. I've added a count and compare to identify how many times my sort actually does something and I believe that I am doing it incorrectly because it is not matching up to my example.
import java.util.Arrays;
public class MergeSort {
/**
* #param args
*/
// Rearranges the elements of a into sorted order using
// the merge sort algorithm (recursive).
static int compCount;
static int swapCount;
public void mergeSort(int[] a, int howMany) {
if (a.length >= 2) {
// split array into two halves
int[] left = Arrays.copyOfRange(a, 0, a.length/2);
int[] right = Arrays.copyOfRange(a, a.length/2, a.length);
// sort the two halves
mergeSort(left,howMany);
mergeSort(right, howMany);
// merge the sorted halves into a sorted whole
merge(a, left, right);
}
swapCount++;
}
// Merges the left/right elements into a sorted result.
// Precondition: left/right are sorted
public static void merge(int[] result, int[] left,
int[] right) {
int i1 = 0; // index into left array
int i2 = 0; // index into right array
for (int i = 0; i < result.length; i++) {
compCount++;
if (i2 >= right.length ||
(i1 < left.length && left[i1] <= right[i2])) {
result[i] = left[i1]; // take from left
i1++;
//swapCount++;
} else {
result[i] = right[i2]; // take from right
i2++;
//swapCount++;
}
}
System.out.println("merge sort " + compCount + " " + swapCount);
}
}

Related

Why doesn't the binary search method work if the array is sorted in descending order?

The binary search method is used to find out values from the sorted array which it is not doing. I know the issue involves sorting it in descending order but It won't work can someone help me figure out the issue.
The descending method is a selection sort if it helps.
I think the issue is with the method only being able to find the value form an ascending order array but don't know how to make it work on a descending order array
package project;
import java.io.*;
import java.util.*;
import java.util.Scanner;
public class project {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int Size;//size of the array
int order; // ascending or descending order
System.out.println("Put in the amount of expenses you have");
Size = sc.nextInt();//User input for the amount of expenses
System.out.println("put in all your expenses");
int userInput[] = new int[Size];// what the users expenses are
for (int i = 0; i < userInput.length; i++)//This loop is for if the i value is smaller than user input then put in more values to complete the array
userInput[i] = sc.nextInt();
System.out.println("do you want it ascending or descending order. If you want it in ascending press 1 or if you want descending press 2");
order = sc.nextInt();// select if they wanted it in ascending or descending order
System.out.print("expenses not sorted : ");
printExpenses(userInput);
if (order == 1) {
expensesAscending(userInput);// If order is equal to one then sort in ascending else if it is equal to 2 then order it descending
} else if (order == 2) {
expensedescending(userInput);
}
int ans = binarySearch(userInput, 0, Size, 10);
System.out.println(ans);
if(ans == -1)
System.out.println("Key not found");
else
System.out.println("Key found at position " + (ans));
}
public static void printExpenses(int[] arr) {
// this is were it is printed
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i] + "$");
}
}
public static void expensedescending(int arr[]) {
// This is were the selection sort starts
int N = arr.length;
for (int i = 0; i < N; i++) {
int small = arr[i];
int pos = i;
for (int j = i + 1; j < N; j++) {
if (arr[j] > small) {
small = arr[j];
pos = j;
}
}
int temp = arr[pos];
arr[pos] = arr[i];
arr[i] = temp;
System.out.println(": ");
// Printing array after pass
printExpenses(arr);
}
}
public static void expensesAscending(int arr[]) {
int N = arr.length;
for (int i = 1; i < N; i++) {
int j = i - 1;
int temp = arr[i];
while (j >= 0 && temp < arr[j]) {
arr[j + 1] = arr[j];
j--;
;
}
arr[j + 1] = temp;
System.out.println(": ");
// Printing array after pass
printExpenses(arr);
}
}
static int binarySearch(int[] array, int left, int right, int key) {
if (left > right) {
return -1;
}
int mid = (left + right) / 2;
if (array[mid] == key) {
return mid;
}
if (array[mid] > key) {
return binarySearch(array, left, mid - 1, key);
}
return binarySearch(array, mid + 1, right, key);
}
}
You are trying binary search of a Ascending order sorted array on a Descending order sorted array. Change your method header and method body like this:
int ans = binarySearch(userInput, 0, Size, 10,order); // order you got from user
Now this Binary search will work on both Ascending and Descending order of array:
static int binarySearch(int[] array, int left, int right, int key,int sorting)
{
if (left > right)
{
return -1;
}
int mid = (left + right) / 2;
if (array[mid] == key)
{
return mid;
}
if(sorting == 2) // if array is sorted in descending order
{
if (array[mid] > key)
{
return binarySearch(array, mid+1, right, key,sorting );
}
return binarySearch(array, left, mid-1, key,sorting );
}
else // if array is sorted in ascnending order
{
if (array[mid] > key)
{
return binarySearch(array, left, mid - 1, key,sorting );
}
return binarySearch(array, mid + 1, right, key,sorting );
}
}
The problem is you need to search in the upper half If the key is less than the mid element. Else search in the lower half.
static int binarySearch(int[] array, int left, int right, int key) {
if (left > right) {
return -1;
}
int mid = (left + right) / 2;
if (array[mid] == key) {
return mid;
}
if (array[mid] > key) {
return binarySearch(array, mid + 1, right, key);
}
return binarySearch(array, left, mid - 1, key);
}

Java: How to change an Integer array parameter from inside a method

I have a programming assignment where I have to make a method that takes in an array that is split up into subarrays, and merge them. My method works fine but the Integer array that is passed in 'a' is only changed from inside the method, and remains unchanged from the outside.
After looking around on google, I have concluded that it is impossible to change an array parameter, but then I thought about Arrays.sort which does end up changing the array parameter from outside of the method.
How do I change the array that is passed in as a parameter so that it stays changed outside of the method?
/**
* Merge three sorted arrays with these ranges [lo..mid1], [mid1+1..mid2], [mid2+1..hi] into one sorted array.
* Array a has the original input and final sorted input.
* Array aux is the auxiliary array.
*/
public static void Merge(Integer[] a, int lo, int mid1, int mid2, int hi, Integer[] aux) {
int[] arr1 = copyArray(a, lo, mid1);
int[] arr2 = copyArray(a, mid1+1, mid2);
int[] arr3 = copyArray(a, mid2+1, hi);
int i = 0, j = 0, k = 0, idx = 0;
while(i < arr1.length-1 || j < arr2.length-1 || k < arr3.length-1) {
// arr1[i] is the smallest
if(arr1[i] <= arr2[j] && arr1[i] <= arr3[k]) {
System.out.println("i is bigger at " + arr1[i]);
aux[idx] = arr1[i];
i++;
}
// arr2[j] is the smallest
else if(arr2[j] <= arr1[i] && arr2[j] <= arr3[k]) {
System.out.println("j is bigger at " + arr2[j]);
aux[idx] = arr2[j];
j++;
}
// arr3[k] is the smallest
else if(arr3[k] <= arr2[j] && arr3[k] <= arr1[i]) {
System.out.println("k is bigger at " + arr3[k]);
aux[idx] = arr3[k];
k++;
}
idx++;
}
System.out.println(Arrays.toString(arr1));
System.out.println(Arrays.toString(arr2));
System.out.println(Arrays.toString(arr3));
System.out.println(Arrays.toString(aux));
a = aux;
a[0] = 900;
System.out.println("a from Merge method: " + Arrays.toString(a));
}
You have to iterate over your aux array and assign the value to the 'a' array:
a[i] = aux[i]
Or return your merged array as result.
public static int[] Merge(int[] a, int mid1, ...){
// do something
return a;
}
You can change void to int[][] and then return the arrays
public static int[][] Merge(arguments) {
//Do math
int[][] result = new int[][] {arr1, arr2, arr3};
return result;
}
Then, outside of the method you call It like below:
int[][] integerArray = Merge(arguments);
arr1 = integerArray[0];
arr2 = integerArray[1];
arr3 = integerArray[2];

Inversion count for 100000 integers using merge sort reading from a text file. But not able to get the correct inversion count

Below is the code I have written to read the 100000 integers from a text file and to sort them using merge sort.
Issues:
The sorted integers are getting displayed in correct order only in the eclipse console, but when I try to write them to an output text file the order is getting changed.
The inversion count value of 100000 integers from the given text file should be around 2407905288 as per my knowledge but I am getting a value of 8096.
Kindly do help me fix these two issues. thank you so much in advance.
package com.inversioncount;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class inversioncount {
public static int mergeSort(Integer[] arr, int array_size) {
int temp[] = new int[array_size];
return _mergeSort(arr, temp, 0, array_size - 1);
}
/* An auxiliary recursive method that sorts the input array and
returns the number of inversions in the array. */
public static int _mergeSort(Integer arr[], int[] temp, int left, int right) {
int mid, count = 0;
if (right > left) {
/* Divide the array into two parts and call _mergeSortAndCountInv()
for each of the parts */
mid = (right + left) / 2;
/* Inversion count will be sum of inversions in left-part, right-part
and number of inversions in merging */
count = _mergeSort(arr, temp, left, mid);
count += _mergeSort(arr, temp, mid + 1, right);
/* Merge the two parts */
count += merge(arr, temp, left, mid + 1, right);
}
return count;
}
/* This method merges two sorted arrays and returns inversion count in
the arrays.*/
static int merge(Integer arr[], int temp[], int left, int mid, int right) {
int x, y, z;
int count = 0;
x = left; /* i is index for left subarray*/
y = mid; /* j is index for right subarray*/
z = left; /* k is index for resultant merged subarray*/
while ((x <= mid - 1) && (y <= right)) {
if (arr[x] <= arr[y]) {
temp[z++] = arr[x++];
} else {
temp[z++] = arr[y++];
count = count + (mid - x);
}
}
/* Copy the remaining elements of left subarray
(if there are any) to temp*/
while (x <= mid - 1)
temp[z++] = arr[x++];
/* Copy the remaining elements of right subarray
(if there are any) to temp*/
while (y <= right)
temp[z++] = arr[y++];
/* Copy back the merged elements to original array*/
for (x = left; x <= right; x++)
arr[x] = temp[x];
return count;
}
// Driver method to test the above function
public static void main(String[] args) throws FileNotFoundException {
try {
BufferedReader br = new BufferedReader(new FileReader("IntegerArray.txt"));
List<Integer> lines = new ArrayList<Integer>();
String line;
while ((line = br.readLine()) != null) {
lines.add(Integer.parseInt(line));
}
br.close();
Integer[] inputArray = lines.toArray(new Integer[lines.size()]);
inversioncount.mergeSort(inputArray, inputArray.length - 1);
System.out.println("Number of inversions are " + mergeSort(inputArray, inputArray.length));
//BufferedWriter writer = null;
//writer = new BufferedWriter(new FileWriter("OutputLatest.txt"));
for (Integer i : inputArray) {
System.out.println(i);
// Writing sorted lines into output file
//writer.write(i);
//writer.newLine();
}
} catch (IOException ie) {
System.out.print(ie.getMessage());
}
}
}
Your recursive calls are from left to mid and mid+1 to right as seen here:
count = _mergeSort(arr, temp, left, mid);
count += _mergeSort(arr, temp, mid+1, right);
Therefore the correct merge function call should be:
count += merge(arr, temp, left, mid, right);
There are also some mistakes in merge definition. Let me point them out to you:
x should vary from left to mid. y should vary from mid+1 to right
x = left; /* i is index for left subarray*/
y = mid+1; /* j is index for right subarray*/
z = left; /* k is index for resultant merged subarray*/
while ((x <= mid) && (y <= right))
Also the inversion count is actually count = count + (mid - x) + 1;. You can figure that out by taking a small array of 5 elements.
Also the indices for temp always start from 0.
Therefore the correct merge function is:
static int merge(Integer arr[], int temp[], int left, int mid, int right)
{
int x, y, z ;
int count = 0;
x = left; /* i is index for left subarray*/
y = mid+1; /* j is index for right subarray*/
z = 0; /* k is index for resultant merged subarray*/
while ((x <= mid) && (y <= right))
{
if (arr[x] <= arr[y])
{
temp[z++] = arr[x++];
}
else
{
temp[z++] = arr[y++];
count = count + (mid - x) + 1;
}
}
/* Copy the remaining elements of left subarray
(if there are any) to temp*/
while (x <= mid)
temp[z++] = arr[x++];
/* Copy the remaining elements of right subarray
(if there are any) to temp*/
while (y <= right)
temp[z++] = arr[y++];
/*Copy back the merged elements to original array*/
z = 0;
for (x=left; x <= right; x++)
arr[x] = temp[z++];
return count;
}
This is task from Hackerrank/Practice/Tutorials/Cracking the Coding Interview/Merge Sort: Counting Inversions:
static long mergeSort(int[] arr, int l, int r) {
long swaps = 0;
if (l < r) {
int p = (l + r) / 2;
swaps += mergeSort(arr, l, p);
swaps += mergeSort(arr, p + 1, r);
// Merge
int[] la = Arrays.copyOfRange(arr, l, p + 1);
int[] ra = Arrays.copyOfRange(arr, p + 1, r + 1);
int i = l, lai = 0, rai = 0;
while (lai < la.length && rai < ra.length) {
if (ra[rai] < la[lai]) {
arr[i++] = ra[rai++];
swaps += la.length - lai;
} else {
arr[i++] = la[lai++];
}
}
while (lai < la.length) {
arr[i++] = la[lai++];
}
while (rai < ra.length) {
arr[i++] = ra[rai++];
}
}
return swaps;
}
Use long instead of int for inv_count. Also, change the return type from int to long.
As with the above solution, int is overflowing.
There are multiple issues in your code:
you use type int to compute the number of inversions. The maximum value for type int is 2147483647. You should instead use type long which can handle values up to 9223372036854775807.
you call mergeSort twice in the main() method, with conflicting array sizes:
inversioncount.mergeSort(inputArray, inputArray.length - 1);
System.out.println("Number of inversions are " + mergeSort(inputArray, inputArray.length));
You should instead call it just once with the correct length:
long inversion_count = mergeSort(inputArray, inputArray.length);
System.out.println("Number of inversions is " + inversion_count + "\n");
It is confusing to specify right as the index of the last element in the slice. A much more regular and idiomatic approach uses right as the index of the first element past the end of the slice.
Here is a corrected version:
package com.inversioncount;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class inversioncount {
public static long mergeSort(Integer[] arr, int array_size) {
int temp[] = new int[array_size];
return _mergeSort(arr, temp, 0, array_size);
}
/* An auxiliary recursive method that sorts a slice of the input array and
returns the number of inversions. */
static long _mergeSort(Integer arr[], int[] temp, int left, int right) {
long count = 0;
if (right - left > 1) {
/* Divide the array into two parts and call _mergeSort() for each of the parts */
int mid = (right + left + 1) / 2;
/* Inversion count will be sum of inversions in left-part, right-part
and number of inversions in merging */
count += _mergeSort(arr, temp, left, mid);
count += _mergeSort(arr, temp, mid, right);
/* Merge the two parts */
count += merge(arr, temp, left, mid, right);
}
return count;
}
/* This method merges two sorted slices of the array and returns the inversion count */
static long merge(Integer arr[], int temp[], int left, int mid, int right) {
long count = 0;
int x = left; /* x is index for left subarray */
int y = mid; /* y is index for right subarray */
int z = left; /* z is index for resultant merged subarray */
while (x < mid && y < right) {
if (arr[x] <= arr[y]) {
temp[z++] = arr[x++];
} else {
temp[z++] = arr[y++];
count += mid - x;
}
}
/* Copy the remaining elements of left subarray if any */
while (x < mid)
temp[z++] = arr[x++];
/* Copy the remaining elements of right subarray if any */
while (y < right)
temp[z++] = arr[y++];
/* Copy back the merged elements to original array */
for (x = left; x < right; x++)
arr[x] = temp[x];
return count;
}
// Driver method to test the above function
public static void main(String[] args) throws FileNotFoundException {
try {
BufferedReader br = new BufferedReader(new FileReader("IntegerArray.txt"));
List<Integer> lines = new ArrayList<Integer>();
String line;
while ((line = br.readLine()) != null) {
lines.add(Integer.parseInt(line));
}
br.close();
Integer[] inputArray = lines.toArray(new Integer[lines.size()]);
long inversion_count = mergeSort(inputArray, inputArray.length);
System.out.println("Number of inversions is " + inversion_count + "\n");
//BufferedWriter writer = null;
//writer = new BufferedWriter(new FileWriter("OutputLatest.txt"));
for (Integer i : inputArray) {
System.out.println(i);
// Writing sorted lines into output file
//writer.write(i);
//writer.newLine();
}
} catch (IOException ie) {
System.out.print(ie.getMessage());
}
}
}

big difference in time between two Implementation of quick Sort

I have two implementation of quick sort the first one uses a median of (fist ,middle , last ) as pivot and the second uses the middle element as pivot
the first Implementation :
public class quickMedian {
public void sort(int array[])
// pre: array is full, all elements are non-null integers
// post: the array is sorted in ascending order
{
quickSort(array, 0, array.length - 1); // quicksort all the elements in the array
}
public void quickSort(int array[], int start, int end)
{
int i = start; // index of left-to-right scan
int k = end; // index of right-to-left scan
if (end - start >= 1) // check that there are at least two elements to sort
{
if (array[start+(end-start)/2]>array[end]){
swap(array,start+(end-start)/2, end);
}
if (array[start]>array[end]){
swap(array,start, end);
}
if (array[start+(end-start)/2]>array[start]){
swap(array, start+(end-start)/2, start);
}
int pivot = array[start]; // set the pivot as the first element in the partition
while (k > i) // while the scan indices from left and right have not met,
{
while (array[i] <= pivot && i <= end && k > i) // from the left, look for the first
i++; // element greater than the pivot
while (array[k] > pivot && k >= start && k >= i) // from the right, look for the first
k--; // element not greater than the pivot
if (k > i) // if the left seekindex is still smaller than
swap(array, i, k); // the right index, swap the corresponding elements
}
swap(array, start, k); // after the indices have crossed, swap the last element in // the left partition with the pivot
quickSort(array, start, k - 1); // quicksort the left partition
quickSort(array, k + 1, end); // quicksort the right partition
}
else // if there is only one element in the partition, do not do any sorting
{
return; // the array is sorted, so exit
}
}
public void swap(int array[], int index1, int index2)
// pre: array is full and index1, index2 < array.length
// post: the values at indices 1 and 2 have been swapped
{
int temp = array[index1]; // store the first value in a temp
array[index1] = array[index2]; // copy the value of the second into the first
array[index2] = temp; // copy the value of the temp into the second
}
}
The second Implementation :
public class quickSort {
private int array[];
private int length;
public void sort(int[] inputArr) {
if (inputArr == null || inputArr.length == 0) {
return;
}
this.array = inputArr;
length = inputArr.length;
quickSorter(0, length - 1);
}
private void quickSorter(int lowerIndex, int higherIndex) {
int i = lowerIndex;
int j = higherIndex;
// calculate pivot number, I am taking pivot as middle index number
int pivot = array[lowerIndex+(higherIndex-lowerIndex)/2];
// Divide into two arrays
while (i <= j) {
/**
* In each iteration, we will identify a number from left side which
* is greater then the pivot value, and also we will identify a number
* from right side which is less then the pivot value. Once the search
* is done, then we exchange both numbers.
*/
while (array[i] < pivot) {
i++;
}
while (array[j] > pivot) {
j--;
}
if (i <= j) {
exchangeNumbers(i, j);
//move index to next position on both sides
i++;
j--;
}
}
// call quickSort() method recursively
if (lowerIndex < j)
quickSorter(lowerIndex, j);
if (i < higherIndex)
quickSorter(i, higherIndex);
}
private void exchangeNumbers(int i, int j) {
int temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
To obtain the median we need to do extra steps on each recursion which may increase the time a little bit (if the array is totally random) .
I am testing these two classes on an array of size N=10,000,000 randomly generated and I have done the test many times the first Implementation takes around 30 seconds and the second takes around 4 seconds
so this is obviously not caused by the extra overhead to get the median of three numbers .
There must be something wrong with the first implementation, what is it ?
here is the testing code :
public static void main(String[] args) {
File number = new File ("f.txt");
final int size = 10000000;
try{
// quickSort s = new quickSort();
quickMedian s = new quickMedian();
writeTofile(number, size);
int [] arr1 =readFromFile(number, size);
long a=System.currentTimeMillis();
s.sort(arr1);
long b=System.currentTimeMillis();
System.out.println("quickSort: "+(double)(b-a)/1000);
}catch (Exception ex){ex.printStackTrace();}
}

Using quicksort on a string array

I'm a programming student and rather than post the whole assignment I'll just ask for help solving what I've tried for hours now to understand. I'm tasked with sorting an array of strings using the quicksort method. Everything else I've been tasked with as part of this problem is fine but when I tested the sorting method by printing out the String Array, it's completely jumbled up without any seeming rhyme or reason. Please help me pinpoint the error in my code, or the several glaring errors I've overlooked. The array of strings provided is this list of 65 names: http://pastebin.com/jRrgeV1E and the method's code is below:
private static void quickSort(String[] a, int start, int end)
{
// index for the "left-to-right scan"
int i = start;
// index for the "right-to-left scan"
int j = end;
// only examine arrays of 2 or more elements.
if (j - i >= 1)
{
// The pivot point of the sort method is arbitrarily set to the first element int the array.
String pivot = a[i];
// only scan between the two indexes, until they meet.
while (j > i)
{
// from the left, if the current element is lexicographically less than the (original)
// first element in the String array, move on. Stop advancing the counter when we reach
// the right or an element that is lexicographically greater than the pivot String.
while (a[i].compareTo(pivot) < 0 && i <= end && j > i){
i++;
}
// from the right, if the current element is lexicographically greater than the (original)
// first element in the String array, move on. Stop advancing the counter when we reach
// the left or an element that is lexicographically less than the pivot String.
while (a[j].compareTo(pivot) > 0 && j >= start && j >= i){
j--;
}
// check the two elements in the center, the last comparison before the scans cross.
if (j > i)
swap(a, i, j);
}
// At this point, the two scans have crossed each other in the center of the array and stop.
// The left partition and right partition contain the right groups of numbers but are not
// sorted themselves. The following recursive code sorts the left and right partitions.
// Swap the pivot point with the last element of the left partition.
swap(a, start, j);
// sort left partition
quickSort(a, start, j - 1);
// sort right partition
quickSort(a, j + 1, end);
}
}
/**
* This method facilitates the quickSort method's need to swap two elements, Towers of Hanoi style.
*/
private static void swap(String[] a, int i, int j)
{
String temp = a[i];
a[i] = a[j];
a[j] = temp;
}
Ok, i was mistaken that it would work and found your tiny mistake.
Take a look at wikipedias pseudo code
You will notice that your conditions in the while loop are causing the error
if you change (a[i].compareTo(pivot) < 0 && i <= end && j > i) and (a[j].compareTo(pivot) > 0 && j >= start && j >= i) to
(a[i].compareTo(pivot) <= 0 && i < end && j > i) and (a[j].compareTo(pivot) >= 0 && j > start && j >= i).
Thought this would help for those who seek for a string sorting algorithm based on quick sorting method.
public class StringQuickSort {
String names[];
int length;
public static void main(String[] args) {
StringQuickSort sorter = new StringQuickSort();
String words[] = {"zz", "aa", "cc", "hh", "bb", "ee", "ll"}; // the strings need to be sorted are put inside this array
sorter.sort(words);
for (String i : words) {
System.out.print(i);
System.out.print(" ");
}
}
void sort(String array[]) {
if (array == null || array.length == 0) {
return;
}
this.names = array;
this.length = array.length;
quickSort(0, length - 1);
}
void quickSort(int lowerIndex, int higherIndex) {
int i = lowerIndex;
int j = higherIndex;
String pivot = this.names[lowerIndex + (higherIndex - lowerIndex) / 2];
while (i <= j) {
while (this.names[i].compareToIgnoreCase(pivot) < 0) {
i++;
}
while (this.names[j].compareToIgnoreCase(pivot) > 0) {
j--;
}
if (i <= j) {
exchangeNames(i, j);
i++;
j--;
}
}
//call quickSort recursively
if (lowerIndex < j) {
quickSort(lowerIndex, j);
}
if (i < higherIndex) {
quickSort(i, higherIndex);
}
}
void exchangeNames(int i, int j) {
String temp = this.names[i];
this.names[i] = this.names[j];
this.names[j] = temp;
}
}

Categories

Resources