Tuesday, September 13, 2011

C code for bubble sort using pointers



//UVA WELLASSA UNIVERSITY
//SORTING TECHNIQUES - C DATA STRUCTURE
//Iimplement Bubble Sort Technique using pointers.
//Program by:- EDIRISINGHE E.D.M.
//TESTED:- OK



#include<stdio.h>


int main(){    //main function
    int a[5];
    int i,temp,j,k;     //variables for loops
    int *arrPtr=a;         //assign array to the pointer
    for(i=0;i<5;i++)    //for loop for getting values to the array
    {
        printf("Enter array values : ");
        scanf("%d",&a[i]);
    }            //end of for loop
       
   
    for(k=0;k<5;k++)    //loops for sorting
    {
        for(j=0;j<4-k;j++)
        {
            if(*(arrPtr+j)>*(arrPtr+j+1))
            {
                temp=*(arrPtr+j);
                *(arrPtr+j)=*(arrPtr+j+1);
                *(arrPtr+j+1)=temp;
           
            }
        }
    }    //end of sorting
   
   
    printf("Sorted array values : \n");
    for(i=0;i<5;i++){    //loop for printing sorted array
       
        printf("%d ",a[i]);
        }//end of printing for loop
    printf("\n");
}//end of main function

No comments:

linkwithin

go