@Test(timeout = TIMEOUT)
  public void testRadixSort() {
    int[] arrZero = new int[0];
    arrZero = Sorting.radixSort(arrZero);
    assertEquals(0, arrZero.length);
    int[] arrOne = new int[1];
    arrOne[0] = 4;
    arrOne = Sorting.radixSort(arrOne);
    assertEquals(1, arrOne.length);
    assertEquals(4, arrOne[0]);

    Random rand = new Random();
    for (int i = 0; i < 50; i++) {
      int arrlen = rand.nextInt(1000) + 2;
      int[] arrMany = new int[arrlen];
      int[] arrManySorted = new int[arrlen];
      for (int j = 0; j < arrlen; j++) {
        arrMany[j] = rand.nextInt(200) - 90;
      }
      System.arraycopy(arrMany, 0, arrManySorted, 0, arrlen);
      Arrays.sort(arrManySorted);
      arrMany = Sorting.radixSort(arrMany);
      assertArrayEquals(arrManySorted, arrMany);
    }
  }
Exemple #2
0
  private SearchRequestBuilder standardSearchRequest(
      String query, Set<String> indices, int limit, int offset, TimeRange range, Sorting sort)
      throws IndexHelper.InvalidRangeFormatException {
    if (query == null || query.trim().isEmpty()) {
      query = "*";
    }

    SearchRequestBuilder srb = c.prepareSearch();
    srb.setIndices(indices.toArray(new String[] {}));

    if (query.trim().equals("*")) {
      srb.setQuery(matchAllQuery());
    } else {
      QueryStringQueryBuilder qs = queryString(query);
      qs.allowLeadingWildcard(server.getConfiguration().isAllowLeadingWildcardSearches());
      srb.setQuery(qs);
    }

    srb.setFrom(offset);

    if (limit > 0) {
      srb.setSize(limit);
    }

    if (range != null) {
      srb.setFilter(IndexHelper.getTimestampRangeFilter(range));
    }

    if (sort != null) {
      srb.addSort(sort.getField(), sort.asElastic());
    }

    return srb;
  }
  @Test(timeout = TIMEOUT)
  public void testQuickSort() {
    DValue[] arrZero = new DValue[0];
    Sorting.quickSort(arrZero, new BasicComparator(), new Random());
    assertEquals(0, arrZero.length);
    DValue[] arrOne = new DValue[1];
    arrOne[0] = new DValue(4, 0);
    Sorting.quickSort(arrOne, new BasicComparator(), new Random());
    assertEquals(1, arrOne.length);
    assertEquals(4, arrOne[0].val.intValue());

    Random rand = new Random();
    HashMap<Integer, Integer> values = new HashMap<Integer, Integer>();
    for (int i = 0; i < 50; i++) {
      int arrlen = rand.nextInt(1000) + 2;
      DValue[] arrMany = new DValue[arrlen];
      DValue[] arrManySorted = new DValue[arrlen];
      for (int j = 0; j < arrlen; j++) {
        arrMany[j] = new DValue(rand.nextInt(200) - 90, 0);
        if (values.containsKey(arrMany[j].val)) {
          arrMany[j].count = values.get(arrMany[j].val) + 1;
          values.put(arrMany[j].val, arrMany[j].count);
        } else {
          values.put(arrMany[j].val, 0);
        }
      }
      System.arraycopy(arrMany, 0, arrManySorted, 0, arrlen);
      Arrays.sort(arrManySorted);
      Sorting.quickSort(arrMany, new BasicComparator(), new Random());
      assertArrayEquals(arrManySorted, arrMany);
    }
  }
