Home > Enterprise >  Bubble sort String array and int array
Bubble sort String array and int array

Time:11-07

Here I want to make a bubble sort that compares all the names of type string in the array names in alphabetic order; like B and A ---> A and B. And when I do this change I also want to change the position of the itemsToSell array so all the right names have the right numbers next to them. I can't use java.util.Arrays; Any advice?

public static void printNames(String[] name, int[] itemsToSell, int[] amountBought) {
    boolean flag = true;

    while (flag) {
        flag = false;

        for (int j = 0; j < name.length - 1; j  ) {
            for (int i = j   1; i < name.length; i  ) {
                if (name[i].compareTo(name[j]) < 0) {
                    int tempTtem = itemsToSell[j];
                    itemsToSell[j] = itemsToSell[i];
                    itemsToSell[i] = tempTtem;

                    String temp = name[j];
                    name[j] = name[i];
                    name[i] = temp;

                }
            }

            System.out.println(name[j]   '\t'   itemsToSell[j]   "\t\t"   amountBought[j]);
        }
    }
}

CodePudding user response:

public class Main {

    public static void bubbleSort(String[] name, int[] itemsToSell) {
        while (true) {
            boolean sorted = moveRight(name, itemsToSell);
            sorted &= moveLeft(name, itemsToSell);

            if (sorted)
                break;
        }
    }

    private static boolean moveRight(String[] name, int[] itemsToSell) {
        boolean sorted = true;

        for (int i = 0, j = 1; j < name.length; i  , j  ) {
            if (name[i].compareTo(name[j]) > 0) {
                sorted = false;
                swap(itemsToSell, i, j);
                swap(name, i, j);
            }
        }

        return sorted;
    }

    private static boolean moveLeft(String[] name, int[] itemsToSell) {
        boolean sorted = true;

        for (int j = name.length - 1, i = j - 1; i >= 0; i--, j--) {
            if (name[i].compareTo(name[j]) > 0) {
                sorted = false;
                swap(itemsToSell, i, j);
                swap(name, i, j);
            }
        }

        return sorted;
    }

    private static void swap(String[] arr, int i, int j) {
        String tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

    private static void swap(int[] arr, int i, int j) {
        int tmp = arr[i];
        arr[i] = arr[j];
        arr[j] = tmp;
    }

}

CodePudding user response:

this is the whole program. I thank you for your answer but that is a bit over my paygrade. I only make one class.

public class Sorting
{
   public static Scanner input = new Scanner(System.in);

   public static void main(String[] args)
   {

      String choice = " ";

      String[] name = new String[100];

      String[] nameOfPersonBuying = new String[100];

      int[] itemsToSell = new int[200];

      int[] id = new int[9999]; // Random number goes in to the parameter.

      int[] amountBought = new int[200];

      Date[] date = new Date[10];

      Boolean[] boughtOrNot = new Boolean[2];

      while (true)
      {
         choice = menu(choice);

         switch (choice)
         {
         case "1":
            add(name, itemsToSell, id);
            break;
         case "2":
            printNames(name, itemsToSell, amountBought);
            break;
         case "3":
            printIdInAscendingOrder(name, id, boughtOrNot);
            break;
         case "4":
            borrow(id, nameOfPersonBuying, date);
            break;
         case "5":
            printListOfBorrowedItem(name, id);
            break;
         case "6":
            sellItem(id);
            break;
         case "q":
            System.out.println("Programmet avslutat");
            break;
         default:
            System.out.println("Felaktig inmatning");
            break;

         }
      }

   } // End main

   public static String menu(String choice)
   {

      System.out.println("1. Add a person");
      System.out.println("2. List of persons in alphabetic order");
      System.out.println("3. List of persons id in ascending order ");
      System.out.println("4. Buy item of a person");
      System.out.println("5. Lista på köpta föremål");
      System.out.println("6. Sell item");
      System.out.println("q. Exit");
      System.out.println("Your choice: ");

      int selection = 0;

      do
      {
         if (input.hasNextLine())
         {
            choice = getStringInput(choice);
            break;
         } else
         {
            System.out.println("\nSkriv ett giltligt nummer, tack");
         }

      } while (selection == 0);

      return choice;

   }

   public static void add(String[] name, int[] itemsToSell, int[] id)
   {

      try
      {
         String names = " ";
         int amountOfItemsToSell = 0;
         int idNumber = 0;

         System.out.println("Namnet på personen? ");

         names = getStringInput(names);

         System.out.println("Antal föremål att sälja? ");

         amountOfItemsToSell = getIntInput(amountOfItemsToSell);

         idNumber = randomNumber(idNumber);

         System.out.print("Namn:\t"   "Antal föremål:\t"   "ID:\n");

         for (int i = 0; i < name.length; i  )
         {
            if (id[i] == 0) // If this dosen't exists the alphabetic order dosen't work. Why?
            {
               name[i] = names;
               itemsToSell[i] = amountOfItemsToSell;
               id[i] = idNumber;
               break;
            }

         }

         System.out.println(names   "\t"   amountOfItemsToSell   "\t\t"   idNumber);
      } catch (Exception e)
      {
         System.out.println("Fel inmatning");
      }

   }

   public static void printNames(String[] name, int[] itemsToSell, int[] amountBought)
   {

      System.out.print("Namn:\t"   "Antal föremål:\t"   "Antal köpta föremål:\n");

      boolean flag = true;

   
      
      while (flag)
      {
         flag = false;

         for (int i = 0; i < name.length - 1; i  )
         {
            for (int j = i   1; j < name.length; j  )
            {

               if (name[i].compareTo(name[j]) > 0 && name[i] != null) 
               {
                  flag = true;
                  int temp_item = itemsToSell[i];
                  itemsToSell[i] = itemsToSell[j];
                  itemsToSell[j] = temp_item;

                  String temp = name[i];
                  name[i] = name[j];
                  name[j] = temp;
                  
                  System.out.println(name[i]   "\t"   itemsToSell[i]   "\t\t"   amountBought[i]);
               }
            }
           

         }
      }

   }

   public static void printIdInAscendingOrder(String[] name, int[] id, Boolean[] boughtOrNot)
   {

      System.out.print("ID:\t"   "Namn:\t"   "Finns/Finns Ej:\n");
      try
      {
         int temp;

         for (int i = 0; i < name.length; i  )
         {
            for (int j = i   1; j < name.length; j  )
            {
               if (id[i] > id[j])
               {
                  temp = id[i];
                  id[i] = id[j];
                  id[j] = temp;
                  break;
               }
            }
         }

         for (int i = 0; i < name.length; i  )
         {
            if (id[i] != 0)
            {
               System.out.println(id[i]   "\t"   name[i]   "\t");

            }
         }

      } catch (Exception e)
      {
         System.out.println(e);
      }
   }

   public static void borrow(int[] id, String[] nameOfPersonBuying, Date[] date)
   {
      // TODO Auto-generated method stub

   }

   public static void printListOfBorrowedItem(String[] name, int[] id)
   {
      // TODO Auto-generated method stub

   }

   public static void sellItem(int[] id)
   {
      // TODO Auto-generated method stub

   }

   public static int randomNumber(int randomNumber)
   {
      int min = 1000;
      int max = 100000;

      int range = (max - min);

      randomNumber = (int) (Math.random() * range)   min;

      return randomNumber;
   }

   public static String getStringInput(String string)
   {
      input.useDelimiter("[\\s] ");

      string = input.nextLine();

      return string;
   }

   public static int getIntInput(int integer)
   {
      input.useDelimiter("[\\s] ");

      integer = input.nextInt();
      input.nextLine(); // ??

      return integer;
   }

}
  • Related