diff --git a/de/d07/cycle__sort_8cpp.html b/de/d07/cycle__sort_8cpp.html index 9a514934f..9d20cdd8c 100644 --- a/de/d07/cycle__sort_8cpp.html +++ b/de/d07/cycle__sort_8cpp.html @@ -135,8 +135,7 @@ Functions

Detailed Description

Implementation of Cycle sort algorithm.

-

Cycle Sort is a sorting algorithm that works in \(O(n^2)\) time in best cas and works in \(O(n^2)\) in worst case. If a element is already at its correct position, do nothing. If a element is not at its correct position, we then need to move it to its correct position by computing the correct positions.Therefore, we should make sure the duplicate elements.

-
Author
TsungHan Ho
+

Cycle Sort is a sorting algorithm that works in \(O(n^2)\) time in the best case and works in \(O(n^2)\) in worst case. If a element is already at its correct position, do nothing. If a element is not at its correct position, we then need to move it to its correct position by computing the correct positions.Therefore, we should make sure the duplicate elements.

Author
TsungHan Ho

Function Documentation

◆ cycleSort()

@@ -170,46 +169,53 @@ template<typename T >
Returns
void
-
39  {
-
40  std::vector<T> arr(in_arr);
-
41  for (size_t cycle_start = 0; cycle_start <= arr.size() - 1; cycle_start++) {
-
42  // initialize item
-
43  T item = arr[cycle_start];
-
44 
-
45  // Count the number of elements smaller than item, this number is the
-
46  // correct index of item.
-
47  int pos = cycle_start;
-
48  for (size_t i = cycle_start + 1; i < arr.size(); i++) {
-
49  if (arr[i] < item) {
-
50  pos++;
-
51  }
-
52  }
-
53 
-
54  // item is already in correct position
-
55  if (pos == cycle_start) {
-
56  continue;
-
57  }
-
58 
-
59  // duplicate elements
-
60  while (item == arr[pos]) pos += 1;
-
61  std::swap(item, arr[pos]);
-
62 
-
63  // Rest of the elements
-
64  while (pos != cycle_start) {
-
65  pos = cycle_start;
-
66  // Find position where we put the element
-
67  for (size_t i = cycle_start + 1; i < arr.size(); i++) {
-
68  if (arr[i] < item) {
-
69  pos += 1;
-
70  }
-
71  }
-
72  // duplicate elements
-
73  while (item == arr[pos]) pos += 1;
-
74  std::swap(item, arr[pos]);
-
75  }
-
76  }
-
77  return arr;
-
78 }
+
37  {
+
38  std::vector<T> arr(in_arr);
+
39  for (int cycle_start = 0; cycle_start <= arr.size() - 1; cycle_start++) {
+
40  // initialize item
+
41  T item = arr[cycle_start];
+
42 
+
43  // Count the number of elements smaller than item, this number is the
+
44  // correct index of item.
+
45  int pos = cycle_start;
+
46  for (int i = cycle_start + 1; i < arr.size(); i++) {
+
47  if (arr[i] < item) {
+
48  pos++;
+
49  }
+
50  }
+
51 
+
52  // item is already in correct position
+
53  if (pos == cycle_start) {
+
54  continue;
+
55  }
+
56 
+
57  // duplicate elements
+
58  while (item == arr[pos]) pos += 1;
+
59  if (pos == cycle_start) {
+
60  continue;
+
61  } else {
+
62  std::swap(item, arr[pos]);
+
63  }
+
64  // Rest of the elements
+
65  while (pos != cycle_start) {
+
66  pos = cycle_start;
+
67  // Find position where we put the element
+
68  for (size_t i = cycle_start + 1; i < arr.size(); i++) {
+
69  if (arr[i] < item) {
+
70  pos += 1;
+
71  }
+
72  }
+
73  // duplicate elements
+
74  while (item == arr[pos]) pos += 1;
+
75  if (item == arr[pos]) {
+
76  continue;
+
77  } else {
+
78  std::swap(item, arr[pos]);
+
79  }
+
80  }
+
81  }
+
82  return arr;
+
83 }
T swap(T... args)
@@ -239,11 +245,11 @@ Here is the call graph for this function:

Main function.

Returns
0 on exit
-
107  {
-
108  test(); // execute the test
-
109  return 0;
-
110 }
-
static void test()
Test implementations.
Definition: cycle_sort.cpp:86
+
127  {
+
128  test(); // execute the test
+
129  return 0;
+
130 }
+
static void test()
Test implementations.
Definition: cycle_sort.cpp:91
Here is the call graph for this function:
@@ -278,25 +284,40 @@ Here is the call graph for this function:

Test implementations.

Returns
void
-
86  {
-
87  // [506, 48, 123, 79, 0, 362, 951, 500, 0] return [0, 0, 48, 79, 123, 362,
-
88  // 500, 506, 951]
-
89  std::vector<int> array1 = {506, 48, 123, 79, 0, 362, 951, 500, 0};
-
90  std::cout << "Test 1... ";
- -
92  assert(std::is_sorted(std::begin(arr1), std::end(arr1)));
-
93  std::cout << "passed" << std::endl;
-
94 
-
95  // [4.3, -6.5, -7.4, 0, 2.7, 1.8] return [-7.4, -6.5, 0, 1.8, 2.7, 4.3]
-
96  std::vector<double> array2 = {4.3, -6.5, -7.4, 0, 2.7, 1.8};
-
97  std::cout << "Test 2... ";
- -
99  assert(std::is_sorted(std::begin(arr2), std::end(arr2)));
-
100  std::cout << "passed" << std::endl;
-
101 }
+
91  {
+
92  // Test 1
+
93  // [4, 3, 2, 1] return [1, 2, 3, 4]
+
94  std::vector<uint32_t> array1 = {4, 3, 2, 1};
+
95  std::cout << "Test 1... ";
+ +
97  assert(std::is_sorted(std::begin(arr1), std::end(arr1)));
+
98  std::cout << "passed" << std::endl;
+
99 
+
100  // [4.3, -6.5, -7.4, 0, 2.7, 1.8] return [-7.4, -6.5, 0, 1.8, 2.7, 4.3]
+
101  std::vector<double> array2 = {4.3, -6.5, -7.4, 0, 2.7, 1.8};
+
102  std::cout << "Test 2... ";
+ +
104  assert(std::is_sorted(std::begin(arr2), std::end(arr2)));
+
105  std::cout << "passed" << std::endl;
+
106 
+
107  // Test 3
+
108  // [3, 3, 3, 3] return [3, 3, 3, 3]
+
109  std::vector<uint32_t> array3 = {3, 3, 3, 3};
+
110  std::cout << "Test 3... ";
+ +
112  assert(std::is_sorted(std::begin(arr3), std::end(arr3)));
+
113  std::cout << "passed" << std::endl;
+
114 
+
115  // [9, 4, 6, 8, 14, 3] return [9, 4, 6, 8, 14, 3]
+
116  std::vector<uint32_t> array4 = {3, 4, 6, 8, 9, 14};
+
117  std::cout << "Test 4... ";
+ +
119  assert(std::is_sorted(std::begin(arr4), std::end(arr4)));
+
120  std::cout << "passed" << std::endl;
+
121 }
T begin(T... args)
-
std::vector< T > cycleSort(const std::vector< T > &in_arr)
The main function implements cycleSort.
Definition: cycle_sort.cpp:39
+
std::vector< T > cycleSort(const std::vector< T > &in_arr)
The main function implements cycleSort.
Definition: cycle_sort.cpp:37
T end(T... args)
T endl(T... args)
T is_sorted(T... args)