Exemple #4
0
  public static void main(String[] args) {
    ArrayList<Long> estimatedBubbleTimeArray = new ArrayList<>();
    ArrayList<Long> estimatedBubbleTimeArrayList = new ArrayList<>();
    long estimatedInsertionTime = 0;
    long estimatedSelectionTime = 0;

    // Base for measuring time in sorting with Strings
    for (int i = 0; i < 10; i++) {
      String[] stringArrayBubble = {
        "Erna", "Elly", "Laurits", "Bertha", "Christian", "August", "Marius", "John", "Tove", "Poul"
      };

      long startTimeArray = System.nanoTime();
      Sorting.<String>bubbleSort(stringArrayBubble);
      estimatedBubbleTimeArray.add(System.nanoTime() - startTimeArray);

      ArrayList<String> stringArrayListBubble = new ArrayList<>();
      stringArrayListBubble.add("Erna");
      stringArrayListBubble.add("Elly");
      stringArrayListBubble.add("Laurits");
      stringArrayListBubble.add("Bertha");
      stringArrayListBubble.add("Christian");
      stringArrayListBubble.add("August");
      stringArrayListBubble.add("Marius");
      stringArrayListBubble.add("John");
      stringArrayListBubble.add("Tove");
      stringArrayListBubble.add("Poul");

      long startTimeArrayList = System.nanoTime();
      Sorting.<String>bubbleSort(stringArrayListBubble);
      estimatedBubbleTimeArrayList.add(System.nanoTime() - startTimeArrayList);
    }
    System.out.println(estimatedBubbleTimeArray);
    System.out.println(estimatedBubbleTimeArrayList);

    String[] stringArrayBubble = {
      "Mark",
      "Nikolaj",
      "Henrik",
      "Tommy",
      "Jesper",
      "Jonas",
      "Margrethe",
      "Liv",
      "Birgitte",
      "Anders"
    };
    long startTimeArrayList = System.nanoTime();
    Sorting.<String>bubbleSort(stringArrayBubble);
    System.out.println(System.nanoTime() - startTimeArrayList);
  }
  public static void main(String[] args) {
    Sorting sorting = new Sorting();
    int[] intArray = {3, 6, 7, 9, 1, 2, 4, 5, 8, 0};
    System.out.print("Array Elements before sorting : ");
    sorting.printArrayElements(intArray);

    // System.out.println("Array Elements after SELECTION sorting : ");
    // sorting.selectionSort(intArray);

    System.out.println("Array Elements after BUBBLE sorting : ");
    sorting.bubbleSort(intArray);

    // System.out.println("Array Elements after INSERTION sorting : ");
    // sorting.insertionSort(intArray);
  }
Exemple #6
0
  /** @param args */
  public static void main(String[] args) {
    Datos[] numeros = new Datos[3000]; // creamos una lista de objetos de tipo Datos
    try {
      // indicamos la direccion y el nombre del archivo a leer
      FileReader lector =
          new FileReader(
              "D:\\Users\\MARLON\\Documents\\UVG\\4to Semestre\\Algoritmos y Estructuras de Datos\\Hoja3\\src\\Hoja3\\texto.txt");
      // El contenido de lector se guarda en un BufferedReader
      BufferedReader contenido = new BufferedReader(lector);

      // instanciamos cada objeto y le colocamos el valor que lee del txt
      for (int a = 0; a <= 2999; a++) {
        numeros[a] = new Datos(Integer.parseInt(contenido.readLine()));
      }
      // usamos un algoritmo de sorting para ordenar la lista
      Sorting.radixSort(numeros);

      // recorremos cada objeto de la lista y luego extraemos su valor para imprimirlo
      for (Datos numero : numeros) {
        System.out.println(numero.getDato());
      }

      lector.close(); // cerramos el lector del archivo
    }
    // Si se causa un error al leer cae aqui
    catch (Exception e) {
      System.out.println("Error al leer el archivo.");
    }
  }
  @Test
  public void testLongIntSort() {
    int N = 10000000;
    long[] a = new long[N];

    Random r = new Random(260379);

    long achecksum = 0;
    for (int i = 0; i < N; i++) {
      a[i] = Math.abs(r.nextLong() % (N / 4));
      achecksum += a[i];
    }

    long st = System.currentTimeMillis();
    int[] b = Sorting.radixSortWithIndex(a);
    System.out.println("Native radix sorting took: " + (System.currentTimeMillis() - st) + " ms");

    long idxsum = 0;
    long asum = 0;
    for (int i = 0; i < N - 1; i++) {
      assertTrue((a[i] < a[i + 1]) || (a[i] == a[i + 1] && b[i] <= b[i + 1]));
      assertTrue(b[i] >= 0 && b[i] < a.length);
      idxsum += b[i];
      asum += a[i];
    }
    asum += a[N - 1];
    idxsum += b[N - 1];
    assertEquals(achecksum, asum);
    assertEquals((long) N * ((long) N - 1) / 2, idxsum);
  }
  public static void main(String[] args) {

    int[] val = new int[] {25, 17, 5, 13, 1};
    val = Sorting.selectionSort(val);

    for (int i = 0; i < val.length; i++) {
      System.out.print(val[i] + ",");
    }
  }
