You are on page 1of 5

PROJECT 2: BUBBLE SORT

OVERVIEW:
In the program below we’re implementing the bubble sort algorithm using
array.

 Initially we declare a global array of size 100 as required.


 Then we create a function called “input” which takes an array and the no.
of elements ‘N’ we want to insert as arguments
 Input function inserts numbers into the array and returns it, if the numbers
exceed 100 an error is generated
 We create another function called “bubblesort” which takes an array and
the array size as arguments.
 Bubblesort function uses the given pseudo code to sort the given array in
ascending order. After sorting the array is returned.
 Final we create another function called “printArray” which takes array and
its size as arguments. It prints the elements of the given array.

CODE:

#include <stdio.h>>

int main()

int arr[100];

int N;

printf("Enter no. of elements you want to insert within the range of 100 : ");

scanf("%d", &N);
input(arr, N);

bubblesort(arr, N);

printArray(arr, N);

return 0;

int input(int array[100], int N)

int i;

if(N > 100)

printf("Error: Array Range Exceeded");

else

printf("Enter numbers: \n");

for(i=0; i<N; i++)

scanf("%d", &array[i]);

}
}

return array;

int bubblesort(int array[], int N )

int c,d,swap;

for (c = 0 ; c < N - 1; c++)

for (d = 0 ; d < N - c - 1; d++)

if (array[d] > array[d+1]) /* For decreasing order use < */

swap = array[d];

array[d] = array[d+1];

array[d+1] = swap;

return array;
}

void printArray(int arr[], int size)

int i;

printf(" Sorted Array elements are: ");

for(i = 0; i < size; i++)

printf("%d, ", arr[i]);

SAMPLE OUTPUT:
 If more than 100 elements are to be inserted the program stops.
 If less than 100 elements are inserted in any order the sorted array is
returned and printed.

You might also like