diff --git a/Sorts/CycleSort.java b/Sorts/CycleSort.java new file mode 100644 index 000000000000..201c007d9a59 --- /dev/null +++ b/Sorts/CycleSort.java @@ -0,0 +1,114 @@ +/** + * + * @author Kriztoper Urmeneta (https://github.com/Kriztoper) + * + */ + +class CycleSort +{ + /** + * This method implements the Generic Cycle Sort + * + * @param array The array to be sorted + * @param last The count of total number of elements in array + * Sorts the array in increasing order + **/ + + public static > void CS(T array[], int last) { + //Sorting + // count number of memory writes + int writes = 0; + + // traverse array elements and put it to on + // the right place + for (int cycleStart = 0; cycleStart <= last - 2; cycleStart++) { + // initialize item as starting point + T item = array[cycleStart]; + + // Find position where we put the item. We basically + // count all smaller elements on right side of item. + int pos = cycleStart; + for (int i = cycleStart + 1; i < last; i++) { + if (array[i].compareTo(item) < 0) { + pos++; + } + } + + // If item is already in correct position + if (pos == cycleStart) { + continue; + } + + // ignore all duplicate elements + while (item.compareTo(array[pos]) == 0) { + pos++; + } + + // put the item to it's right position + if (pos != cycleStart) { + T temp = item; + item = array[pos]; + array[pos] = temp; + writes++; + } + + // Rotate rest of the cycle + while (pos != cycleStart) { + pos = cycleStart; + + // Find position where we put the element + for (int i = cycleStart + 1; i < last; i++) { + if (array[i].compareTo(item) < 0) { + pos += 1; + } + } + + // ignore all duplicate elements + while (item.compareTo(array[pos]) == 0) { + pos++; + } + + // put the item to it's right position + if (item.compareTo(array[pos]) != 0) { + T temp = item; + item = array[pos]; + array[pos] = temp; + writes++; + } + } + } + } + + // Driver Program + public static void main(String[] args) + { + // Integer Input + int[] arr1 = {4,23,6,78,1,54,231,9,12}; + int last = arr1.length; + Integer[] array = new Integer[last]; + for (int i=0;i 1 4 6 9 12 23 54 78 231 + for(int i=0; i a b c d e + for(int i=0; i pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy