Home >Backend Development >C++ >How to Sort a 2D Array by Column Values in C ?

How to Sort a 2D Array by Column Values in C ?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-11-08 12:24:01445browse

How to Sort a 2D Array by Column Values in C  ?

Sorting a 2D Array by Column Values Using Built-in Functions or Custom Methods in C

In C , unlike Java, there is no direct built-in function that can sort a 2D array by specific column values. However, various approaches can be employed to achieve this desired sorting behavior.

Using C Standard Library Functions

One method involves utilizing the std::qsort function, which is a generic sorting algorithm. By defining a custom comparator function, you can control the sorting logic based on the column values. Here's an example implementation:

<code class="cpp">#include <iostream>
#include <algorithm>

int compareArrayColumns(int **lhs, int **rhs) {
  return (*lhs)[0] < (*rhs)[0];
}

int main() {
  int arr[][2] = {
    {20, 11},
    {10, 20},
    {39, 14},
    {29, 15},
    {22, 23}
  };

  std::qsort(arr, 5, sizeof(*arr), (int (*)(const void *, const void *))compareArrayColumns);

  for (int i = 0; i < 5; ++i) {
    std::cout << arr[i][0] << " " << arr[i][1] << std::endl;
  }

  return 0;
}</code>

In this code, the compareArrayColumns function is used to compare the first column values of each array row. The sorting is performed using the std::qsort function, and the sorted 2D array is printed.

Using Custom Methods

Another approach involves creating custom sorting functions that implement the desired logic. Here's an example implementation using a bubble sort:

<code class="cpp">#include <iostream>

void bubbleSortByColumn(int arr[][2], int size) {
  for (int i = 0; i < size; ++i) {
    for (int j = 0; j < size - 1; ++j) {
      if (arr[j][0] > arr[j + 1][0]) {
        int temp[2];
        temp[0] = arr[j][0];
        temp[1] = arr[j][1];
        arr[j][0] = arr[j + 1][0];
        arr[j][1] = arr[j + 1][1];
        arr[j + 1][0] = temp[0];
        arr[j + 1][1] = temp[1];
      }
    }
  }
}

int main() {
  int arr[][2] = {
    {20, 11},
    {10, 20},
    {39, 14},
    {29, 15},
    {22, 23}
  };

  int size = sizeof(arr) / sizeof(arr[0]);
  bubbleSortByColumn(arr, size);

  for (int i = 0; i < size; ++i) {
    std::cout << arr[i][0] << " " << arr[i][1] << std::endl;
  }

  return 0;
}</code>

In this code, the bubbleSortByColumn function implements a simple bubble sort algorithm that iterates through the array and swaps rows based on the first column values. The sorted 2D array is then printed.

The choice of approach depends on the specific requirements and performance considerations of your application.

The above is the detailed content of How to Sort a 2D Array by Column Values in C ?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn