Sunday, March 13, 2011

C program for bubble sorting

Program
#include <stdio.h>

#define SIZE 5

int main()
{
  int a[SIZE],b[SIZE];
  int temp,i,j;

  printf("\nBubble Sorting\n");
  printf("\nEnter the %d numbers: ",SIZE);

  for(i=0; i<SIZE; i++)
    scanf("%d",&a[i]);

  for(i=0; i<SIZE; i++)
    {
      for(j=SIZE-1; j>i; j--)
 {
   if (a[j-1]>a[j])
     {
       temp = a[j-1];
       
       a[j-1] = a[j];
       
       a[j] = temp;       
     }
 }
    }

  printf("\nSorted numbers: \n");
  for(i=0;i<SIZE;i++) printf("%d ",a[i]);

  printf("\n\nIf you need to sort more than %d numbers, change SIZE in porogram\n\n",SIZE);
  
}
Compilation, Run and Output
[sreedhar@manchu2 cprograms]$ gcc bubble_sort.c -o bubble_sort
[sreedhar@manchu2 cprograms]$ ./bubble_sort 

Bubble Sorting

Enter the 5 numbers: 8 3 16 24 19

Sorted numbers: 
3 8 16 19 24 

If you need to sort more than 5 numbers, change SIZE in porogram

[sreedhar@manchu2 cprograms]$ ./bubble_sort 

Bubble Sorting

Enter the 5 numbers: 6 3 15 27 1

Sorted numbers: 
1 3 6 15 27 

If you need to sort more than 5 numbers, change SIZE in porogram

[sreedhar@manchu2 cprograms]$

No comments:

PBS Script Generator: Interdependent dropdown/select menus in Javascript

PBS SCRIPT GENERATOR
SH/BASH TCSH/CSH
Begin End Abort

About Me

LA, CA, United States
Here I write about the battles that have been going on in my mind. It's pretty much a scribble.

Sreedhar Manchu

Sreedhar Manchu
Higher Education: Not a simple life anymore