bubble sort algorithm - displaying content
Hello,
I am working on a bubble sort program and it is kicking my butt. I have been learning C for about 6 weeks now 1-2 times a week when I have the time away from work and family.
One of the items I am working on is a bubble sort program. I have listened to lecture after lecture, read and followed the tutorial (which did not work for me) on cprogramming.com and looked at multiple other examples.
I am not good at understanding techinese yet. I need some help displaying this. I have two programs. I am working in linux.
My first program generates psuedo random numbers. I pipe the generated numbers over to the find program which contains the bubblesort algorithm to look for a specfic number and display those numbers in order.
The numbers I am trying to sort are (using a seed for the psuedo random numbers):
17767
9158
39017
18547
56401
23807
37962
22764
7977
31949
22714
55211
16882
Now, the command for this is:
./generate 13 1
Now I use the pipe character to send those numbers to the find program as such ./generate 13 1 | ./find 9158
The find program has the bubble sort algorithm. I think the algorithm is correct but I cannot get this to diplay correctly to save my life.
Code:
void
sort(int values[], int n)
{
// O(n^2) sort
for(int i=n-2;i>0;i--) {
for(int j=0;j<i;j++) {
if(values[j]>values[j+1]) {
int tempholder=values[j+1];
values[j+1]=values[j];
values[j]=tempholder;
printf("%d\n",values[j]);
}
}
}
return;
}
Now it should display
7977
9158
16882
17767
18547
22714
22764
23807
31949
37962
39017
55211
56401
Instead it displays:
9158
18547
23807
37962
22764
7977
31949
22714
55211
23807
37962
22764
7977
31949
22714
22764
7977
31949
22714
22764
7977
22714
7977
22714
7977
22714
7977
7977
My two biggest issues:
1. I don't quite understand what I am doing with the bubblesort even after everything that I have read. I understand it probably 70% of the way.
2. I know that I have the printf() in the wrong place but I have tried it in numerous places to no avail so I figure maybe I have programmed something wrong.
Any help would be greatly appreciated.
Thank you,
Wayne