forked from lprimeroo/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BubbleSort.c
36 lines (36 loc) · 833 Bytes
/
BubbleSort.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <stdio.h>
// Function to swap elements
//Code is Written By Krishna
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
// bubble sort function
void bubbleSort(int array[], int n)
{
int i, j;
for (i = 0; i < n - 1; i++)
for (j = 0; j < n - i - 1; j++)
if (array[j] > array[j + 1])
swap(&array[j], &array[j + 1]);
}
// Function to print the elements of an array
void printArray(int array[], int size)
{
int i;
for (i = 0; i < size; i++)
printf("%d ", array[i]);
printf("n");
}
// Main Function
int main()
{
int array[] = {89, 32, 20, 113, -15};
int size = sizeof(array) / sizeof(array[0]);
bubbleSort(array, size);
printf("Sorted array: ");
printArray(array, size);
return 0;
}