Bubble sort : Bubble Sort « Search Sort « C Tutorial

Home
C Tutorial
1.Language
2.Data Type
3.String
4.printf scanf
5.Operator
6.Statement
7.Array
8.Function
9.Structure
10.Pointer
11.Memory
12.Preprocessor
13.File
14.Data Structure
15.Search Sort
16.Wide Character String
17.assert.h
18.ctype.h
19.math.h
20.setjmp.h
21.signal.h
22.stdio.h
23.stdlib.h
24.string.h
25.time.h
26.wctype.h
C / ANSI-C
C++
C++ Tutorial
Visual C++ .NET
C Tutorial » Search Sort » Bubble Sort 
15.1.1.Bubble sort
#include <stdio.h>
#define MAX 10

void swap(int *x,int *y)
{
   int temp;
   temp = *x;
   *x = *y;
   *y = temp;
}
void bsort(int list[])
{
   int i,j;
   for(i=0;i<(MAX-1);i++){
      for(j=0;j<(MAX-(i+1));j++){
             if(list[j> list[j+1]){
                    swap(&list[j],&list[j+1]);
             }
      }
   }
}
void printlist(int list[])
{
   int i;
   printf("The elements of the list are: \n");
   for(i=0;i<MAX;i++)
      printf("%d\t",list[i]);
}

void main()
{
   int list[MAX];

   list[02; list[11; list[24; list[33; list[49;
   list[519; list[617; list[711; list[85; list[96;

   printf("The list before sorting is:\n");
   printlist(list);
   bsort(list);
   printf("The list after sorting is:\n");
   printlist(list);
}
The list before sorting is:
The elements of the list are:
2       1       4       3       9       19      17      11      5       6
The list after sorting is:
The elements of the list are:
1       2       3       4       5       6       9       11      17      19
15.1.Bubble Sort
15.1.1.Bubble sort
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.