Skip to content

Commit 645ecdd

Browse files
committed
Add Cycle Sort
1 parent db727d1 commit 645ecdd

File tree

5 files changed

+106
-2
lines changed

5 files changed

+106
-2
lines changed

src/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ public class Main {
77
public static void main(String[] args) {
88
int[] arr = {5, 3, 4, 2, 1};
99

10-
SortingService.sort(arr, SortingAlgorithms.INSERTION_SORT);
10+
SortingService.sort(arr, SortingAlgorithms.CYCLE_SORT);
1111

1212
System.out.println(Arrays.toString(arr));
1313
}

src/sort/SortingAlgorithms.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
public enum SortingAlgorithms {
44
BUBBLE_SORT,
55
SELECTION_SORT,
6-
INSERTION_SORT
6+
INSERTION_SORT,
7+
CYCLE_SORT
78
}

src/sort/SortingService.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package sort;
22

33
import sort.bubble.BubbleSort;
4+
import sort.cycle.CycleSort;
45
import sort.insertion.InsertionSort;
56
import sort.selection.SelectionSort;
67

@@ -20,6 +21,9 @@ public static void sort(int[] arr, SortingAlgorithms sortAlgorithms) {
2021
case INSERTION_SORT:
2122
InsertionSort.sort(arr);
2223
break;
24+
case CYCLE_SORT:
25+
CycleSort.sort(arr);
26+
break;
2327
}
2428
}
2529
}

src/sort/cycle/CycleSort.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package sort.cycle;
2+
3+
public class CycleSort {
4+
public static void sort(int[] arr) {
5+
int n = arr.length;
6+
7+
// loop to start a cycle from each element
8+
for (int cs = 0; cs < n; cs++) {
9+
int item = arr[cs];
10+
int pos = cs;
11+
12+
// count no of the smallest element from this position
13+
for (int j = cs + 1; j < n; j++) {
14+
if (arr[j] < item)
15+
pos++;
16+
}
17+
18+
// if pos == cs means element is already at correct place
19+
if (pos == cs)
20+
continue;
21+
22+
// ignore duplicates
23+
while (item == arr[pos])
24+
pos++;
25+
26+
// swap if smaller elements are found
27+
if (pos != cs) {
28+
int temp = arr[pos];
29+
arr[pos] = item;
30+
item = temp;
31+
}
32+
33+
while (pos != cs) {
34+
pos = cs;
35+
36+
// count no of the smallest element from this position
37+
for (int j = cs + 1; j < n; j++) {
38+
if (arr[j] < item)
39+
pos++;
40+
}
41+
42+
// ignore duplicates
43+
while (item == arr[pos])
44+
pos++;
45+
46+
// swap if smaller elements are found
47+
if (item != arr[pos]) {
48+
int temp = arr[pos];
49+
arr[pos] = item;
50+
item = temp;
51+
}
52+
}
53+
}
54+
}
55+
}
56+

src/sort/cycle/README.MD

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Cycle Sort
2+
*In each cycle the smallest element will be moved to its correct place to the left*
3+
4+
### How It Works: [Link to Video](https://www.youtube.com/watch?v=vJn71L7CPH4)
5+
6+
7+
### Time Complexity
8+
9+
---
10+
1. **Worst Case Complexity: O(n<sup>2</sup>)**
11+
If we want to sort in ascending order and the array is in descending order then, the worst case occurs.
12+
2. **Best Case Complexity: O(n<sup>2</sup>)**
13+
It occurs when the array is already sorted
14+
3. **Average Case Complexity: O(n<sup>2</sup>)**
15+
It occurs when the elements of the array are in jumbled order (neither ascending nor descending).
16+
17+
18+
### Space Complexity
19+
20+
---
21+
1. The space complexity is constant cause this algorithm is in place so it does not use any extra memory to sort.
22+
23+
### Properties
24+
1. Comparison Based
25+
2. Not Stable
26+
3. Inplace
27+
4. Makes min memory writes as compare to all other sorting algorithms
28+
29+
### Cycle Sort Applications
30+
1. This sorting algorithm is best suited for situations where memory write or swap operations are costly.
31+
2. Useful for complex problems.
32+
33+
34+
### Advantages
35+
1. No additional storage is required.
36+
2. In-Place sorting algorithm.
37+
3. A minimum number of writes to the memory
38+
4. Cycle sort is useful when the array is stored in EEPROM or FLASH.
39+
40+
### Disadvantages
41+
1. It is not mostly used.
42+
2. It has more time complexity O(n<sup>2</sup>)
43+
3. Unstable sorting algorithm.

0 commit comments

Comments
 (0)