Results 1 to 7 of 7

Thread: Cocktail Sort

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Posts
    216

    Cocktail Sort

    Hi, i was wondering i some one could show me how does a cocktail sort works in java. i need to sort an array list alphabetically any help would be nice ty

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Cocktail Sort

    I read the documentation from Wikipedia about the Cocktail Sort and tried my best
    Code:
    import java.util.Random;
    
    public class Test
    {
    	public static void cocktailSort(final int[] array)
    	{
    		boolean swapped = false;
    		do
    		{
    			for (int i = 0; i < array.length - 2; i++)
    				if (array[i] > array[i + 1])
    				{
    					Test.swap(array, i);
    					swapped = true;
    				}
    			if (!swapped)
    				break;
    			swapped = false;
    			for (int i = array.length - 2; i >= 0; i--)
    				if (array[i] > array[i + 1])
    				{
    					Test.swap(array, i);
    					swapped = true;
    				}
    		} while (swapped);
    	}
    
    	public static void main(final String[] args) throws InterruptedException
    	{
    		final Random rand = new Random();
    		final int[] testArray = new int[rand.nextInt(1000)];
    		for (int i = 0; i < testArray.length; i++)
    			testArray[i] = rand.nextInt();
    		Test.printArray(testArray);
    		Thread.sleep(30000);
    		Test.cocktailSort(testArray);
    		Test.printArray(testArray);
    	}
    
    	private static void printArray(final int[] array)
    	{
    		for (int i = 0; i < array.length; i++)
    		{
    			System.out.print(array[i]);
    			if (i == array.length - 1)
    				System.out.println();
    			else
    				System.out.print(',');
    		}
    	}
    
    	private static void swap(final int[] array, final int index)
    	{
    		final int temp = array[index];
    		array[index] = array[index + 1];
    		array[index + 1] = temp;
    	}
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Cocktail Sort

    2 things though, I restricted the size of the array to something less than 1000 so I don't get the OutOfMemory exception. And I added the Thread.sleep for 30 seconds because IO operations are so slow, so the array might be sorted before it's done printing it.
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Posts
    216

    Re: Cocktail Sort

    that kinda makes sense but i have to do it with ArrayList not Array i found it difficult to change positions ...i would really appreciate if you could show me how to do it with arraylists

  5. #5
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Cocktail Sort

    Same algorithm, ArrayList
    Code:
    import java.util.ArrayList;
    import java.util.Random;
    
    public class Test
    {
    	public static void cocktailSort(final ArrayList<Integer> list)
    	{
    		boolean swapped = false;
    		do
    		{
    			for (int i = 0; i < list.size() - 2; i++)
    				if (list.get(i) > list.get(i + 1))
    				{
    					Test.swap(list, i);
    					swapped = true;
    				}
    			if (!swapped)
    				break;
    			swapped = false;
    			for (int i = list.size() - 2; i >= 0; i--)
    				if (list.get(i) > list.get(i + 1))
    				{
    					Test.swap(list, i);
    					swapped = true;
    				}
    		} while (swapped);
    	}
    
    	public static void main(final String[] args) throws InterruptedException
    	{
    		final Random rand = new Random();
    		final ArrayList<Integer> testList = new ArrayList<Integer>();
    		for (int i = 0; i < 1000; i++)
    			testList.add(rand.nextInt());
    		Test.printList(testList);
    		Thread.sleep(10000);
    		Test.cocktailSort(testList);
    		Test.printList(testList);
    	}
    
    	private static void printList(final ArrayList<Integer> list)
    	{
    		for (int i = 0; i < list.size(); i++)
    		{
    			System.out.print(list.get(i));
    			if (i == list.size() - 1)
    				System.out.println();
    			else
    				System.out.print(", ");
    		}
    	}
    
    	private static void swap(final ArrayList<Integer> list, final int index)
    	{
    		final int temp = list.get(index);
    		list.set(index, list.get(index + 1));
    		list.set(index + 1, temp);
    	}
    }
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2006
    Posts
    216

    Re: Cocktail Sort

    whats Test? in code above?

  7. #7
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Cocktail Sort

    The name of the class !! :S
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width