AscendingSort.java

  1. package org.drip.zen.algorithm;

  2. public class AscendingSort {

  3.     static void CompareAndSwap (
  4.         int[] numberArray,
  5.         int leftLocation,
  6.         int rightLocation)
  7.     {
  8.         int leftNumber = numberArray[leftLocation];
  9.         int rightNumber = numberArray[rightLocation];

  10.         if (leftNumber > rightNumber)
  11.         {
  12.             numberArray[leftLocation] = rightNumber;
  13.             numberArray[rightLocation] = leftNumber;
  14.         }
  15.     }

  16.     static void BubbleMaximumToRight (
  17.         int[] numberArray,
  18.         int rightMostLocation)
  19.     {
  20.         for (int location = 0; location <= rightMostLocation; location = location + 1)
  21.         {
  22.             CompareAndSwap (numberArray, location, location + 1);
  23.         }
  24.     }

  25.     static void BubbleSort (
  26.         int[] numberArray)
  27.     {
  28.         for (int sweep = numberArray.length - 2; sweep >= 0; sweep = sweep - 1)
  29.         {
  30.             BubbleMaximumToRight (numberArray, sweep);
  31.         }
  32.     }

  33.     public static void main (
  34.         String[] input)
  35.     {
  36.         int[] unsortedNumberArray = {6, 1, 5, 7, 2, 8, 4, 3};

  37.         BubbleSort (unsortedNumberArray);

  38.         for (int i = 0; i < unsortedNumberArray.length; i = i + 1)
  39.         {
  40.             System.out.println (unsortedNumberArray[i]);
  41.         }
  42.     }
  43. }