|
-
Nov 19th, 2008, 06:25 PM
#1
Thread Starter
Addicted Member
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
-
Nov 20th, 2008, 02:36 AM
#2
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
-
Nov 20th, 2008, 02:38 AM
#3
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
-
Nov 20th, 2008, 02:40 AM
#4
Thread Starter
Addicted Member
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
-
Nov 20th, 2008, 07:29 AM
#5
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
-
Nov 20th, 2008, 02:37 PM
#6
Thread Starter
Addicted Member
Re: Cocktail Sort
whats Test? in code above?
-
Nov 20th, 2008, 03:47 PM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|