PRN:1272241017
ADVANCE DATA STRUCTURE
C PROGRAMMING-LAB
1. Write a c program to implement bubble sort.
Codebase:
#include <stdio.h>
int main() {
int arr[10], n, a, b;
printf("Enter the number of elements:\n");
scanf("%d", &n);
printf("Enter %d elements:\n", n);
for (a = 0; a < n; a++) {
scanf("%d", &arr[a]);
}
for (a = 0; a < n - 1; a++) {
for (b = 0; b < n - a - 1; b++) {
int temp = arr[b];
if (arr[b] > arr[b + 1]) {
arr[b] = arr[b + 1];
arr[b + 1] = temp;
}
}
}
printf("Sorted array:\n");
for (a = 0; a < n; a++) {
printf("%d\n", arr[a]);
}
return 0;
}
Output:
2. Write a C program to implement selection sort.
Codebase:
#include <stdio.h>
int main() {
int arr[10]={6,12,0,18,11,99,55,45,34,2};
int n=10;
int i, j, position, swap;
for (i = 0; i < (n - 1); i++) {
position = i;
for (j = i + 1; j < n; j++) {
if (arr[position] > arr[j])
position = j;
}
if (position! = i) {
swap = arr[i];
arr[i] = arr[position];
arr[position] = swap;
}
}
for (i = 0; i < n; i++)
printf("%d\t", arr[i]);
return 0;
}
Output:
Scanned with ACE Scanner
Scanned with ACE Scanner