0% found this document useful (0 votes)
26 views3 pages

Bubblesort

Bubble sort code

Uploaded by

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

Bubblesort

Bubble sort code

Uploaded by

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

CODE:-

#include<iostream>
#include<string.h>
using namespace std;

// Define a structure to store student details


typedef struct student {
int rn; // Roll number of the student
char name[20]; // Name of the student
float marks; // Marks obtained by the student
} stud;

// Function declarations
void create(stud s[20], int n); // To create student records
void display(stud s[20], int n); // To display student records
void bubblesort(stud s[20], int n); // To perform bubble sort on student
records
int main() {
stud s[20]; // Array to store up to 20 students
int ch, n;

do {
// Menu options for the user
cout << "\n Creating Student Database:-";
cout << "\n 1) Create student database ";
cout << "\n 2) Display student records ";
cout << "\n 3) Bubble sort ";
cout << "\n 4) Exit";
cout << "\n Enter your choice: ";
cin >> ch; // Input the user's choice

switch (ch) {
case 1:
cout << "\n Enter the number of records=";
cin >> n; // Input the number of records
create(s, n); // Create student records
break;
case 2:
display(s, n); // Display student records
break;
case 3:
bubblesort(s, n); // Perform bubble sort
cout << "\n Records sorted successfully!";
break;
case 4:
cout << "\n Exiting program.";
break;
default:
cout << "\n Invalid choice!! Re-enter your choice" << endl; //
Handle invalid choices
}
} while (ch != 4); // Repeat until the user chooses to exit

return 0; // End of program


}
// Function to create student records
void create(stud s[20], int n) {
for (int i = 0; i < n; i++) {
cout << "\n Enter the roll no.: ";
cin >> s[i].rn; // Input roll number
cout << " Enter name: ";
cin >> s[i].name; // Input name
cout << " Enter marks: ";
cin >> s[i].marks; // Input marks
}
}

// Function to display student records


void display(stud s[20], int n) {
cout << "\n" << "\t" << "Roll no." << "\t" << "Name" << "\t" << "Marks";
for (int i = 0; i < n; i++) {
cout << "\n";
cout << "\t";
cout << "\t";
cout << "\t" << s[i].rn << "\t" << s[i].name << "\t" << s[i].marks;
}
}

// Bubble sort function


void bubblesort(stud s[20], int n) {
for (int i = 1; i < n; i++) {
for (int j = 0; j < n - i; j++) {
if (s[j].rn > s[j + 1].rn) {
// Swap records if they are in the wrong order
stud temp = s[j];
s[j] = s[j + 1];
s[j + 1] = temp;
}
}
}
}

OUTPUT:-

Creating Student Database:-


1) Create student database
2) Display student records
3) Bubble sort
4) Exit
Enter your choice: 1

Enter the number of records=4

Enter the roll no.: 21


Enter name: Alex
Enter marks: 45

Enter the roll no.: 12


Enter name: cris
Enter marks: 66

Enter the roll no.: 18


Enter name: Natasha
Enter marks: 50

Enter the roll no.: 8


Enter name: carol
Enter marks: 77

Creating Student Database:-


1) Create student database
2) Display student records
3) Bubble sort
4) Exit
Enter your choice: 2

Roll no. Name Marks


21 Alex 45
12 cris 66
18 Natasha 50
8 carol 77

Creating Student Database:-


1) Create student database
2) Display student records
3) Bubble sort
4) Exit
Enter your choice: 3

Records sorted successfully!

Creating Student Database:-


1) Create student database
2) Display student records
3) Bubble sort
4) Exit
Enter your choice: 2

Roll no. Name Marks


8 carol 77
12 cris 66
18 Natasha 50
21 Alex 45

Creating Student Database:-


1) Create student database
2) Display student records
3) Insertion sort
4) Exit
Enter your choice: 4

Exiting program.

You might also like