├── README.md ├── merge short.cpp └── KMeans Cluster.cpp /README.md: -------------------------------------------------------------------------------- 1 | # Algorithoms -------------------------------------------------------------------------------- /merge short.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | std::vector merge(const std::vector& left, const std::vector& right) { 5 | std::vector result; 6 | int i = 0, j = 0; 7 | 8 | while (i < left.size() && j < right.size()) { 9 | if (left[i] < right[j]) { 10 | result.push_back(left[i++]); 11 | } else { 12 | result.push_back(right[j++]); 13 | } 14 | } 15 | 16 | while (i < left.size()) { 17 | result.push_back(left[i++]); 18 | } 19 | 20 | while (j < right.size()) { 21 | result.push_back(right[j++]); 22 | } 23 | 24 | return result; 25 | } 26 | 27 | 28 | int main() { 29 | std::vector left = {1, 3, 5}; 30 | std::vector right = {2, 4, 6}; 31 | 32 | std::vector merged = merge(left, right); 33 | 34 | std::cout << "Merged Array: "; 35 | for (int num : merged) { 36 | std::cout << num << " "; 37 | } 38 | std::cout << std::endl; 39 | 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /KMeans Cluster.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | struct Point { 8 | double x, y; // Coordinates 9 | 10 | double distance(const Point& p) const { 11 | return std::sqrt((x - p.x) * (x - p.x) + (y - p.y) * (y - p.y)); 12 | } 13 | }; 14 | 15 | class KMeans { 16 | private: 17 | int K, maxIterations; 18 | std::vector centroids; 19 | 20 | public: 21 | KMeans(int K, int maxIterations) : K(K), maxIterations(maxIterations) {} 22 | 23 | int getClosestCentroidIndex(const Point& point) { 24 | double minDistance = std::numeric_limits::max(); 25 | int index = 0; 26 | 27 | for (int i = 0; i < K; ++i) { 28 | double dist = point.distance(centroids[i]); 29 | if (dist < minDistance) { 30 | minDistance = dist; 31 | index = i; 32 | } 33 | } 34 | 35 | return index; 36 | } 37 | 38 | void run(std::vector& points) { 39 | // Random K initial centroids 40 | for (int i = 0; i < K; ++i) { 41 | centroids.push_back(points[rand() % points.size()]); 42 | } 43 | 44 | std::vector> clusters; 45 | clusters.resize(K); 46 | 47 | bool converged = false; 48 | int iteration = 0; 49 | 50 | while (!converged && iteration < maxIterations) { 51 | 52 | for (auto& cluster : clusters) { 53 | cluster.clear(); 54 | } 55 | 56 | 57 | for (const auto& point : points) { 58 | int centroidIndex = getClosestCentroidIndex(point); 59 | clusters[centroidIndex].push_back(point); 60 | } 61 | 62 | 63 | converged = true; 64 | for (int i = 0; i < K; ++i) { 65 | Point newCentroid = {0, 0}; 66 | for (const auto& point : clusters[i]) { 67 | newCentroid.x += point.x; 68 | newCentroid.y += point.y; 69 | } 70 | newCentroid.x /= clusters[i].size(); 71 | newCentroid.y /= clusters[i].size(); 72 | 73 | if (newCentroid.distance(centroids[i]) > 0.001) { 74 | centroids[i] = newCentroid; 75 | converged = false; 76 | } 77 | } 78 | 79 | iteration++; 80 | } 81 | } 82 | 83 | void printCentroids() { 84 | std::cout << "Centroids:\n"; 85 | for (const auto& centroid : centroids) { 86 | std::cout << "(" << centroid.x << ", " << centroid.y << ")\n"; 87 | } 88 | } 89 | }; 90 | 91 | int main() { 92 | std::vector points = {{1, 2}, {1, 4}, {1, 0}, 93 | {10, 2}, {10, 4}, {10, 0}}; 94 | 95 | KMeans kmeans(2, 100); 96 | kmeans.run(points); 97 | kmeans.printCentroids(); 98 | 99 | return 0; 100 | } 101 | --------------------------------------------------------------------------------