-2

I am just sorting an array and need some advice on the sorting and I also need help printing the array after I have sorted it. Also no, I do not want to use the Arrays utility.

Code:

package Sort;

public class SortCode {

    static int[] intArray = {
        12, 34, 99, 1, 89,
        39, 17, 8, 72, 68};
    int j = 0;
    int i = 0;

    void printArray(int[] arrayInts) {
        System.out.println("Values before sorting:");
        System.out.println("Index" + "\tValue");

        for (; j < arrayInts.length; j++) {
            System.out.println(j + "\t" + arrayInts[j]);

        } //for (int j)

    } //void printArray

    void sortArray() {
      System.out.println("Values after sorting:");
      System.out.println("Index" + "\tValue");
       int i;
       int k;
        for (i = 0; i < intArray.length; i++) {
          for (k = 0; k > intArray.length; k++) {
            if (intArray[i] > intArray[k]) {
                int firstNum = intArray[i];
                int secondNum = intArray[k];
                intArray[i] = secondNum;
                intArray[k] = firstNum;

            } //if

          } //for

        } //for

    } //void sortArray

} //class BranchCode
user3666197
  • 1
  • 6
  • 50
  • 92
  • define "acting weird". What kind of error you are getting? – Ashalynd Sep 30 '14 at 12:41
  • 2
    Debugging. Pretend you're the computer, line by line--it's a very easy error to find if you do that, deliberately, with attention. Also, I advocate against right-brace comments like that--instead, rely on indentation (I'd suggest 4 spaces) and limited nesting. – Dave Newton Sep 30 '14 at 12:43

2 Answers2

4

Change sign > for < inside for (k = 0; k > intArray.length; k++) {

Probably it should help you

Mysterion
  • 9,050
  • 3
  • 30
  • 52
0

You would be able to find different sorting implementation on mathbits http://mathbits.com/MathBits/Java/arrays/Sorting.htm .

Here is better example of bubble sort .

public  void bubbleSort(int[] array) {
    boolean swapped = true;
    int j = 0;
    int tmp;
    while (swapped) {
        swapped = false;
        j++;
        for (int i = 0; i < array.length - j; i++) {
            if (array[i] > array[i + 1]) {
                tmp = array[i];
                array[i] = array[i + 1];
                array[i + 1] = tmp;
                swapped = true;
            }
        }
    }
}

This might help as well Java: Sort an array

Example to use code

public class SortExample {

int[] intArray = { 12, 34, 99, 1, 89, 39, 17, 8, 72, 68 };

public void printArray(int[] arrayInts) {
    for (int j = 0; j < arrayInts.length; j++) {
        System.out.println(j + "\t" + arrayInts[j]);

    } // for (int j)

} // void printArray

public void bubbleSort(int[] array) {
    boolean swapped = true;
    int j = 0;
    int tmp;
    while (swapped) {
        swapped = false;
        j++;
        for (int i = 0; i < array.length - j; i++) {
            if (array[i] > array[i + 1]) {
                tmp = array[i];
                array[i] = array[i + 1];
                array[i + 1] = tmp;
                swapped = true;
            }
        }
    }
}

public void process() {

    System.out.println("Values before sorting:");
    System.out.println("Index \tValue");
    printArray(intArray);
    bubbleSort(intArray);
    System.out.println("Values after sorting:");
    System.out.println("Index" + "\tValue");
    printArray(intArray);
}

public static void main(String[] args) {
    SortExample example = new SortExample();
    example.process();
}

}

Community
  • 1
  • 1
Sayyid
  • 86
  • 3
  • Ok, now what would be the println statement to printout the sorted array using this? – fixterjake11 Sep 30 '14 at 13:01
  • assuming I am calling the methods in following order bubbleSort(intArray); printArray(intArray); Output will be as follows Values before sorting: Index Value 0 1 1 8 2 12 3 17 4 34 5 39 6 68 7 72 8 89 9 99 – Sayyid Sep 30 '14 at 13:06
  • Well yeah but what would the System.out.println( what would go here to print sorted array) – fixterjake11 Sep 30 '14 at 13:12
  • you are already printing . public void printArray(int[] arrayInts) { for (int j = 0; j < arrayInts.length; j++) { System.out.println(j + "\t" + arrayInts[j]); } } – Sayyid Sep 30 '14 at 13:19
  • I want to print it twice, before its sorted and after. How can I print it after it is sorted in the sortCode method – fixterjake11 Sep 30 '14 at 13:20
  • Modify the your printArray method , remove the print line . something like this System.out.println("Values before sorting:"); System.out.println("Index \tValue"); printArray(intArray); bubbleSort(intArray); System.out.println("Values after sorting:"); System.out.println("Index" + "\tValue"); printArray(intArray); – Sayyid Sep 30 '14 at 13:31