Skip to content

Wigglesort #3032

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 25, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
add Tests, remove main and outputs, fix bug, add comment, fix typo
  • Loading branch information
BranAndSceolan committed Apr 23, 2022
commit b6b154eaefdfda44150e4f8a53d1f1c8d86bb52c
33 changes: 7 additions & 26 deletions src/main/java/com/thealgorithms/sorts/WiggleSort.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import static com.thealgorithms.searches.QuickSelect.select;

/**
* A wriggle sort implementation based on John L.s' answer in
* A wiggle sort implementation based on John L.s' answer in
* https://cs.stackexchange.com/questions/125372/how-to-wiggle-sort-an-array-in-linear-time-complexity
* Not all arrays are wiggle-sortable. This algorithm will find some obviously not wiggle-sortable arrays and throw an error,
* but there are some exceptions that won't be caught, for example [1, 2, 2].
*/
public class WiggleSort implements SortAlgorithm {
@Override
Expand All @@ -17,7 +19,7 @@ public <T extends Comparable<T>> T[] sort(T[] unsorted) {
}

private int mapIndex(int index, int n){
return (2 * index) % (n-1 | 1);
return ((2 * index + 1) % (n | 1)) ;
}

/**
Expand All @@ -32,11 +34,11 @@ private <T extends Comparable<T>> void triColorSort(T[] sortThis, T median){
int j = 0;
int k = n - 1;
while (j <= k) {
if (0 > sortThis[mapIndex(j, n)].compareTo(median)) {
if (0 < sortThis[mapIndex(j, n)].compareTo(median)) {
SortUtils.swap(sortThis, mapIndex(j, n), mapIndex(i, n));
i++;
j++;
} else if (0 < sortThis[mapIndex(j, n)].compareTo(median)) {
} else if (0 > sortThis[mapIndex(j, n)].compareTo(median)) {
SortUtils.swap(sortThis, mapIndex(j, n), mapIndex(k, n));
k--;
} else {
Expand All @@ -49,13 +51,7 @@ private <T extends Comparable<T>> T[] wiggleSort(T[] sortThis) {
// find the median using quickSelect (if the result isn't in the array, use the next greater value)
T median;

if(sortThis.length % 2 == 0) {
median = select(Arrays.<T>asList(sortThis), (int) (sortThis.length / 2.0));
} else {
median = select(Arrays.<T>asList(sortThis), (int) floor(sortThis.length / 2.0));
}

System.out.println(median);
median = select(Arrays.<T>asList(sortThis), (int) floor(sortThis.length / 2.0) -1);

for (T sortThi : sortThis) {
int numMedians = 0;
Expand All @@ -70,19 +66,4 @@ private <T extends Comparable<T>> T[] wiggleSort(T[] sortThis) {
triColorSort(sortThis, median);
return sortThis;
}
public static void main(String[] args) {
WiggleSort wiggleSort = new WiggleSort();

Integer[] integers0 = new Integer[]{1, 2, 3, 4, 5};
Integer[] integers1 = new Integer[]{1, 2, 1, 1, 5};
String[] strings = new String[]{"a", "b", "e", "d"};

wiggleSort.sort(integers0);
wiggleSort.sort(integers1);
wiggleSort.sort(strings);

System.out.println(Arrays.toString(integers0));
System.out.println(Arrays.toString(integers1));
System.out.println(Arrays.toString(strings));
}
}
53 changes: 53 additions & 0 deletions src/test/java/com/thealgorithms/sorts/WiggleSortTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.thealgorithms.sorts;

import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;


public class WiggleSortTest {
@Test
void WiggleTestNumbersEven(){
WiggleSort wiggleSort = new WiggleSort();
Integer[] values = {1, 2, 3, 4};
Integer[] result = {2, 4, 1, 3};
wiggleSort.sort(values);
assertArrayEquals(values, result);
}

@Test
void WiggleTestNumbersOdd(){
WiggleSort wiggleSort = new WiggleSort();
Integer[] values = {1, 2, 3, 4, 5};
Integer[] result = {3, 4, 2, 5, 1};
wiggleSort.sort(values);
assertArrayEquals(values, result);

}

@Test
void WiggleTestNumbersOddDuplicates(){
WiggleSort wiggleSort = new WiggleSort();
Integer[] values = {7, 2, 2, 2, 5};
Integer[] result = {2, 7, 2, 5, 2};
wiggleSort.sort(values);
assertArrayEquals(values, result);
}

@Test
void WiggleTestNumbersEvenDuplicates(){
WiggleSort wiggleSort = new WiggleSort();
Integer[] values = {1, 2, 4, 4};
Integer[] result = {2, 4, 1, 4};
wiggleSort.sort(values);
assertArrayEquals(values, result);
}

@Test
void WiggleTestStrings(){
WiggleSort wiggleSort = new WiggleSort();
String[] values = {"a", "b", "d", "c"};
String[] result = {"b", "c", "a", "d"};
wiggleSort.sort(values);
assertArrayEquals(values, result);
}
}