0

I am trying to make a cocktail sort but I am getting an out of bound exception at the line if (a[i] > a[i + 1]) and I'm not sure why.

Here is the full code. Sorry if this is completely wrong.

import java.util.Arrays;
import java.util.Scanner;

public class Cocktail 
{

public static void main(String[] args) 
{
    Scanner input = new Scanner(System.in);
    int count = 0;
    boolean switched = true;
    int[]a = new int[10];
    for (int i = 0; i < a.length; i++)
    {
        int value = input.nextInt();
        a[i] = value;
    }
    System.out.println(a[0]);
    while (switched == true)
    {
        switched = false;
        for (int i = 0; i < a.length; i++)
        {
            if (a[i] > a[i + 1]) 
            {
                int temp = a[i];
                a[i] = a[i + 1];
                a[i + 1] = temp;
                count++;
                switched = true;
            }
        }
        for (int i = a.length; i >= 0; i++)
        {
            if (a[i] > a[i + 1]) 
            {
                int temp = a[i];
                a[i] = a[i + 1];
                a[i + 1] = temp;
                count++;
                switched = true;
            }
        }
        if (switched == false)
        {
            System.out.println(count);
        }
    }

}

}

1 Answers1

1

you need to Change

if (a[i] > a[i + 1])to be like this --> if (i < a.length-1 && a[i] > a[i + 1]).

The problem was it is trying to reach the 11th element ;)

If you may, here's an Edited version of your code:

Scanner input = new Scanner(System.in);
    int count = 0;
    boolean switched = true;


    int[]a = new int[10];
    System.out.println("enter 10 Integers: ");// # Added to make code clearer
    for (int i = 0; i < a.length; i++)
    {

        int value = input.nextInt();
        a[i] = value;
    }
        System.out.println("thankyou, Sorting now!");//# also this one 
    while(switched == true)
    {
        switched = false;
        for (int i = 0; i < a.length; i++)
        {
            if (i < a.length-1 && a[i] > a[i + 1]) // <-- # here was the problem
            {
                int temp = a[i];
                a[i] = a[i + 1];
                a[i + 1] = temp;
                count++;
                switched = true;
            }
        }
        for (int i = a.length; i >= 0; i++)
        {
            if (i < a.length-1 && a[i] > a[i + 1]) //<-- # Also Here 
            {
                int temp = a[i];
                a[i] = a[i + 1];
                a[i + 1] = temp;
                count++;
                switched = true;
            }
        }
        if (switched == false)
        {
            System.out.println("count is "+ count);
        }
    }
    // # added part to print array for testing 
        System.out.println("Sorted Array:");
        for (int i = 0; i <a.length ; i++) {


            System.out.print(a[i]+", ");
        }


    }//main 

}//class

Here's the output:

OUTPUT

Copy and paste it, Run and Happy Coding =D