This repository has been archived by the owner on May 29, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 775
/
cycle-sort.cpp
127 lines (102 loc) · 4.37 KB
/
cycle-sort.cpp
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
// ***Cycle Sort***
// This sorting algorithm is best suited for situations where memory write or swap operations are costly.
// it is In-place and not stable
#include<bits/stdc++.h>
using namespace std;
void cycleSort(int arr[], int size)
{
for(int cyclestart=0;cyclestart<size-1;cyclestart++){
int item=arr[cyclestart];
int pos=cyclestart;
for(int i=cyclestart+1;i<size;i++) //count total no of item less then curr item in an array
if(arr[i]<item)
pos++;
swap(item,arr[pos]); //swap item to it's correct position
while(pos!=cyclestart){ // now we have to find the index of prev item which is presnt at pos and we keep doing this till pos!=cyclestart
pos=cyclestart;
for(int i=cyclestart+1;i<size;i++)
if(arr[i]<item)
pos++;
swap(item,arr[pos]);
}
}
}
int main()
{
int arr[] = { 20,40,10,50,30 };
int size = sizeof(arr) / sizeof(arr[0]);
cycleSort(arr, size);
for (int i = 0; i < size; i++)
cout << arr[i] << " ";
return 0;
}
/*
Time Complexity : O(n2)
Worst Case : O(n2)
Average Case: O(n2)
Best Case : O(n2)
Explanation:-
int arr[] = { 20,40,10,50,30};
int size= 5;
iteration 1:
cyclestart=0;
item = 20;
pos = 0;
for(int i=cyclestart+1;i<size;i++) //count total no of item less then 20 in a array to find correct index of 20 in array
if(arr[i]<item)
pos++;
pos=1 //index of 20 is 1(pos) in a array
swap(item,arr[pos]); //swap 20(item) with item present at index pos i.e 40.
item=40;
pos=1;
//Now we have to count item less then 40 in array and swap that item with updated pos
pos=3
swap(40,arr[3]) // swap 40 with 50
item=50;
pos=3
//Now we have to count item less then 50 in array and swap that item with updated pos
pos=4
swap(50,arr[4]) //swap 50 with 30
item=30;
pos=4;
//Now we have to count item less then 30 in array and swap that item with updated pos
pos=2;
swap(30,arr[2]) //swap 30 with 10
item=10;
pos=2;
//Now we have to count item less then 10 in array and swap that item with updated pos
pos=0;
swap(10,arr[0]) //place 10 at 0index
pos=cyclestart while-loop terminate
Updated array after iterative 1 array = {10,20,30,40,50};
iteration 2:
cyclestart=1;
item = 20;
pos = 1;
for(int i=cyclestart+1;i<size;i++) //count total no of item less then 20 in a array to find correct index of 20 in array
if(arr[i]<item
pos++;
pos=1
swap(item,arr[pos]);
pos=cyclestart while-loop terminate
iteration 3:
cyclestart=2;
item = 30;
pos = 2;
for(int i=cyclestart+1;i<size;i++) //count total no of item less then 30 in a array to find correct index of 20 in array
if(arr[i]<item)
pos++;
pos=2
swap(item,arr[pos]);
pos=cyclestart while-loop terminate
iteration 4:
cyclestart=3;
item = 40;
pos = 3;
for(int i=cyclestart+1;i<size;i++) //count total no of item less then 40 in a array to find correct index of 20 in array
if(arr[i]<item)
pos++;
pos=3
swap(item,arr[pos]);
pos=cyclestart while-loop terminate
*/