Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author Aamir khan
- */
- public class NoDuplicates {
- public static void main(String[] args) {
- int[] a = new int[100];
- int[] b = new int[100];
- fillRandom(a);
- fillRandom(b);
- //lets sortBoth first
- new NoDuplicates().sortBoth(a, b);
- //now find equals
- boolean isEquals = equals(a, b);
- System.out.println("is a equals b: "+ isEquals);
- }
- /**
- * Returns <tt>true</tt> if the two specified arrays of ints are
- * <i>equal</i> 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 <tt>null</tt>.<p>
- *
- * @param a one array to be tested for equality
- * @param a2 the other array to be tested for equality
- * @return <tt>true</tt> if the two arrays are equal
- */
- public static boolean equals(int[] a, int[] a2) {
- if (a == a2) {
- return true;
- }
- if (a == null || a2 == null) {
- return false;
- }
- int length = a.length;
- if (a2.length != length) {
- return false;
- }
- for (int i = 0; i < length; i++) {
- if (a[i] != a2[i]) {
- return false;
- }
- }
- return true;
- }
- /**
- Fill Array with some random numbers
- */
- private static void fillRandom(int[] a) {
- for (int i = 0, len = a.length; i < len; i++)
- a[i] = new java.util.Random().nextInt();
- }
- /**
- * Sorts the specified array into ascending numerical order.
- *
- * <p>
- * Implementation note: The sorting algorithm is a Dual-Pivot Quicksort A
- * much better implementation can be found here
- * http://www.docjar.com/html/api/java/util/DualPivotQuicksort.java.html
- *
- * @param a the array to be sorted
- */
- public void sortBoth(int[] a, int[] b) {
- QuickSort quickSort = new QuickSort();
- quickSort.sort(a);
- quickSort.sort(b);
- }
- 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;
- quickSort(0, length - 1);
- }
- private void quickSort(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) {
- swap(i, j);
- //move index to next position on both sides
- i++;
- j--;
- }
- }
- // call quickSort() method recursively
- if (lowerIndex < j) {
- quickSort(lowerIndex, j);
- }
- if (i < higherIndex) {
- quickSort(i, higherIndex);
- }
- }
- private void swap(int i, int j) {
- int temp = array[i];
- array[i] = array[j];
- array[j] = temp;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement