Skip to content

Commit

Permalink
missing cstdint include
Browse files Browse the repository at this point in the history
  • Loading branch information
yahya-mohammed07 committed Oct 1, 2023
1 parent fdcb5d1 commit d1ce9b6
Showing 1 changed file with 59 additions and 59 deletions.
118 changes: 59 additions & 59 deletions sorting/selection_sort_iterative.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
*******************************************************************************/
#include <algorithm> /// for std::is_sorted
#include <cassert> /// for std::assert
#include <cstdint> /// for uint64_t
#include <iostream> /// for IO operations
#include <vector> /// for std::vector
#include <cstdint> /// for uint64_t
#include <iostream> /// for IO operations
#include <vector> /// for std::vector

/******************************************************************************
* @namespace sorting
Expand All @@ -47,25 +47,25 @@ namespace sorting {

std::vector<uint64_t> selectionSort(const std::vector<uint64_t> &arr,
const uint64_t len) {
std::vector<uint64_t> array(
arr.begin(),
arr.end()); // declare a vector in which result will be stored
for (uint64_t it = 0; it < len; ++it) {
uint64_t min = it; // set min value
for (uint64_t it2 = it + 1; it2 < len; ++it2) {
if (array[it2] < array[min]) { // check which element is smaller
min = it2; // store index of smallest element to min
}
}
std::vector<uint64_t> array(
arr.begin(),
arr.end()); // declare a vector in which result will be stored
for (uint64_t it = 0; it < len; ++it) {
uint64_t min = it; // set min value
for (uint64_t it2 = it + 1; it2 < len; ++it2) {
if (array[it2] < array[min]) { // check which element is smaller
min = it2; // store index of smallest element to min
}
}

if (min != it) { // swap if min does not match to i
uint64_t tmp = array[min];
array[min] = array[it];
array[it] = tmp;
if (min != it) { // swap if min does not match to i
uint64_t tmp = array[min];
array[min] = array[it];
array[it] = tmp;
}
}
}

return array; // return sorted vector
return array; // return sorted vector
}
} // namespace sorting

Expand All @@ -74,54 +74,54 @@ std::vector<uint64_t> selectionSort(const std::vector<uint64_t> &arr,
* @returns void
*******************************************************************************/
static void test() {
// testcase #1
// [1, 0, 0, 1, 1, 0, 2, 1] returns [0, 0, 0, 1, 1, 1, 1, 2]
std::vector<uint64_t> vector1 = {1, 0, 0, 1, 1, 0, 2, 1};
uint64_t vector1size = vector1.size();
std::cout << "1st test... ";
std::vector<uint64_t> result_test1;
result_test1 = sorting::selectionSort(vector1, vector1size);
assert(std::is_sorted(result_test1.begin(), result_test1.end()));
std::cout << "Passed" << std::endl;
// testcase #1
// [1, 0, 0, 1, 1, 0, 2, 1] returns [0, 0, 0, 1, 1, 1, 1, 2]
std::vector<uint64_t> vector1 = {1, 0, 0, 1, 1, 0, 2, 1};
uint64_t vector1size = vector1.size();
std::cout << "1st test... ";
std::vector<uint64_t> result_test1;
result_test1 = sorting::selectionSort(vector1, vector1size);
assert(std::is_sorted(result_test1.begin(), result_test1.end()));
std::cout << "Passed" << std::endl;

// testcase #2
// [19, 22, 540, 241, 156, 140, 12, 1] returns [1, 12, 19, 22, 140, 156,
// 241,540]
std::vector<uint64_t> vector2 = {19, 22, 540, 241, 156, 140, 12, 1};
uint64_t vector2size = vector2.size();
std::cout << "2nd test... ";
std::vector<uint64_t> result_test2;
result_test2 = sorting::selectionSort(vector2, vector2size);
assert(std::is_sorted(result_test2.begin(), result_test2.end()));
std::cout << "Passed" << std::endl;
// testcase #2
// [19, 22, 540, 241, 156, 140, 12, 1] returns [1, 12, 19, 22, 140, 156,
// 241,540]
std::vector<uint64_t> vector2 = {19, 22, 540, 241, 156, 140, 12, 1};
uint64_t vector2size = vector2.size();
std::cout << "2nd test... ";
std::vector<uint64_t> result_test2;
result_test2 = sorting::selectionSort(vector2, vector2size);
assert(std::is_sorted(result_test2.begin(), result_test2.end()));
std::cout << "Passed" << std::endl;

// testcase #3
// [11, 20, 30, 41, 15, 60, 82, 15] returns [11, 15, 15, 20, 30, 41, 60, 82]
std::vector<uint64_t> vector3 = {11, 20, 30, 41, 15, 60, 82, 15};
uint64_t vector3size = vector3.size();
std::cout << "3rd test... ";
std::vector<uint64_t> result_test3;
result_test3 = sorting::selectionSort(vector3, vector3size);
assert(std::is_sorted(result_test3.begin(), result_test3.end()));
std::cout << "Passed" << std::endl;
// testcase #3
// [11, 20, 30, 41, 15, 60, 82, 15] returns [11, 15, 15, 20, 30, 41, 60, 82]
std::vector<uint64_t> vector3 = {11, 20, 30, 41, 15, 60, 82, 15};
uint64_t vector3size = vector3.size();
std::cout << "3rd test... ";
std::vector<uint64_t> result_test3;
result_test3 = sorting::selectionSort(vector3, vector3size);
assert(std::is_sorted(result_test3.begin(), result_test3.end()));
std::cout << "Passed" << std::endl;

// testcase #4
// [1, 9, 11, 546, 26, 65, 212, 14, -11] returns [-11, 1, 9, 11, 14, 26, 65,
// 212, 546]
std::vector<uint64_t> vector4 = {1, 9, 11, 546, 26, 65, 212, 14};
uint64_t vector4size = vector2.size();
std::cout << "4th test... ";
std::vector<uint64_t> result_test4;
result_test4 = sorting::selectionSort(vector4, vector4size);
assert(std::is_sorted(result_test4.begin(), result_test4.end()));
std::cout << "Passed" << std::endl;
// testcase #4
// [1, 9, 11, 546, 26, 65, 212, 14, -11] returns [-11, 1, 9, 11, 14, 26, 65,
// 212, 546]
std::vector<uint64_t> vector4 = {1, 9, 11, 546, 26, 65, 212, 14};
uint64_t vector4size = vector2.size();
std::cout << "4th test... ";
std::vector<uint64_t> result_test4;
result_test4 = sorting::selectionSort(vector4, vector4size);
assert(std::is_sorted(result_test4.begin(), result_test4.end()));
std::cout << "Passed" << std::endl;
}

/*******************************************************************************
* @brief Main function
* @returns 0 on exit
*******************************************************************************/
int main() {
test(); // run self-test implementations
return 0;
test(); // run self-test implementations
return 0;
}

0 comments on commit d1ce9b6

Please sign in to comment.