Search code examples
javaarrayssortingalternate

I needed a program to sort an integer Array alternating, but it gives me a StackOverflow Error


I need to sort an integer Array alternating and I am not allowed to use any packages. I'm sitting on this code for a long time, and while it works on paper, the Test program my university uses, comments my code with StackOverflow.

Here is the code in question. I'm just hoping for some experienced Developer to tell me whats wrong. I'm pretty new to Java.

public class Functionality {

public static int[] alternateSorting(int[] a) {

    int v[] = new int[a.length];

    int z = a.length;


    if (z == 0) {

        System.out.println("Error, empty Array");
        return a;

    }else {



        for ( int i = 0; i < a.length; i++) {
            v[i] = a[i];
        }

        boolean swapped;

        do {

            swapped = false;
            for (int i = 1; i < v.length; i++) {

                if (v[i-1] > v[i]) {
                    int swap = v[i];
                    v[i] = v[i-1];
                    v[i-1] = swap;
                    swapped = true;
                }
            }
        }while (swapped);



        for (int f = 0; f < a.length - 1; f++) {

            for (int c = f + 1; c < a.length; c++)

                if (v[c] == v[f]) {

                    z = z - 1;


                } 
        }


        int b[]  = new int[z];

        int k = v[0];

        int g = v[v.length - 1];

        int l = v.length;

        int m = 1;

        for (int i = 0; i < v.length; i++) {



            if ( (i*2)%4 == 0) {
                if (v[i] == v[i+1]) {
                    b[i] = k;
                    k = v[i+m+1];
                    m--;
                } else if (v[i] != v[i+1]) {
                    b[i] = k;
                    k = v[i+m];
                    m--;
                }else{
                    b[i] = k;
                }

            }else {
                if (v[l - 1] == v[l - 2]) {
                    b[i] = g;
                    g = v[l - 3];
                    l--;
                } else if (v[l - 1] != v[l - 2]) {
                    b[i] = g;
                    g = v[l - 2];
                    l--;
                } else {
                    b[i] = g;
                }
            }
        }return  b ;
    }

}

The Array a has to be sorted out place. b is the array that gets returned, and v is the "working" array.

Thanks for your time and attention. I'm sorry if this is a bother to you. I'm new to this site, so critique is alright!

Greetings, T.


Solution

  • public class Functionality {
    
        public static void main(String args[]) {
            int[] ind = {1, 10, 2, 6, 11, 5};
    
            int res[] = alternateSorting(ind);
    
            for(int i=0; i< res.length; i++) {
                System.out.println(res[i]);    
            }
    
        }
    
        public static int[] alternateSorting(int[] a) {
    
            int v[] = new int[a.length];
    
            int z = a.length;
    
    
            if (z == 0) {
    
                System.out.println("Error, empty Array");
                return a;
    
            } else {
    
    
                for ( int i = 0; i < a.length; i++) {
                    v[i] = a[i];
                }
    
                boolean swapped;
    
                do {
    
                    swapped = false;
                    for (int i = 1; i < v.length; i++) {
    
                        if (v[i-1] > v[i]) {
                            int swap = v[i];
                            v[i] = v[i-1];
                            v[i-1] = swap;
                            swapped = true;
                        }
                    }
                } while (swapped);
    
    
    
                for (int f = 0; f < a.length; f++) {
    
                    for (int c = f + 1; c < a.length; c++)
    
                        if (v[c] == v[f]) {
    
                            z = z - 1;
    
                        } 
                }
    
    
                int b[]  = new int[z];
    
                int k = v[0];
    
                int g = v[v.length - 1];
    
                int l = v.length;
    
                int m = 1;
    
                for (int i = 0; i < v.length; i++) {
                    if ( (i*2)%4 == 0) {
                        if (v[i] == v[i+1]) {
                            b[i] = k;
                            k = v[i+m+1];
                            m--;
                        } else if (v[i] != v[i+1]) {
                            b[i] = k;
                            k = v[i+m];
                            m--;
                        }  else  {
                            b[i] = k;
                        }
    
                    } else {
                        if (v[l - 1] == v[l - 2]) {
                            b[i] = g;
                            g = v[l - 3];
                            l--;
                        } else if (v[l - 1] != v[l - 2]) {
                            b[i] = g;
                            g = v[l - 2];
                            l--;
                        } else {
                            b[i] = g;
                        }
                    }
                }
                return  b ;
            }
        }    
    }
    

    Improve this to fit your needs. This will output:

    1
    11 
    2
    10
    5
    6