Exemple #9
0
  public void testQuickSort() {
    long before, after;
    System.out.println("Testing quick sort.");
    for (int i = 0; i < 5; i++) {
      before = System.currentTimeMillis();
      sort.quicksort(lists[i]);
      after = System.currentTimeMillis();

      System.out.println(after - before);
    }
  }
  @Test
  public void test_Wallets_Transactions_SortByCreationDate() throws Exception {
    Wallet wallet = this.getJohnsWallet();

    // create two payin objects
    this.getJohnsPayInCardWeb();
    this.holdOn(2);
    this.getNewPayInCardWeb();
    Sorting sorting = new Sorting();
    sorting.addField("CreationDate", SortDirection.desc);
    Pagination pagination = new Pagination(1, 20);
    FilterTransactions filter = new FilterTransactions();
    filter.Type = TransactionType.PAYIN;

    List<Transaction> transactions =
        this._api.Wallets.getTransactions(wallet.Id, pagination, filter, sorting);

    assertNotNull(transactions);
    assertTrue(transactions.size() > 1);
    assertTrue(transactions.get(0).CreationDate > transactions.get(1).CreationDate);
  }
Exemple #11
0
  public static void main(String[] args) {
    int N = 10000000;
    int K = 1;
    Integer[] newArray = new Integer[N];
    StopWatch sw = new StopWatch();
    while (K <= N) {
      newArray = createArray(N, K);
      Sorting obj = new Sorting(newArray);
      newArray = (Integer[]) obj.selectionsort();

      System.out.println("The time needed for K=" + K + " is " + sw.elapsedTime());
      K = K * 2;
      System.out.println(
          "The number of comparisons is "
              + obj.compareCount()
              + "; "
              + "The number of exchanges is "
              + obj.exchangeCount()
              + "; "
              + "The number of copies is "
              + obj.copyCount()
              + "; ");
    }
  }
  @Test
  public void testSortWithValues() {
    long[] ids = new long[] {7, 4, 5, 8, 2, 9};
    float[] valuef = new float[] {7.0f, 4.0f, 5.0f, 8.0f, 2.0f, 9.0f};
    byte[] valuedat = new byte[4 * valuef.length];

    FloatConverter floatConv = new FloatConverter();
    for (int i = 0; i < valuef.length; i++) {
      byte[] tmp = new byte[4];
      floatConv.setValue(tmp, valuef[i]);
      System.arraycopy(tmp, 0, valuedat, i * 4, 4);
    }

    Sorting.sortWithValues(ids, valuedat, 4);

    for (int i = 0; i < valuef.length; i++) {
      byte[] tmp = new byte[4];
      System.arraycopy(valuedat, i * 4, tmp, 0, 4);
      float f = floatConv.getValue(tmp);
      assertEquals(ids[i] * 1.0f, f);
      assertTrue(i == 0 || ids[i] > ids[i - 1]);
    }
  }
  @Test
  public void testMergeLarge() {
    long[] src1 = new long[100000];
    long[] dst1 = new long[100000];

    for (int j = 0; j < src1.length; j++) {
      src1[j] = (j / 4) * 9999;
      dst1[j] = src1[j] + 1;
    }

    byte[] values1 = new byte[4 * src1.length];
    int checksum = 0;
    for (int i = 0; i < src1.length; i++) {
      byte[] tmp = new byte[4];
      intc.setValue(tmp, (int) dst1[i]);
      System.arraycopy(tmp, 0, values1, i * 4, 4);
      checksum += src1[i];
    }

    /* Array 2 */
    long[] src2 = new long[153003];
    long[] dst2 = new long[153003];
    for (int j = 0; j < src2.length; j++) {
      src2[j] = (j / 7) * 9999;
      dst2[j] = src2[j] + 1;
    }
    byte[] values2 = new byte[4 * src2.length];
    for (int i = 0; i < src2.length; i++) {
      byte[] tmp = new byte[4];
      intc.setValue(tmp, (int) dst2[i]);
      System.arraycopy(tmp, 0, values2, i * 4, 4);
      checksum += src2[i];
    }

    /* Results */
    long[] ressrc = new long[src1.length + src2.length];
    long[] resdst = new long[src1.length + src2.length];
    byte[] resvals = new byte[values1.length + values2.length];

    Sorting.mergeWithValues(src1, dst1, values1, src2, dst2, values2, ressrc, resdst, resvals, 4);

    for (int j = 1; j < ressrc.length; j++) {
      assertTrue(
          (ressrc[j] >= ressrc[j - 1]
              || (ressrc[j - 1] == ressrc[j] && resdst[j] > resdst[j - 1])));
      if (ressrc[j] == ressrc[j - 1] && resdst[j] < resdst[j - 1]) {
        System.out.println("Mismatch: " + j);
        System.out.println(ressrc[j]);
        System.out.println(resdst[j - 1]);
        System.out.println(resdst[j]);

        assertTrue(resdst[j] > resdst[j - 1]);
      }

      byte[] tmp = new byte[4];
      System.arraycopy(resvals, j * 4, tmp, 0, 4);
      int x = intc.getValue(tmp);
      assertEquals(resdst[j], (long) x);
    }

    // Merge other direction
    Sorting.mergeWithValues(src2, dst2, values2, src1, dst1, values1, ressrc, resdst, resvals, 4);

    for (int j = 1; j < ressrc.length; j++) {
      assertTrue(
          (ressrc[j] >= ressrc[j - 1]
              || (ressrc[j - 1] == ressrc[j] && resdst[j] > resdst[j - 1])));

      if (ressrc[j] == ressrc[j - 1] && resdst[j] < resdst[j - 1]) {
        System.out.println("Mismatch: " + j);
        System.out.println(ressrc[j]);
        System.out.println(resdst[j - 1]);
        System.out.println(resdst[j]);

        assertTrue(resdst[j] > resdst[j - 1]);
      }
      byte[] tmp = new byte[4];
      System.arraycopy(resvals, j * 4, tmp, 0, 4);
      int x = intc.getValue(tmp);
      assertEquals(resdst[j], (long) x);
    }
  }
  @Test
  public void testMerge2WithValues() {

    /* Array 1 */
    long[] src1 = new long[] {1, 2, 4, 5, 5, 5, 10};
    long[] dst1 = new long[] {1001, 1002, 1004, 5, 4005, 5005, 1010};
    byte[] values1 = new byte[4 * src1.length];
    int checksum = 0;
    for (int i = 0; i < src1.length; i++) {
      byte[] tmp = new byte[4];
      intc.setValue(tmp, (int) dst1[i]);
      System.arraycopy(tmp, 0, values1, i * 4, 4);
      checksum += src1[i];
    }

    /* Array 2 */
    long[] src2 = new long[] {2, 3, 5, 8, 9, 11, 20};
    long[] dst2 = new long[] {2002, 2003, 2005, 2008, 2009, 2011, 2020};
    byte[] values2 = new byte[4 * src2.length];
    for (int i = 0; i < src2.length; i++) {
      byte[] tmp = new byte[4];
      intc.setValue(tmp, (int) dst2[i]);
      System.arraycopy(tmp, 0, values2, i * 4, 4);
      checksum += src2[i];
    }

    /* Results */
    long[] ressrc = new long[src1.length + src2.length];
    long[] resdst = new long[src1.length + src2.length];
    byte[] resvals = new byte[values1.length + values2.length];

    Sorting.mergeWithValues(src1, dst1, values1, src2, dst2, values2, ressrc, resdst, resvals, 4);

    for (int j = 1; j < ressrc.length; j++) {
      assertTrue(ressrc[j] >= ressrc[j - 1]);
      assertTrue(resdst[j] % 1000 == ressrc[j]);
      if (ressrc[j] == ressrc[j - 1]) assertTrue(resdst[j] > resdst[j - 1]);

      byte[] tmp = new byte[4];
      System.arraycopy(resvals, j * 4, tmp, 0, 4);
      int x = intc.getValue(tmp);
      assertEquals(resdst[j], (long) x);
    }

    // Merge other direction
    Sorting.mergeWithValues(src2, dst2, values2, src1, dst1, values1, ressrc, resdst, resvals, 4);

    for (int j = 1; j < ressrc.length; j++) {
      assertTrue(ressrc[j] >= ressrc[j - 1]);
      assertTrue(resdst[j] % 1000 == ressrc[j]);

      if (ressrc[j] == ressrc[j - 1]) assertTrue(resdst[j] > resdst[j - 1]);

      byte[] tmp = new byte[4];
      System.arraycopy(resvals, j * 4, tmp, 0, 4);
      int x = intc.getValue(tmp);
      assertEquals(resdst[j], (long) x);
    }

    /* Check that all values in */
    int sum = 0;
    for (int i = 0; i < ressrc.length; i++) sum += ressrc[i];
    assertEquals(checksum, sum);
  }
