0% found this document useful (1 vote)
234 views4 pages

Practical No:02: Harshvardhan Bhausaheb

The document outlines a lab course assignment focused on searching algorithms, specifically bubble sort and selection sort. It includes code examples for sorting an array of integers using bubble sort and reading student data from a file to sort by percentage using selection sort. The outputs demonstrate the sorted results for both algorithms.

Uploaded by

gostperson12
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
234 views4 pages

Practical No:02: Harshvardhan Bhausaheb

The document outlines a lab course assignment focused on searching algorithms, specifically bubble sort and selection sort. It includes code examples for sorting an array of integers using bubble sort and reading student data from a file to sort by percentage using selection sort. The outputs demonstrate the sorted results for both algorithms.

Uploaded by

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

Practical No :02

Name :- Boharde Harshvardhan Bhausaheb


Class:-S.Y.B.Sc.(Computer Science)
Div:-A Batch:- A
Roll No:- 10
Subject:- Lab Course based on CS-201-MJ-T
Title:- Searching Algorithms
Date:- /07/2025

Q1] Sort a random array of n integers (accept the value of n from user) in ascending order by
using bubble sort algorithm.
ANS:

#include<stdio.h>
void bubbleSort(int arr[],int n) {
for(int i = 0; i < n-1; i++) {
for(int j = 0; j < n-i-1;j++){
if(arr[j]>arr[j+1]) {
// Swap elements
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main(){
int n;
printf("Enter number of elements:");
scanf("%d", &n);
int arr[n];
printf("Enter %d elements:\n",n);
for(int i = 0; i < n; i++)
scanf("%d", &arr[i]);
bubbleSort(arr,n);
printf("Sorted array (Bubble Sort):\n");
for(int i = 0; i < n; i++)
printf("%d",arr[i]);
return 0;
}

OUTPUT:
Enter number of elements:5
Enter 5 elements:
20
1
-9
0
4
Sorted array (Bubble Sort):
-901420

Q2] Read the data from “Student.txt (Stud_name, Age, Percentage)” file and sort on Percentage
using selection sort.(Create a separate sorted file).
ANS:

PROGRAM:

#include<stdio.h>

#include<stdlib.h>

#include<string.h>

#define MAX 100

// Structure to hold student data

struct Student {

char name[50];
int age;

float percentage;

};

int main() {

struct Student students[MAX];

int n = 0;

FILE*file=fopen("student.txt","r");

if (file == NULL){

printf("Error opening file!\n");

return 1;

// Reading data from file

while (fscanf(file,"%s %d %f", students[n].name,&students[n].age, &students[n].percentage) == 3){

n++;

fclose(file);

// Selection Sort based on percentage

for (int i = 0;i < n - 1; i++){

int min_idx = i;

for (int j = i+1; j < n;j++){

if (students[j].percentage < students[min_idx].percentage){

min_idx = j;

// Swap

if (min_idx !=i){

struct Student temp = students[i];

students[i] = students[min_idx];

students[min_idx] = temp;

// Display sorted data

printf("\nSorted Student Data (by Percentage):\n");


printf("Name\tAge\tPercentage\n");

for (int i = 0; i < n; i++) {

printf("%s\t%d\t%.2f\n",students[i].name,students[i].age,students[i].percentage);

return 0;

OUTPUT:

Sorted Student Data (by Percentage):

Name Age Percentage

Alice 20 85.50

David 21 88.70

David 21 88.70

Charlie 19 92.10

You might also like