C examples for Data Structure:Sort
Sort a Given List of Numbers Using a Bubble Sort
#include <stdio.h> int myArray[20];//from w ww . j a v a 2s . c o m int count = 10; void bubbleSort() { int temp; int i,j; int intSwap = 0; /* 0-false & 1-true */ for(i = 0; i < count-1; i++) { /* outer for loop begins */ intSwap = 0; for(j = 0; j < count-1-i; j++) { /* inner for loop begins */ if(myArray[j] > myArray[j+1]) { /* if statement begins */ temp = myArray[j]; myArray[j] = myArray[j+1]; myArray[j+1] = temp; intSwap = 1; } /* if statement ends */ } /* inner for loop ends */ if(!intSwap) { break; } } /* outer for loop ends */ } void main() { int i; printf("Enter the 10 number for bubble sort, separated by white spaces: \n"); for (i=0; i < count; i++) scanf("%d", &myArray[i]); fflush(stdin); bubbleSort(); printf("Sorted List: "); for(i = 0; i < count; i++) printf("%d ", myArray[i]); printf("\nThank you.\n"); }