Skip to content

Commit

Permalink
rename in task_06
Browse files Browse the repository at this point in the history
  • Loading branch information
Mathew131 committed Jun 16, 2024
1 parent 2cd7fd8 commit a0dfea2
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 25 deletions.
38 changes: 19 additions & 19 deletions task_06/src/task_06.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,38 @@

#include <vector>

int partition(std::vector<int>& arr, int left, int right) {
int pivotIndex = left + (right - left) / 2;
int pivotValue = arr[pivotIndex];
std::swap(arr[pivotIndex], arr[right]);
int storeIndex = left;
int Partition(std::vector<int>& arr, int left, int right) {
int pivot_index = left + (right - left) / 2;
int pivot_value = arr[pivot_index];
std::swap(arr[pivot_index], arr[right]);
int store_index = left;
for (int i = left; i < right; ++i) {
if (arr[i] < pivotValue) {
std::swap(arr[i], arr[storeIndex]);
++storeIndex;
if (arr[i] < pivot_value) {
std::swap(arr[i], arr[store_index]);
++store_index;
}
}
std::swap(arr[storeIndex], arr[right]);
return storeIndex;
std::swap(arr[store_index], arr[right]);
return store_index;
}

int quickSelect(std::vector<int>& arr, int left, int right, int k) {
int QuickSelect(std::vector<int>& arr, int left, int right, int k) {
while (left <= right) {
int pivotIndex = partition(arr, left, right);
if (pivotIndex == k) {
return arr[pivotIndex];
} else if (pivotIndex < k) {
left = pivotIndex + 1;
int pivot_index = Partition(arr, left, right);
if (pivot_index == k) {
return arr[pivot_index];
} else if (pivot_index < k) {
left = pivot_index + 1;
} else {
right = pivotIndex - 1;
right = pivot_index - 1;
}
}
return -1;
}

int findNthOrder(std::vector<int> arr, int k) {
int FindNthOrder(std::vector<int> arr, int k) {
if (arr.size() == 0) {
throw std::invalid_argument("vector must be not empty");
}
return quickSelect(arr, 0, arr.size() - 1, k - 1);
return QuickSelect(arr, 0, arr.size() - 1, k - 1);
}
12 changes: 6 additions & 6 deletions task_06/src/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@

#include "task_06.hpp"

TEST(findNthOrder, Case1) {
ASSERT_EQ(findNthOrder(std::vector<int>{73, 74, 75, 71, 69, 72, 76, 73}, 2),
TEST(FindNthOrder, Case1) {
ASSERT_EQ(FindNthOrder(std::vector<int>{73, 74, 75, 71, 69, 72, 76, 73}, 2),
71);
}

TEST(findNthOrder, Case2) {
ASSERT_EQ(findNthOrder(std::vector<int>{30, 40, 50, 60}, 1), 30);
ASSERT_EQ(FindNthOrder(std::vector<int>{30, 40, 50, 60}, 1), 30);
}

TEST(findNthOrder, Case3) {
ASSERT_EQ(findNthOrder(std::vector<int>{30, 60, 90, 70, 80, 40, 30}, 4), 60);
ASSERT_EQ(FindNthOrder(std::vector<int>{30, 60, 90, 70, 80, 40, 30}, 4), 60);
}

TEST(findNthOrder, Case4) {
ASSERT_EQ(findNthOrder(std::vector<int>{55, 55, 55, 55, 55}, 5), 55);
ASSERT_EQ(FindNthOrder(std::vector<int>{55, 55, 55, 55, 55}, 5), 55);
}

TEST(findNthOrder, Case5) {
EXPECT_THROW(findNthOrder(std::vector<int>{}, 5), std::invalid_argument);
EXPECT_THROW(FindNthOrder(std::vector<int>{}, 5), std::invalid_argument);
}

0 comments on commit a0dfea2

Please sign in to comment.