Skip to content

Commit

Permalink
Create CycleSort
Browse files Browse the repository at this point in the history
Explanation: Cycle Sort is an in-place, non-comparing sorting algorithm. It minimizes the number of writes to the original array, making it useful when writing to the data is costly. It works by selecting an item and finding its correct position in the sorted part of the array, then repeating this process for the remaining items.
  • Loading branch information
yellurividyendra authored Oct 1, 2023
1 parent 6376bf4 commit 4928c3f
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions sorting/CycleSort
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
-->Explanation: Cycle Sort is an in-place, non-comparing sorting algorithm. It minimizes the number of writes to the original array,
-->making it useful when writing to the data is costly.
-->It works by selecting an item and finding its correct position in the sorted part of the array, then repeating this process for the remaining items.

#include <iostream>
using namespace std;
void cycleSort(int arr[], int n) {
for (int cycleStart = 0; cycleStart < n - 1; cycleStart++) {
int item = arr[cycleStart];
int pos = cycleStart;
for (int i = cycleStart + 1; i < n; i++) {
if (arr[i] < item) {
pos++;
}
}
if (pos == cycleStart) {
continue;
}
while (item == arr[pos]) {
pos++;
}
swap(item, arr[pos]);
while (pos != cycleStart) {
pos = cycleStart;
for (int i = cycleStart + 1; i < n; i++) {
if (arr[i] < item) {
pos++;
}
}
while (item == arr[pos]) {
pos++;
}
swap(item, arr[pos]);
}
}
}

int main() {
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[n];
cout << "Enter the elements: ";
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
cycleSort(arr, n);
cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}

0 comments on commit 4928c3f

Please sign in to comment.