Thread: simple bubble sort

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    67

    simple bubble sort

    ive tried to implement a simple bubble sort on 8 items in an array however whenever i execute it i get random values out of memory, here is my code
    Code:
     
    #include<stdio.h>
    #include<stdlib.h>
    #include<ctype.h>
    
    int main(void)
    {
        int a[]={1,2,3,4,5,6,7,8};
        printf("%s",a);
        bubble(a,8);
        printf("%s",a);
    }
    
    int bubble(int a[],int n)
    {
        int i,j;
         for(i=0; i<n-1;++i)
         for(j=n-1;i<j;--j)
         if(a[j-1]>a[j])
         swap(&a[j-1],&a[j]);
    	
    }
     
     int swap(int *p, int *q)
     {
         int temp;
         
        temp=*p;
        *p=*q;
        *q=temp;
    }
    where have i gone wrong?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Your functions aren't prototyped, both bubble and swap return nothing, so void would be a better return value, and a is not a string, so the %s format modifier is a poor choice. You really need to use a loop and %d. It's also hard to tell how well the sort works when the input is already sorted.
    Code:
    #include<stdio.h>
    
    void bubble(int a[],int n);
    void swap(int *p, int *q);
    
    int main(void)
    {
      int a[]={7,6,5,4,3,2,1};
      int i;
    
      for (i = 0; i < 7; i++)
        printf("%d ",a[i]);
      printf("\n");
      bubble(a,8);
      for (i = 0; i < 7; i++)
        printf("%d ",a[i]);
      printf("\n");
    }
    
    void bubble(int a[],int n)
    {
      int i,j;
      for(i=0; i<n-1;++i)
        for(j=n-1;i<j;--j)
          if(a[j-1]>a[j])
            swap(&a[j-1],&a[j]);
    }
    
    void swap(int *p, int *q)
    {
      int temp;
    
      temp=*p;
      *p=*q;
      *q=temp;
    }
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    67
    thanks , are the function prototypes necessarry?

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >are the function prototypes necessarry?
    No, but functions must be declared before you use them. This means either providing the declaration in the form of a prototype, or defining the function before its use. An equivalent solution without prototypes would be:
    Code:
    #include<stdio.h>
    
    void swap(int *p, int *q)
    {
      int temp;
    
      temp=*p;
      *p=*q;
      *q=temp;
    }
    
    void bubble(int a[],int n)
    {
      int i,j;
      for(i=0; i<n-1;++i)
        for(j=n-1;i<j;--j)
          if(a[j-1]>a[j])
            swap(&a[j-1],&a[j]);
    }
    
    int main(void)
    {
      int a[]={7,6,5,4,3,2,1};
      int i;
    
      for (i = 0; i < 7; i++)
        printf("%d ",a[i]);
      printf("\n");
      bubble(a,8);
      for (i = 0; i < 7; i++)
        printf("%d ",a[i]);
      printf("\n");
    }
    A definition is also a declaration, so the requirement that swap be declared before its use in bubble and bubble be declared before its use in main is met. Of course, now the code is order dependent. If you move swap below bubble then it won't work.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Nov 2004
    Posts
    67
    thanks for the explanation

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do I bubble sort alphabetically?
    By arih56 in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2008, 02:30 AM
  2. help with debug (bubble sort algorithm)
    By lbraglia in forum C Programming
    Replies: 5
    Last Post: 10-19-2007, 05:24 PM
  3. Bubble sort error
    By swgh in forum C Programming
    Replies: 2
    Last Post: 08-05-2007, 11:13 AM
  4. help, bubble sort function
    By Crcullen3916 in forum C++ Programming
    Replies: 8
    Last Post: 05-01-2007, 07:00 AM
  5. Bubble Sort... which type?
    By gflores in forum C++ Programming
    Replies: 8
    Last Post: 08-15-2004, 04:48 AM