#include<stdio.h> /* * Implemented by Arjun Sunel. */ // Method to swap two integers by reference. void Swap(int *first, int *second) { int temp = *first; *first = *second; *second = temp; } // Bubble Sort Method void BubbleSort(int array[], int size, int swapped) { if(swapped == 0 || size == 0) { return; } int i; for(i = 0; i < size - 1; i++) { swapped = 0; if( array[i] > array[i + 1] ) { Swap(&array[i], &array[i + 1]); swapped = 1; } } BubbleSort(array, size-1, swapped); } // Entry point of the program int main() { int array[] = {5, -12, 14, 7}; int arraySize = 4; int index; printf("Before sorting : \n"); for(index = 0; index <= arraySize - 1; index++) { printf("%d ", array[index]); } int swapped = 1; BubbleSort(array, arraySize, swapped); printf("\nAfter sorting : \n"); for(index = 0; index <= arraySize - 1; index++) { printf("%d ", array[index]); } printf("\n"); }
Sunday, 14 December 2014
Recursive BubbleSort for Array of Integers
Labels:
Sorting
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment