954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Bubble sort-source code

Can someone show me what bubble-sorting an int[] of { 5, 4, 3, 2, 1 } looks like? I have to place it inside

public class SortIntArray {
    // this method needs your code to be added to work
    public static void sort(int[] list) {


with no calls to any other methods or classes. I've been working on an assignment for two days and haven't been able to make any progress.

JRDJ12
Newbie Poster
19 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 
stevanity
Posting Whiz in Training
293 posts since Oct 2010
Reputation Points: 43
Solved Threads: 28
 
Algorithm: http://en.wikipedia.org/wiki/Bubble_sort


Thanks, I've applied bubble sort. Now I'm having troubles figuring out how to pass the sorted array to the end of my program (which looks like this)

// this method is complete, do not alter it
    public static String format(int[] set) {
        StringBuilder sb = new StringBuilder();
        for ( int i = 0; i < set.length; i++ ) {
            if ( i > 0 ) {
                sb.append(", ");
            }
            sb.append(set[i]);
        }
        return sb.toString();
    }

    // this method is complete, do not alter it
    public static void process(int[] set) {
        System.out.println("Original int[]: " + format(set));
        sort(set);
        System.out.println("    after sort: " + format(set));
        System.out.println("----------");
    }

    // this method is complete, do not alter it
    public static void main(String[] args) {
        process(new int[] { 5, 4, 3, 2, 1 });
        process(new int[] { 1, 2, 3, 4, 5 });
        process(new int[] { 5, 5, 4, 4, 3, 3, 2, 2, 1, 1 });
        process(new int[] { 9, 7, 5, 3, 1, 8, 6, 4, 2 });
        process(new int[] { 10, 5, 6, 1, 4, 1, 5, 5, 9, 2 });
    }
}


For clarity, I'm trying to get my output of the first int[] to read:
Original int[]: 5, 4, 3, 2, 1
after sort: 1 2 3 4 5

What would be my next step to accomplish this without changing the methods listed above?

JRDJ12
Newbie Poster
19 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 
how to pass the sorted array


Which array are you trying to pass to what code?

Can you explain a bit more? Where do you get/define the array, where is it sorted and where do you want to pass the sorted array?

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

Which array are you trying to pass to what code?

Can you explain a bit more? Where do you get/define the array, where is it sorted and where do you want to pass the sorted array?

Sorry Norm, should've been more clear...below is the entire code:

package com.abc.sort;

public class SortIntArray {
    // this method needs your code to be added to work
    public static void sort(int[] list) {
        // Use the Bubble Sort algorithm to sort. Do NOT use any external
        // utilities to sort (such as Arrays.sort(int[])). Do not call
        // any other methods from within this method.
        //
        // When using Bubble Sort, after each pass, an additional int
        // at the end is in the correct position, so we don't have to 
        // compare it again (see the example below).
        // 
        // For example, if we had an array with 5 int's in it, we'll need
        // 4 passes, like this:
        // Pass 1:
        //   compare list[0] and list[1] and swap if out of order
        //   compare list[1] and list[2] and swap if out of order
        //   compare list[2] and list[3] and swap if out of order
        //   compare list[3] and list[4] and swap if out of order
        //   (now, list[4] has the largest, no need to look at it again)
        // Pass 2:
        //   compare list[0] and list[1] and swap if out of order
        //   compare list[1] and list[2] and swap if out of order
        //   compare list[2] and list[3] and swap if out of order
        //   (now, list[3] has the second largest, no need to look at it again)
        // Pass 3:
        //   compare list[0] and list[1] and swap if out of order
        //   compare list[1] and list[2] and swap if out of order
        //   (now, list[2] has the third largest, no need to look at it again)
        // Pass 4:
        //   compare list[0] and list[1] and swap if out of order
        //   (now, list[1] has the fourth largest, and list[0] has the smallest)
        //
        // And again with specific data to further illustrate the example:
        //    3, 5, 4, 3, 1   (initial state, 5 int's -> 4 passes)
        // Pass 1:
        //    3, 5, 4, 3, 1   (compared 3 and 5, no swap needed)
        //    3, 4, 5, 3, 1   (compared 5 and 4, and swapped them)
        //    3, 4, 3, 5, 1   (compared 5 and 3, and swapped them)
        //    3, 4, 3, 1, 5   (compared 5 and 1, and swapped them)
        //                    now 5 is in the right position
        // Pass 2:
        //    3, 4, 3, 1, 5   (compared 3 and 4, no swap needed)
        //    3, 3, 4, 1, 5   (compared 4 and the second 3, and swapped them)
        //    3, 3, 1, 4, 5   (compared 4 and 1, and swapped them)
        //                    now 4 and  5 are in the right positions
        // Pass 3:
        //    3, 3, 1, 4, 5   (compared 3 and 3, no swap needed)
        //    3, 1, 3, 4, 5   (compared the second 3 and 1, and swapped them)
        //                    now 3, 4, and 5 are in the right positions
        // Pass 4:
        //    1, 3, 3, 4, 5   (compared 3 and 1, and swapped them)
        //                    Now all done!
        // 


        // your code goes here -------------
        
        int a [] = {5, 4, 3, 2, 1};

        for(int x = 0; x < a.length; x++){
            System.out.print(a[x] + " ");}

        for (int x = a.length-1; x > 0; x--){
            for(int y = 0; y < a.length-1; y++){
                int temp = a[y];
                int tempo = a[y+1];
                if(a[y] > a[y+1]){
                    a[y] = tempo;
                    a[y+1] = temp;

                }
            }
        }
        System.out.println();
        for(int x = 0; x < a.length; x++){

        }


    }

        

    // this method is complete, do not alter it
    public static String format(int[] set) {
        StringBuilder sb = new StringBuilder();
        for ( int i = 0; i < set.length; i++ ) {
            if ( i > 0 ) {
                sb.append(", ");
            }
            sb.append(set[i]);
        }
        return sb.toString();
    }

    // this method is complete, do not alter it
    public static void process(int[] set) {
        System.out.println("Original int[]: " + format(set));
        sort(set);
        System.out.println("    after sort: " + format(set));
        System.out.println("----------");
    }

    // this method is complete, do not alter it
    public static void main(String[] args) {
        process(new int[] { 5, 4, 3, 2, 1 });
        process(new int[] { 1, 2, 3, 4, 5 });
        process(new int[] { 5, 5, 4, 4, 3, 3, 2, 2, 1, 1 });
        process(new int[] { 9, 7, 5, 3, 1, 8, 6, 4, 2 });
        process(new int[] { 10, 5, 6, 1, 4, 1, 5, 5, 9, 2 });
    }
}


Be advised....my output is not correct at this point in time....The previous post shows how the output should look for the first array...I'm just having trouble figuring out how to get the sorted array into the ->

System.out.println("    after sort: " + format(set));


instead of somewhere else.

JRDJ12
Newbie Poster
19 posts since Nov 2011
Reputation Points: 10
Solved Threads: 0
 
how to get the sorted array into the->


Your sentence ends without saying where the sorted array is and where you want to access it.
If you create a variable that points to the array, you can pass that variable to various methods so they can access the array.
Your code creates several arrays without having variables.
Change it to:
int[] anArray1 = new int[].....

The you can use the variable anArray1 to pass that array to methods as required.

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: