I have an array of numbers, a[n], and I want to sort it very fast, but I want to save the original positions. so I think I should use a 2D array a[2][n], so a[0][n] is for the initial positions and a[1][n] for the values. so I want to sort this array based on the second row.
for example, my array is
a[2][5] = [{ 0, 1, 2, 3, 4 }, { 10, 30, 40, 20, 50 }];
and after sorting I want
a[2][5] = [{ 0, 3, 1, 2, 4 }, { 10, 20, 30, 40, 50 }];
I tried to change merge sort so that it sorts the first row based on second row (and also sort second row) but there is segmentation fault in line 14, here is my code
#include <stdio.h>
#include <stdlib.h>
void merge(int arr[][201], int l, int m, int r) {
int i, j, k;
int n1 = m - l + 1;
int n2 = r - m;
/* create temp arrays */
int L[2][n1], R[2][n2];
/* Copy data to temp arrays L[] and R[] */
for (i = 0; i < n1; i++)
L[1][i] = arr[1][l + i];
L[0][i] = arr[0][l + i];
for (j = 0; j < n2; j++)
R[1][j] = arr[1][m + 1 + j];
R[0][j] = arr[0][m + 1 + j];
/* Merge the temp arrays back into arr[l..r]*/
i = 0; // Initial index of first subarray
j = 0; // Initial index of second subarray
k = l; // Initial index of merged subarray
while (i < n1 && j < n2) {
if (L[1][i] <= R[1][j]) {
arr[1][k] = L[1][i];
arr[0][k] = L[0][i];
i++;
} else {
arr[1][k] = R[1][j];
arr[0][k] = R[0][j];
j++;
}
k++;
}
/* Copy the remaining elements of L[], if there
are any */
while (i < n1) {
arr[0][k] = L[0][i];
arr[1][k] = L[1][i];
i++;
k++;
}
/* Copy the remaining elements of R[], if there
are any */
while (j < n2) {
arr[1][k] = R[1][j];
arr[0][k] = R[0][j];
j++;
k++;
}
}
/* l is for left index and r is right index of the
sub-array of arr to be sorted */
void mergeSort(int arr[][201], int l, int r) {
if (l < r) {
// Same as (l+r)/2, but avoids overflow for
// large l and h
int m = l + (r - l) / 2;
// Sort first and second halves
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
}
}
void get_array(int length, int array[]) {
for (int i = 0; i < length; i++) {
scanf("%d", &array[i]);
}
}
int main() {
int n;
scanf("%d", &n);
int packages[2][n];
for (int i = 0; i < n; i++) {
packages[0][i]= i;
}
get_array(n, packages[1]);
mergeSort(packages, 0, n);
return 0;
}
I just want to solve this problem, I'm OK with using another function.