468,752 Members | 1,421 Online

I am trying to create a multi-threaded bubblesort but continue to get nothing. Here is my code, can someone help. Thanks

Expand|Select|Wrap|Line Numbers
1. #include <stdio.h>
2. #include <stdlib.h>
4.
5. void *bubblesortup(int *a)
6.  {
7.  char i,j,hup;
8.
9.  for (i=0; i<30; i++)
10.
11.     for (j=0; j<30-1;j++)
12.
13.      if (a[j]> a[j+1])
14.        {
15.        hup = a[j+1];
16.        a[j+1]=a[j];
17.        a[j]=hup;
18.        }
19.  }
20.
21. void *bubblesortdn(int *a)
22. {
23. char n,o,hdn;
24.
25. for (n=30; n>0; --n)
26.   for (o=30; 0+1; --n)
27.
28.   if (a[o-1]>a[o])
29.     {
30.     hdn = a[o-1];
31.     a[o-1]=a[o];
32.     a[o]=hdn;
33.     }
34.  }
35.
36.
37.
38.
39.
40. int main(int argc, char *argv[])
41. {
44.   int k,l,i;
45.
46.   char a[] = {4,67,45,3,41,43,75,3,9,34,6,3,4,12,41,84,49,33,65,74,54,12,58,12,65,34,26,99,85,1};
49.
50. }
51.
Sep 26 '07 #1
1 9767
Banfa
9,057 Expert Mod 8TB
You use the magic number 30 in many places, this should be the size of the array a using a magic number like tghis is asking for trouble and errors later on.

The end condition for the j for loop is wrong, it can reduce by 1 for each iteration of the i for loop.

The end condition for the o for loop is no existant, this is an inifinite loop. This end condition should depend on n.

The bigest mistake though is that both threads simultaneously assess the array a. This is also asking for trouble and will likely not work and cause errors. In a multithreaded program it is important to ensure that any given piece of data is only accessed from 1 thread at a time otherwise data corruption can, and most probably will, occur.

I do not think the bubble sort algorithm is ideally suited to multithreaded resolution, it is too simple.
Sep 26 '07 #2