Search code examples
javaarrayssortingdrjava

Bubble sort code in JAVA


I still can't get this right. The code in the bubble sort is incorrect. How can I get this right? What should I change or add to get the correct results? Thanks in advance. :)

import java.util.Random;
import java.util.Scanner;

public class HomeWork {

    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);

        int choice;
        int e;

        Random t = new Random();
        for (e = 1; e <= 5; e++) {
            System.out.println(t.nextInt(1000));
        }
        System.out.println(" \n1: BUBBLE SORT ");
        System.out.println(" 2: SELECTION SORT ");
        System.out.println(" 3: QUICK SORT ");
        System.out.println(" Choose a number from 1-3 ");
        choice= s.nextInt();

        if(choice == 1) {
            System.out.print("You chose BUBBLE sort!");
            int temp, q, w;
            for(int i=0;i<w-1;i++) { //I think there is something wrong here in my bubble sort code. 
                       // What should I add or change to make this correct?
                for(int j=0;j<w-1-i;j++) {
                    if(q[j]>q[j+1]) {
                        temp = q[j];
                        q[j] = q[j+1];
                        q[j+1] = temp;
                        System.out.println(q[i]+""); // What should I change here to print the correct results?
                    } else if(choice == 2) {
                        System.out.print("You chose SELECTION sort!");
                    } else if(choice == 3) {
                        System.out.println("You chose QUICK sort!");
                    } else {
                        System.out.println("Not in the choices!");
                    }
                }
            }
        }
    }
}

I am still just a beginner. Please please help. Thanks in advance :)


Solution

  • public static void main(String[] args) {
    
        int a[] = { 1, 5, 100, 40, 80, 50 };
    
        int length = a.length;
        int temp;
    
        for (int i = 0; i < length; i++) {
            for (int j = 1; j < length - i; j++) {
                if (a[j - 1] > a[j]) {
                    temp = a[j - 1];
                    a[j - 1] = a[j];
                    a[j] = temp;
                }
            }
        }
    
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
    }
    

    Like the guy commented above, this the bubble sorting algorithm, and the guy above that, your Variable (w) is wrong, all wrong, and as a tip in the future, post the error, with the code, that's probably why you got a downvote and or because this is pretty simple.

    Also forgive me, but i dont have eclipse, and am now coding in C++, but this should work

    int x[] = new int[5]
    Random t = new Random();
    for (e = 1; e <= 5; e++) {
        x[e] = t.nextInt(1000); // you didn't even assign any variables to sort, that's one problem, you just printed them.
    } // and idk how you get random int's in java, but if this doesn't work, just make your own random int generator.
      // There's LOTS of better ones than the one your using now.
    
    int temp, q, w;
    
    for(int i=0;i< 5;i++) { 
        for(int j=0;j<5-i;j++) {
            if(q[j]>q[j+1]) {
                temp = q[j-1];
                q[j-1] = q[j];
                q[j] = temp;
            } 
            // Add 'else ifs' here
        }
    }
    
    for (int i = 0; i < 5; i++) { // and this will print the results
        System.out.print(q[i] + " ");
    }
    

    This should work, idk, not really experienced in java >.>, and btw there are tons of books that teach algorithm's, you should first look through your code really well, because of it's a simply mistake like now, these people on stack overflow aren't merciful, there all experience programmer that are really hard on you, but they are smart. (like me :D)

    [EDIT] -btw, just merge this with your code, but here are some useful sites. for random int's - http://www.javapractices.com/topic/TopicAction.do?Id=62 for bubble sort - http://examples.javacodegeeks.com/core-java/bubble-sort-algorithm-in-java-code-example/