diff --git a/16_FirstContribution/SORTING/CycleSort.java b/16_FirstContribution/SORTING/CycleSort.java new file mode 100644 index 0000000..5fd48b8 --- /dev/null +++ b/16_FirstContribution/SORTING/CycleSort.java @@ -0,0 +1,51 @@ +//What is Cycle sort +//When we are given numbers from 1 to N (not necessarily exactly from 1 to N) we use cycle sort. +//It sorts the entire array in one single pass. +//The basic idea behind cycle sort is the sorted array will contain elements on their correct index. +//For eg. - elements from 1 to 5 will be on index 0 to 4 in the sorted array. +//Advantages are :- It sorts the array in-place and does not require additional memory for temporary variables. + +//Here is the link for leetcode question which is solved using cycle sort. +//https://leetcode.com/problems/missing-number/ + +//Here is the link for leetcode question which is solved using cycle sort. +//https://leetcode.com/problems/first-missing-positive/description/ + + +public class CycleSort { + + public static void swap(int arr[],int start,int end) + { + int temp=arr[start]; + arr[start]=arr[end]; + arr[end]=temp; + } + + public static void cycleSort(int arr[]) + { + int i=0; + while(i