2019-10-30 13:20:50 +03:00
|
|
|
// Sorting of array list using cycle sort
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
// Displays the array, passed to this method
|
2020-04-24 03:08:15 +03:00
|
|
|
void display(int *arr, int n)
|
|
|
|
{
|
2019-10-30 13:20:50 +03:00
|
|
|
int i;
|
2020-04-24 03:08:15 +03:00
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
2019-10-30 13:20:50 +03:00
|
|
|
printf("%d ", arr[i]);
|
|
|
|
}
|
2020-04-24 03:08:15 +03:00
|
|
|
|
2019-10-30 13:20:50 +03:00
|
|
|
printf("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap function to swap two values
|
2020-04-24 03:08:15 +03:00
|
|
|
void swap(int *first, int *second)
|
|
|
|
{
|
2019-10-30 13:20:50 +03:00
|
|
|
int temp = *first;
|
|
|
|
*first = *second;
|
|
|
|
*second = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Function sort the array using Cycle sort
|
2020-04-24 03:08:15 +03:00
|
|
|
void cycleSort(int *arr, int n)
|
2019-10-30 13:20:50 +03:00
|
|
|
{
|
|
|
|
// count number of memory writes
|
|
|
|
int writes = 0;
|
|
|
|
|
|
|
|
// traverse array elements and put it to on
|
|
|
|
// the right place
|
2020-04-24 03:08:15 +03:00
|
|
|
for (int cycle_start = 0; cycle_start <= n - 2; cycle_start++)
|
|
|
|
{
|
2019-10-30 13:20:50 +03:00
|
|
|
// initialize item as starting point
|
|
|
|
int item = arr[cycle_start];
|
|
|
|
|
|
|
|
// Find position where we put the item. We basically
|
|
|
|
// count all smaller elements on right side of item.
|
|
|
|
int pos = cycle_start;
|
|
|
|
for (int i = cycle_start + 1; i < n; i++)
|
|
|
|
if (arr[i] < item)
|
|
|
|
pos++;
|
|
|
|
|
|
|
|
// If item is already in correct position
|
|
|
|
if (pos == cycle_start)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// ignore all duplicate elements
|
2020-06-28 18:25:37 +03:00
|
|
|
while (item == arr[pos]) pos += 1;
|
2019-10-30 13:20:50 +03:00
|
|
|
|
|
|
|
// put the item to it's right position
|
2020-04-24 03:08:15 +03:00
|
|
|
if (pos != cycle_start)
|
|
|
|
{
|
2019-10-30 13:20:50 +03:00
|
|
|
swap(&item, &arr[pos]);
|
|
|
|
writes++;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Rotate rest of the cycle
|
2020-04-24 03:08:15 +03:00
|
|
|
while (pos != cycle_start)
|
|
|
|
{
|
2019-10-30 13:20:50 +03:00
|
|
|
pos = cycle_start;
|
|
|
|
|
|
|
|
// Find position where we put the element
|
|
|
|
for (int i = cycle_start + 1; i < n; i++)
|
|
|
|
if (arr[i] < item)
|
|
|
|
pos += 1;
|
|
|
|
|
|
|
|
// ignore all duplicate elements
|
2020-06-28 18:25:37 +03:00
|
|
|
while (item == arr[pos]) pos += 1;
|
2019-10-30 13:20:50 +03:00
|
|
|
|
|
|
|
// put the item to it's right position
|
2020-04-24 03:08:15 +03:00
|
|
|
if (item != arr[pos])
|
|
|
|
{
|
2019-10-30 13:20:50 +03:00
|
|
|
swap(&item, &arr[pos]);
|
|
|
|
writes++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Driver program to test above function
|
|
|
|
int main()
|
|
|
|
{
|
2020-06-28 18:25:37 +03:00
|
|
|
int n; // Size of array elements
|
2019-10-30 13:20:50 +03:00
|
|
|
|
|
|
|
printf("Enter size of array:\n");
|
2020-06-28 18:25:37 +03:00
|
|
|
scanf("%d", &n); // E.g. 8
|
2020-04-24 03:08:15 +03:00
|
|
|
|
2019-10-30 13:20:50 +03:00
|
|
|
printf("Enter the elements of the array\n");
|
|
|
|
int i;
|
2020-04-24 03:08:15 +03:00
|
|
|
int *arr = (int *)malloc(n * sizeof(int));
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
scanf("%d", &arr[i]);
|
2019-10-30 13:20:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("Original array: ");
|
|
|
|
display(arr, n);
|
|
|
|
|
|
|
|
cycleSort(arr, n);
|
|
|
|
printf("Sorted array: ");
|
|
|
|
display(arr, n);
|
|
|
|
|
2020-04-24 03:08:15 +03:00
|
|
|
free(arr);
|
2019-10-30 13:20:50 +03:00
|
|
|
return 0;
|
|
|
|
}
|