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
Printable View
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
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;
}
}
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.
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
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);
}
}
whats Test? in code above?
The name of the class !! :S