Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By jim829

Thread: Bubble Sort

  1. #1
    Cruger is offline Member
    Join Date
    Oct 2013
    Posts
    1
    Rep Power
    0

    Post Bubble Sort

    Hi there,
    I have a bubble sort java code which is sorting a integer of array in ascending order. But I also want another code which sorting it in descending order. But i have no idea how to do it. Please help me to modify my code to sort an array in descending order.

    Here is my code which sort in ascending order:
    public class BubbleSort{

    public static void main(String[] args) {
    int[] number = {8,5,3,2,9};
    int temp;
    boolean fixed = false;

    while (fixed == false){
    fixed = true;

    for (int i=0; i<number.length-1; i++){
    if (number [i]>number [i+1]){
    temp = number [i+1];
    number [i+1] = number [i];
    number [i] = temp;
    fixed = false;

    }

    }

    }

    for (int i=0; i<number.length; i++){
    System.out.println(number[i]);

    }

    }

    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Bubble Sort

    Moved to New to Java from a staff only section.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,023
    Rep Power
    6

    Default Re: Bubble Sort

    Please place your code between [CODE][/CODE] tags to format it. I have done it here for you.

    Java Code:
    public class BubbleSort {
       public static void main(String[] args) {
          int[] number = { 8, 5, 3, 2, 9
          };
          int temp;
          boolean fixed = false;
          while (fixed == false) {
             fixed = true;
             for (int i = 0; i < number.length - 1; i++) {
                if (number[i] > number[i + 1]) {
                   temp = number[i + 1];
                   number[i + 1] = number[i];
                   number[i] = temp;
                   fixed = false;
                }
             }
          }
          for (int i = 0; i < number.length; i++) {
             System.out.println(number[i]);
          }
       }
    }
    Did you write this code? The reason I ask is because you must know how to sort in one direction vs the other to write any sort routine. So find that particular statement and change it. It may help to follow the algorithm on paper.

    Regards,
    Jim
    Last edited by jim829; 10-10-2013 at 06:24 PM.
    Cruger likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Please help me to initialise my bubble sort
    By riddy in forum New To Java
    Replies: 4
    Last Post: 03-21-2013, 12:09 AM
  2. Bubble sort
    By pineapple in forum New To Java
    Replies: 3
    Last Post: 04-25-2009, 01:45 AM
  3. How to sort a list using Bubble sort algorithm
    By Java Tip in forum Algorithms
    Replies: 3
    Last Post: 04-29-2008, 09:04 PM
  4. Bubble Sort in Java
    By Java Tip in forum Algorithms
    Replies: 0
    Last Post: 04-15-2008, 08:42 PM
  5. need help with bubble sort
    By lowpro in forum New To Java
    Replies: 3
    Last Post: 12-17-2007, 06:27 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •