0

I have an array including user's inputs. The program is about Bubble sort, Selection sort and Insertion sort. First Bubble, Second Selection and then Insertion sort comes.

I couldn't manage to solve a problem. When the code run into selection sort, the array is already sorted by bubble sort.

I tried to make 2 temporary arrays at first to use the "source array" at selection and insertion sorting but those arrays re-arranged by bubble sort again. ( Which I don't understand why )

Is there any way to sort my array seperately or I have to make them methods ? I'm counting the swaps and comparisons also BTW. Thanks !

    System.out.println("• Please enter the number of elements in the Sorting Bag:");
    length = input.nextInt();
    System.out.println("• The number of elements: " + length);

    int[] SorBag = new int[length];
    int[] SorBag2 = new int[length];
    int[] SorBag3 = new int[length];

    System.out.println("• Please enter the elements of Sorting Bag:");
    for (int i = 0; i < SorBag.length ; i++) {
        SorBag[i] = input.nextInt();
    }

    SorBag2 = SorBag;
    SorBag3 = SorBag;

    System.out.print("• Elements in the Sorting Bag are:");
    for (int j = 0; j < SorBag.length; j++) {
        System.out.print(" " + SorBag[j]);
    }
    System.out.println("");
    System.out.println("");

    //Bubble Sort    
    for (int i = 1; i < SorBag.length; i++) {
        for (int j = 0; j < SorBag.length - i; j++) {
            BComparison++;
            if (SorBag[j] > SorBag[j + 1]) {
                BSwaps++;
                temp1 = SorBag[j + 1];
                SorBag[j + 1] = SorBag[j];
                SorBag[j] = temp1;
            }
        }
    }
    System.out.print("• Bubble Sort:");
    for (int k = 0; k < SorBag.length; k++) {
        System.out.print(" " + SorBag[k] + " ");
    }
    System.out.print("Comparisons: " + BComparison + " Swaps: " + BSwaps);
    System.out.println(" ");

    //Selection Sort
    for (int i = 0; i < SorBag2.length; i++) {
        min = i;

        for (int j = i + 1; j < SorBag2.length; j++) {
            SComparison++;

            if (SorBag2[j] < SorBag2[min]) {
                min = j;
            }

            if (min != i) {

                temp2 = SorBag2[i];
                SorBag2[i] = SorBag2[min];
                SorBag2[min] = temp2;
                SSwaps++;
            }
        }
    }

    System.out.print("• Selection Sort:");
    for (int k = 0; k < SorBag2.length; k++) {
        System.out.print(" " + SorBag2[k] + " ");
    }
    System.out.print("Comparisons: " + SComparison + " Swaps: " + SSwaps);
    System.out.println(" ");

    //Insertion Sort
    for (int i = 1; i < SorBag3.length; i++) {

        int j = 0;

        while (j > i && SorBag3[j] < SorBag3[j - 1]) {

            temp3 = SorBag3[j];
            SorBag3[j] = SorBag3[j - 1];
            SorBag3[j - 1] = temp3;

            ISwaps++;

            j--;
        }

        IComparison++;
    }
    System.out.print("• Insertion Sort:");
    for (int k = 0; k < SorBag3.length; k++) {
        System.out.print(" " + SorBag3[k] + " ");
    }
    System.out.print("Comparisons: " + IComparison + " Swaps: " + ISwaps);
    System.out.println(" ");

}
}
10
  • 2
    You are going to add the relevant parts of your code, right? Commented Oct 21, 2014 at 13:50
  • Where is your code ? Commented Oct 21, 2014 at 13:50
  • 2
    Now that you uploaded your code, once can see that since SorBag2 and 3 point to SorBag, they will also be already sorted. Commented Oct 21, 2014 at 13:52
  • 3
    Instead of writing everything in the main method, write separate methods and call them from main(). Also, for Sorbag2 and Sorbag3, write for loops to manually copy the elements of sorbag to 2 and 3, so that the pointers are not pointing to the same place in memory. Commented Oct 21, 2014 at 13:54
  • 2
    If you don't want to make methods, make loops to manually copy each element of sorbag to sorbag2 and sorbag3. Commented Oct 21, 2014 at 13:56

1 Answer 1

4

SorBag2 = SorBag and SorBag3 = SorBag copies the reference of SorBag to the other two arrays, instead of only copying the data. So instead of:

System.out.println("• Please enter the elements of Sorting Bag:");
for (int i = 0; i < SorBag.length ; i++) {
    SorBag[i] = input.nextInt();
}

SorBag2 = SorBag;
SorBag3 = SorBag;

Try this:

System.out.println("• Please enter the elements of Sorting Bag:");
for (int i = 0; i < SorBag.length ; i++) {
    int nextInt = intput.nextInt();
    SorBag[i] = nextInt;
    SorBag2[i] = nextInt;
    SorBag3[i] = nextInt;        
}
Sign up to request clarification or add additional context in comments.

1 Comment

No problem. If this answered your question, please accept it as the answer. Also, a recommendation: use sorBag instead of SorBag. It's not a must, but in most programming languages variables/field-names start with a lower-case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.