Bubble Sort
/*******************************************************************************/
#include<stdio.h>
void swap(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
void bubbleSort(int arr[], int n) {
int swapped = 1;
while (swapped) {
swapped = 0;
for (int i = 0; i < n - 1; i++) {
if (arr[i] > arr[i + 1]) {
swap(&arr[i], &arr[i + 1]);
swapped = 1;
}
}
}
}
int main(){
int size,i;
printf("Enter size of the array: ");
scanf("%d",&size);
int arr[size];
printf("Enter %d elements: ",size);
for(i=0;i<size;i++)
scanf("%d",&arr[i]);
bubbleSort(arr,size);
printf("Sorted elements: ");
for(i=0;i<size;i++)
printf(" %d",arr[i]);
return 0;
}
Comments
Post a Comment