0% found this document useful (0 votes)
3 views

advance_data_structure

The document contains C programs for implementing bubble sort and selection sort algorithms. The bubble sort program prompts the user to input elements, sorts them using nested loops, and prints the sorted array. The selection sort program initializes an array, sorts it by finding the minimum element in each iteration, and prints the sorted array.

Uploaded by

Fikirini Akbar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

advance_data_structure

The document contains C programs for implementing bubble sort and selection sort algorithms. The bubble sort program prompts the user to input elements, sorts them using nested loops, and prints the sorted array. The selection sort program initializes an array, sorts it by finding the minimum element in each iteration, and prints the sorted array.

Uploaded by

Fikirini Akbar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

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

You might also like