Exemple #15
0
  public float[] movLED(float[] LED, int[] x, int[] y, int box_size) {
    int limit = (int) x.length;
    int count1 = 0, count2 = 0;
    float est_LED1x, est_LED2x, est_LED1y, est_LED2y;
    // Arrays used for storing the usefull values for the LED updating.
    float[] LED1xi = new float[limit];
    float[] LED2xi = new float[limit];
    float[] LED1yi = new float[limit];
    float[] LED2yi = new float[limit];
    float[] LEDu = new float[5];

    int[] LED1 = new int[2];
    int[] LED2 = new int[2];
    LED1[0] = (int) LED[0];
    LED1[1] = (int) LED[1];
    LED2[0] = (int) LED[2];
    LED2[1] = (int) LED[3];

    // Instantiating descriptive object
    Descriptive simple = new Descriptive();
    Sorting sort = new Sorting();

    // Finding the "good" x's'
    for (int index = 0; index < x.length; index++) {
      // Finding the observations in the allowed region for the first LED
      if (x[index] >= LED1[0] - box_size
          & x[index] <= LED1[0] + box_size
          & y[index] >= LED1[1] - box_size
          & y[index] <= LED1[1] + box_size) {
        // if (y[index]>=LED1[1]-box_size & y[index]<=LED1[1]+box_size)
        // {
        LED1xi[count1] = x[index];
        LED1yi[count1] = y[index];
        count1++;
        // }
      }

      // Finding the observations in the allowed region for the second LED
      if (x[index] >= LED2[0] - box_size
          & x[index] <= LED2[0] + box_size
          & y[index] >= LED2[1] - box_size
          & y[index] <= LED2[1] + box_size) {
        //  if ()
        // {
        LED2xi[count2] = x[index];
        LED2yi[count2] = y[index];
        count2++;
        // }
      }
    }

    // Knowing the number of observations in each of the LED boxes we start
    // defining new array with the exact length so as to make it possible to
    // calculate the median.
    float[] LED1x = new float[count1];
    float[] LED2x = new float[count2];
    float[] LED1y = new float[count1];
    float[] LED2y = new float[count2];

    for (int index = 0; index < count1; index++) {
      LED1x[index] = LED1xi[index];
      LED1y[index] = LED1yi[index];
    }

    for (int index = 0; index < count2; index++) {
      LED2x[index] = LED2xi[index];
      LED2y[index] = LED2yi[index];
    }

    // Calculating the median of each LED box by sorting 4 arrays and then
    // finding the median
    LED1x = sort.heapSort(LED1x);
    est_LED1x = simple.Median(LED1x);
    LED2x = sort.heapSort(LED2x);
    est_LED2x = simple.Median(LED2x);
    LED1y = sort.heapSort(LED1y);
    est_LED1y = simple.Median(LED1y);
    LED2y = sort.heapSort(LED2y);
    est_LED2y = simple.Median(LED2y);

    // This creates the result array, where the elements 0 and 1 are the coordinates
    // for LED1 and elements 2 and 3 are the coordinates for LED2 and element 4 is the
    // distance between the two LED.

    LEDu[0] = (float) est_LED1x;
    LEDu[1] = (float) est_LED1y;
    LEDu[2] = (float) est_LED2x;
    LEDu[3] = (float) est_LED2y;
    LEDu[4] = (float) distance(LEDu);

    // Returning the LED positions in an array [5] where the last cell is the distance between the
    // LED
    return LEDu;
  }