diff --git a/algorithms/Java/README.md b/algorithms/Java/README.md index 0aeeea66..252d6dbe 100644 --- a/algorithms/Java/README.md +++ b/algorithms/Java/README.md @@ -55,6 +55,7 @@ 6. [Quick Sort](sorting/quick-sort.java) 7. [Selection Sort](sorting/selection-sort.java) 8. [Shell Sort](sorting/shell-sort.java) +9. [Cyclic Sort](sorting/cyclic-sort.java) ## Stacks diff --git a/algorithms/Java/sorting/cyclic-sort.java b/algorithms/Java/sorting/cyclic-sort.java new file mode 100644 index 00000000..b6b53915 --- /dev/null +++ b/algorithms/Java/sorting/cyclic-sort.java @@ -0,0 +1,70 @@ + +import java.util.Arrays; + +public class cyclicSort { + public static void main(String[] args) + { + int[] array1 = {4,5,2,3}; + int[] array2 = {2, 1, 5, 2, 9, 1, 2, 3, 3, 4, 4, 5, 6, 7, 8}; + + Sort(array1); + Sort(array2); + + + System.out.println(Arrays.toString(array1)); + System.out.println(Arrays.toString(array2)); + } + + + static void Sort(int[] array) + { + // step 1: loop from the beginning of the array to the second to last item + for (int currentIndex = 0; currentIndex < array.length - 1; currentIndex++) + { + // step 2: save the value of the item at the currentIndex + int item = array[currentIndex]; + // step 3: save a copy of the current index + int currentIndexCopy = currentIndex; + + // step 4: loop through all indexes that proceed the currentIndex + for (int i = currentIndex + 1; i < array.length; i++) + if (array[i] < item) + currentIndexCopy++; + + // step 5: if currentIndexCopy has not changed, the item at the currentIndex is already in the correct position + if (currentIndexCopy == currentIndex) + continue; + + // step 6: skip duplicates + while (item == array[currentIndexCopy]) + currentIndexCopy++; + + // step 7: swap + int temp = array[currentIndexCopy]; + array[currentIndexCopy] = item; + item = temp; + + // step 8: repeat steps 4, 6 and 7 above as long as we can find values to swap + while (currentIndexCopy != currentIndex) + { + + // step 9: save a copy of the currentIndex + currentIndexCopy = currentIndex; + + // step 10: repeat step 4 + for (int i = currentIndex + 1; i < array.length; i++) + if (array[i] < item) + currentIndexCopy++; + + // step 10: repeat step 6 + while (item == array[currentIndexCopy]) + currentIndexCopy++; + + // step 10: repeat step 7 + temp = array[currentIndexCopy]; + array[currentIndexCopy] = item; + item = temp; + } + } + } +}