0

I am writing code to sort an array in order. I checked some algorithm for sort and merge, however I found that if I just go through the array and compare every 2 elements and swap them and repeat that until the array is sorted. So if array[I] > array[i++], swap, repeat.

It is not working so far. I also need a break point to avoid stack overflow: I need some help please

Array:

    int[] newArray = new int[] {3,9,5,7,4,6,1};

    SortArray s = new SortArray();

    s.sortThisArray(newArray, 0, 0);

Recursive function:

public String sortThisArray(int[] array, double counter, double a)
{
    int swap0 = 0; 
    int swap1 = 0; 

    if (a > 1000)
    {
        return "reached the end" ; 
    }

    for (int i =0; i<array.length; i++)
    {
        if (array[i] > array[i++])
        {
            swap0 = array[i];
            swap1 = array[i++];
            array[i++] = swap0; 
            array[i] = swap1; 

            counter = counter++; 
            a = array.length * counter; 

            sortThisArray (array, counter, a); 
        }
    }

    for (int j = 0; j<array.length ; j++)
    {
        System.out.println(array[j]);
    }
    return "completed"; 
}

}

Eihab
  • 7
  • 4
  • 1
    i++ increments i. You used it several times when you probably wanted to use i+1. counter = counter++; doesn't do anything. It increments counter and then assigns it back to the old value. See https://stackoverflow.com/questions/2371118/how-do-the-post-increment-i-and-pre-increment-i-operators-work-in-java – Bene May 31 '18 at 14:03
  • Why not use standard sorting functions? – lexicore May 31 '18 at 14:57
  • I am trying to resolve the time complexity, using the standard sorting functions is time efficient – Eihab Jun 01 '18 at 06:44

2 Answers2

0

What you are searching for is recursive bubble sort algorithm.

The main error is confusing i++ (which increments i every time) with i+1, that is only the position in the array after i, without incrementing. Then, there is no need to use double for counter, and also the a variable is not needed. You need only the length of the current segment, this way:

import java.util.*;
public class RecursiveBubbleSort {

public static void main(String[] args) throws Exception {
    int[] newArray = new int[] {3,9,5,7,4,6,1};

sortThisArray(newArray, newArray.length);

    System.out.println("Sorted array : ");
    System.out.println(Arrays.toString(newArray));
}

public static int[] sortThisArray(int[] array, int n) {
    if (n == 1) {
        return array; //finished sorting
    }

    int temp;
    for (int i = 0; i < n-1; i++) {
        if (array[i+1] < array[i]) {
            temp = array[i];
            array[i] = array[i+1];
            array[i+1] = temp;
        }
    }
    return sortThisArray(array, n-1);
}

}
  • And hope the compiler is smart enough to remove tail recursion. Otherwise an array of even moderate size is going to blow the stack. – Jim Mischel May 31 '18 at 21:58
  • Thank you, this was very helpful, I made changes to the code, and it now works. Sorting algorithms are not easy to grasp, will need to spend some more time to understand it. Thanks again – Eihab Jun 01 '18 at 07:12
0

The ++ operator evalutes separately. So in the case i++, we will read i and then increment it by 1. And the other way around, ++i, would first apply the increment to i, and then read i. So when you want to check for the next element in the array, [i+1] is what you are looking for. With the same reasoning, can you figure out whats wrong with this line?

counter = counter++;

And do we even need it?

JohEker
  • 627
  • 5
  • 13