5 Replies - 231 Views - Last Post: 07 February 2012 - 05:50 AM Rate Topic: -----

Topic Sponsor:

#1 praditmodi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 30-November 11

Bubble Sort/ Pointers

Posted 06 February 2012 - 07:54 PM


#include<stdio.h>
#include<stdlib.h>
int main () 
{
int *array[] = { 10, 7, 15, 25, 11, 37, 12, 44, 30, 14};
int n =10;
sort (array,10);
}

int sort(int *a, int x)
{int temp=0,i, j;
        for( i=0; i<x;i++)
        {
                for ( j= 0 ; j<x-1;j++)
                {
                        if(*(a+j)>*(a+j+1))
                        {
                                 temp=*(a+j+1);
                                *(a+j)=*(a+j+1);
                                *(a+j+1)=temp;
                                printf("%d\n",a[j]);
                        }
                }
        }

}



My output shows all 0's ? can some one enlighten me on this ? :s

Is This A Good Question/Topic? 0
  • +

Replies To: Bubble Sort/ Pointers

#2 Aphex19  Icon User is offline

  • Born again Pastafarian
  • member icon

Reputation: 469
  • View blog
  • Posts: 1,589
  • Joined: 02-August 09

Re: Bubble Sort/ Pointers

Posted 06 February 2012 - 08:43 PM

temp=*(a+j+1);
*(a+j)=*(a+j+1);
*(a+j+1)=temp;


This will produce two elements with both the same value, namely, the value of "*(a+j+1)". I think you intend to store "*(a+j)" in "temp", rather than "*(a+j+1)".

e.g.
temp=*(a+j);
*(a+j)=*(a+j+1);
*(a+j+1)=temp;


Changing that, the code runs fine (compiled in gcc) and "sort" seems to sort the integers correctly.

One thing to note, you can easily swap the values of two integers without a temp variable using the xor trick. It could potentially be more efficient too.

e.g.
a[j] ^= a[j+1];
a[j+1] ^= a[j];
a[j] ^= a[j+1];

Was This Post Helpful? 1
  • +
  • -

#3 praditmodi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 30-November 11

Re: Bubble Sort/ Pointers

Posted 06 February 2012 - 09:05 PM

I get this as my output when I print the array in the function itself ?
: 0
0
0
0
0
7
10
11
15
25
#include<stdio.h>
#include<stdlib.h>
int main () 
{
int *array[] = { 10, 7, 15, 25, 11, 37, 12, 44, 30, 14};
int n =10,i;
sort (array,10);
}

int sort(int *a, int x)
{int temp=0,i, j;
        for( i=0; i<x;i++)
        {
                for ( j= 0 ; j<x-1;j++)
                {
                        if(*(a+j)>*(a+j+1))
                        {
                                 temp=*(a+j);
                                *(a+j)=*(a+j+1);
                                *(a+j+1)=temp;

                        }
                }
        }
for(i=0;i<x;i++)
{printf("%d\n",a[i]);
}
}


Was This Post Helpful? 0
  • +
  • -

#4 Aphex19  Icon User is offline

  • Born again Pastafarian
  • member icon

Reputation: 469
  • View blog
  • Posts: 1,589
  • Joined: 02-August 09

Re: Bubble Sort/ Pointers

Posted 06 February 2012 - 09:20 PM

Can't say I have the same issue. Compiled with GCC, this is the output your code gives.

Quote

7
10
11
12
14
15
25
30
37
44


What OS and compiler are you using?

This post has been edited by Aphex19: 06 February 2012 - 09:23 PM

Was This Post Helpful? 0
  • +
  • -

#5 praditmodi  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 37
  • Joined: 30-November 11

Re: Bubble Sort/ Pointers

Posted 06 February 2012 - 09:33 PM

It works perfectly in devC++ thanks a ton ! :-)
Was This Post Helpful? 0
  • +
  • -

#6 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 3756
  • View blog
  • Posts: 9,707
  • Joined: 16-October 07

Re: Bubble Sort/ Pointers

Posted 07 February 2012 - 05:50 AM

You have a couple of true errors here. This is simply wrong int *array[] = {. You're only getting away with it because your compiler is sloppy. You also need to forward declare that sort, which should be an error.

Interesting that you didn't use pedantic pointer notation on your print. Shame it's not actually a bubble sort...

You got me thinking about how you might actually make use of silly pointer notation for this. Because the compares are adjacent, you can get clever about it.

So, for the silly:
#include <stdio.h>
#include <stdlib.h>

// you need to forward declare this
void sort(int *, int);
void show(int *, int);

int main () {
	int array[] = { 10, 7, 15, 25, 11, 37, 12, 44, 30, 14 };
	int size = sizeof(array)/sizeof(*array);
	show(array, size);
	sort(array, size);
	show(array, size);
	return 0;
}

void show(int *a, int x) {
	while(x-- > 0) { printf("%d ", *a++); }
	printf("\n");
}

void sort(int *a, int x) {
	int flag=1;
	while(flag) {
		int i = x, *p = a;
		flag = 0;
		for (; i>0; i--, p++) {
			int *p2 = p+1;
			if(*p > *p2) {
				int temp = *p;
				*p = *p2;
				*p2 = temp;
				flag = 1;
			}
		}
	}
}



Hope this helps.

This post has been edited by baavgai: 07 February 2012 - 05:50 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1