From 797d731eb0ca1f92290f6ec1633ad73fcd95e082 Mon Sep 17 00:00:00 2001 From: rhinemann Date: Fri, 27 Dec 2024 18:50:35 +0200 Subject: [PATCH] Tests passed. --- .gitignore | 2 ++ CMakeLists.txt | 11 ++++++++ main.cpp | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++ sort.cpp | 50 ++++++++++++++++++++++++++++++++ sort.h | 6 ++++ 5 files changed, 146 insertions(+) create mode 100644 .gitignore create mode 100644 CMakeLists.txt create mode 100644 main.cpp create mode 100644 sort.cpp create mode 100644 sort.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..602b07f --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea +cmake-build-debug/ \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..6d26434 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,11 @@ +cmake_minimum_required(VERSION 3.30) +project(code) + +set(CMAKE_CXX_STANDARD 23) + +add_executable(code main.cpp + sort.cpp + sort.h +) + +target_link_libraries(code gtest gmock pthread) diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..43654df --- /dev/null +++ b/main.cpp @@ -0,0 +1,77 @@ +#include "sort.h" + +#include +#include + +TEST(QuickSortTest, SortsArrayInAscendingOrder) +{ + // Test input array + char* arr[] = { "banana", "apple", "cherry", "date", "elderberry" }; + const size_t length = std::size(arr); + + // Sort the array + quick_sort(arr, length); + + // Check if the array is sorted in ascending order + for (size_t i = 1; i < length; i++) + { + ASSERT_TRUE(strcmp(arr[i - 1], arr[i]) <= 0); + } +} + +TEST(QuickSortTest, SingleElementArray) { + // Test sorting an array with a single element. + char* arr[] = { "hello" }; + quick_sort(arr, 1); + ASSERT_STREQ(arr[0], "hello"); +} + +TEST(QuickSortTest, SortedArray) { + // Test sorting an array that is already sorted. + char* arr[] = { "alpha", "beta", "gamma" }; + quick_sort(arr, 3); + ASSERT_STREQ(arr[0], "alpha"); + ASSERT_STREQ(arr[1], "beta"); + ASSERT_STREQ(arr[2], "gamma"); +} + +TEST(QuickSortTest, ReverseSortedArray) { + // Test sorting an array that is reverse sorted. + char* arr[] = { "gamma", "beta", "alpha" }; + quick_sort(arr, 3); + ASSERT_STREQ(arr[0], "alpha"); + ASSERT_STREQ(arr[1], "beta"); + ASSERT_STREQ(arr[2], "gamma"); +} + +TEST(QuickSortTest, RandomArray) { + // Test sorting an array with random elements. + char* arr[] = { "hello", "world", "foo", "bar", "baz", "qux", "quux", "corge" }; + quick_sort(arr, 8); + ASSERT_STREQ(arr[0], "bar"); + ASSERT_STREQ(arr[1], "baz"); + ASSERT_STREQ(arr[2], "corge"); + ASSERT_STREQ(arr[3], "foo"); + ASSERT_STREQ(arr[4], "hello"); + ASSERT_STREQ(arr[5], "quux"); + ASSERT_STREQ(arr[6], "qux"); + ASSERT_STREQ(arr[7], "world"); +} + +TEST(SwapTest, SwapsValuesCorrectly) +{ + char* a = "apple"; + char* b = "banana"; + + swap(&a, &b); + + EXPECT_STREQ(a, "banana"); + EXPECT_STREQ(b, "apple"); +} + +int main(int argc, char **argv) +{ + ::testing::InitGoogleTest(&argc, argv); + + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/sort.cpp b/sort.cpp new file mode 100644 index 0000000..e613b71 --- /dev/null +++ b/sort.cpp @@ -0,0 +1,50 @@ +#include "sort.h" + +#include + +void swap(char **a, char **b) { + char *temp = *a; + *a = *b; + *b = temp; +} + +int partition(char **arr, const int first, const int last) +{ + const char *pivot = arr[last]; + + int i = first; + for (int j = first; j < last; j++) { + if (strcmp(arr[j], pivot) <= 0) { + swap(&arr[i], &arr[j]); + i++; + } + } + swap(&arr[i], &arr[last]); + return (i); +} + +void quick_sort(char **arr, const size_t length) { + if (length <= 1) { + return; + } + int low = 0, high = length - 1; + int stack[high - low + 1]; + int top = -1; + stack[++top] = low; + stack[++top] = high; + while (top >= 0) { + high = stack[top--]; + low = stack[top--]; + const int pivot_pos = partition(arr, low, high); + + if (pivot_pos - 1 > low) { + stack[++top] = low; + stack[++top] = pivot_pos - 1; + } + + if (pivot_pos + 1 < high) { + stack[++top] = pivot_pos + 1; + stack[++top] = high; + } + } +} diff --git a/sort.h b/sort.h new file mode 100644 index 0000000..2a6e896 --- /dev/null +++ b/sort.h @@ -0,0 +1,6 @@ +#ifndef SORT_H +#define SORT_H +#include +void swap(char** a, char** b); +void quick_sort(char** arr, size_t length); +#endif //SORT_H