I'm working on a program that takes a given number of inputs and gives a sorted output(ascending/descending) using the shared memory in C++ using the fork() system call. I have created a shared memory segment, assigned a key and even put values in it. The values are taken as input in the child process and I can easily output them from the array in the parent process. However, my main issue is with the sorting. I want to sort the data in the shared memory in child process before getting it printed from the parent process.I am trying to use bubble sort to do it but for some reason, the bubble sort doesn't sort. I can print the values in the shared memory that I have taken as input in array easily but can't get the sorting to work. How do I get the sorting to work? Any help would be appreciated.
for (i = 0; i < n - 1; i++) {
for (j = 0; j < n - i - 1; i++) {
if (shared_memory->arr[j] > shared_memory->arr[j + 1]) {
int temp = shared_memory->arr[j];
shared_memory->arr[j] = shared_memory->arr[j + 1];
shared_memory->arr[j + 1] = temp;
}
}
In the inner loop you write for(j=0;j<n-i-1;i++) but you increment i instead of incrementing j. If you increment j like for(j=0;j<n-i-1;j++) that will fix it.