Skip to content

Commit

Permalink
Create Cycle_Sort.c (jainaman224#3058)
Browse files Browse the repository at this point in the history
  • Loading branch information
saloniankita authored Jun 4, 2020
1 parent 1700661 commit 7029a6d
Showing 1 changed file with 78 additions and 0 deletions.
78 changes: 78 additions & 0 deletions Cycle_Sort.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Cycle sort is an in-place and unstable sorting algorithm, a comparison sort that is based on the idea that array to be sorted can be divided into cycles.

# include <stdio.h>

// Cycle Sort begins
void cyclesort(int a[], int n)
{
int write = 0, start, element, pos, temp, i;

// Finding the position where we put the element.
for (start = 0 ; start <= n - 2 ; start++)
{
element = a[start];
pos = start;

for (i = start + 1 ; i < n ; i++)
if (a[i] < element)
pos++;

// Checking if element is already in correct position
if (pos == start)
continue;

while (element == a[pos])
pos += 1;

// Putting the element to their right position
if (pos != start)
{
temp = element;
element = a[pos];
a[pos] = temp;
write++;
}

// Rotating rest of the cycle
while (pos != start) {
pos = start;
for (i = start + 1 ; i < n ; i++)
if (a[i] < element)
pos += 1;

while (element == a[pos])
pos += 1;

// Putting the element to their right position
if (element != a[pos])
{
temp = element;
element = a[pos];
a[pos] = temp;
write++;
}
}
}
}

int main()
{ int n,i;
int a[20];
printf("Enter the no. of elements : ");
scanf("%d",&n);
printf("Enter the elements : ");
for(i = 0 ; i < n ; i++)
scanf("%d ",a[i]);
cyclesort(a, n);
printf("After sorting : ");
for (i = 0 ; i < n ; i++)
printf("%d ",a[i]);
return 0;
}

/* Sample Output:
Enter the no. of elements : 4
Enter the elements : 5 8 2 3
After sorting : 2 3 5 8
*/

0 comments on commit 7029a6d

Please sign in to comment.