-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patha41cyclesSort.java
More file actions
76 lines (64 loc) · 3.13 KB
/
a41cyclesSort.java
File metadata and controls
76 lines (64 loc) · 3.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
// Class to demonstrate Cyclic Sort for any variation
public class a41cyclesSort {
// Method to perform cyclic sort on the given array
public static void cyclicSort(int arr[]) {
int n = arr.length; // Get the length of the array
// Iterate through the array to place each element at its correct position
for (int cycles = 0; cycles < n - 1; cycles++) {
int pos = cycles; // Initial position for the current cycle
int item = arr[cycles]; // Store the current element
// Finding the correct position for the current element
for (int i = cycles + 1; i < n; i++) {
if (arr[i] < item) {
pos++; // Increment position for each element smaller than the current element
}
}
// If the element is already at its correct position
if (pos == cycles) {
continue;
}
// If duplicates are present, skip them
while (arr[pos] == item) {
pos++;
}
// If the element is not at its correct position, swap it
if (pos != cycles) {
int temp = arr[pos]; // Temporary storage for the element at the current position
arr[pos] = item; // Place the current element at its correct position
item = temp; // Update the item with the swapped element
}
// Check if more swaps are needed to place the element correctly
while (pos != cycles) {
pos = cycles; // Reset position for the next cycle
// Finding the correct position for the current element
for (int i = cycles + 1; i < n; i++) {
if (arr[i] < item) {
pos++; // Increment position for each element smaller than the current element
}
}
// If duplicates are present, skip them
while (arr[pos] == item) {
pos++;
}
// If the element is not at its correct position, swap it
if (item != arr[pos]) {
int temp = arr[pos]; // Temporary storage for the element at the current position
arr[pos] = item; // Place the current element at its correct position
item = temp; // Update the item with the swapped element
}
}
}
}
// Method to print the elements of the array
public static void printArray(int arr[]) {
for (int i = 0; i < arr.length; i++) { // Iterate through the array
System.out.print(arr[i] + " "); // Print each element followed by a space
}
}
// Main method to run the Cyclic Sort algorithm
public static void main(String[] args) {
int[] arr = {4, 2, 1, 3, 5, 6, 7, 8, 10, 9}; // Initialize an array with unsorted elements
cyclicSort(arr); // Call the cyclicSort method to sort the array
printArray(arr); // Print the sorted array
}
}