From a4298280dddce19d84df6bcff294b4db937c9145 Mon Sep 17 00:00:00 2001 From: Kasun <31097851+KasunAtSLIIT@users.noreply.github.com> Date: Sun, 15 Oct 2023 12:25:08 +0530 Subject: [PATCH] Create CycleSort.java --- CycleSort.java | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 CycleSort.java diff --git a/CycleSort.java b/CycleSort.java new file mode 100644 index 0000000..06b4d53 --- /dev/null +++ b/CycleSort.java @@ -0,0 +1,84 @@ +// Java program to implement cycle sort + +import java.util.*; +import java.lang.*; + +class GFG +{ +// Function sort the array using Cycle sort + public static void cycleSort (int arr[], int n) + { + // count number of memory writes + int writes = 0; + + // traverse array elements and put it to on + // the right place + for (int cycle_start=0; cycle_start<=n-2; cycle_start++) + { + // initialize item as starting point + int item = arr[cycle_start]; + + // Find position where we put the item. We basically + // count all smaller elements on right side of item. + int pos = cycle_start; + for (int i = cycle_start+1; i