From 7bb484d3667eb5f452d954da10c48ae481dd84e5 Mon Sep 17 00:00:00 2001 From: milishparsai007 Date: Tue, 24 Oct 2023 09:09:04 +0530 Subject: [PATCH 1/2] Added cycle sort --- 16_FirstContribution/SORTING/CycleSort.java | 48 +++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 16_FirstContribution/SORTING/CycleSort.java diff --git a/16_FirstContribution/SORTING/CycleSort.java b/16_FirstContribution/SORTING/CycleSort.java new file mode 100644 index 0000000..75f8704 --- /dev/null +++ b/16_FirstContribution/SORTING/CycleSort.java @@ -0,0 +1,48 @@ +//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/ + + +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 Date: Tue, 24 Oct 2023 09:12:14 +0530 Subject: [PATCH 2/2] Added cycle sort --- 16_FirstContribution/SORTING/CycleSort.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/16_FirstContribution/SORTING/CycleSort.java b/16_FirstContribution/SORTING/CycleSort.java index 75f8704..5fd48b8 100644 --- a/16_FirstContribution/SORTING/CycleSort.java +++ b/16_FirstContribution/SORTING/CycleSort.java @@ -8,6 +8,9 @@ //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 {