0% found this document useful (0 votes)
8 views6 pages

Assignment 1 Code

The document is a C++ program that manages student records, allowing for data input, sorting, and searching functionalities. It defines a 'student' structure and implements various sorting algorithms (Bubble, Insertion, Quick) and search methods (linear and binary) for handling student data. Users can input student details and choose operations to sort or search the records based on roll number, name, or SGPA.

Uploaded by

riddhidhere2010
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)
8 views6 pages

Assignment 1 Code

The document is a C++ program that manages student records, allowing for data input, sorting, and searching functionalities. It defines a 'student' structure and implements various sorting algorithms (Bubble, Insertion, Quick) and search methods (linear and binary) for handling student data. Users can input student details and choose operations to sort or search the records based on roll number, name, or SGPA.

Uploaded by

riddhidhere2010
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/ 6

#include<iostream>

#include<string.h>

using namespace std;

int const size = 4;

struct student {

int rollno;

char stdname[20];

float SGPA;

};

void accept(struct student list[4]);

void display(struct student list[4]);

void displayTop(struct student list[4]);

void bubbleSort(struct student list[4]);

void insertSort(struct student list[4]);

void quickSort(struct student list[4], int, int);

void search(struct student list[4]);

void binarysearch(struct student list[4]);

int main() {

int ch, i;

struct student data[4];

accept(data);

cout << "\n 1: Bubble Sort";

cout << "\n 2: Insertion Sort";


cout << "\n 3: Quick Sort";

cout << "\n 4: Search";

cout << "\n 5: Binary Search";

cout << "\n Select your choice: ";

cin >> ch;

switch(ch) {

case 1:

bubbleSort(data);

display(data);

break;

case 2:

insertSort(data);

display(data);

break;

case 3:

quickSort(data, 0, size-1);

displayTop(data);

break;

case 4:

search(data);

break;

case 5:

binarysearch(data);

break;

default:

cout << "Invalid choice...";

}
void accept(struct student list[4]) {

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

cout << "Enter rollno, stdname and SGPA: ";

cin >> list[i].rollno >> list[i].stdname >> list[i].SGPA;

void display(struct student list[4]) {

cout << "\n\nRollno\tName\tSGPA\n";

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

cout << "\n" << list[i].rollno << "\t" << list[i].stdname << "\t" << list[i].SGPA;

void displayTop(struct student list[4]) {

cout << "\n\nRollno\tName\tSGPA\n";

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

cout << "\n" << list[i].rollno << "\t" << list[i].stdname << "\t" << list[i].SGPA;

void bubbleSort(struct student list[4]) {

struct student temp;

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

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

if(list[j].rollno > list[j+1].rollno) {

temp = list[j];

list[j] = list[j+1];
list[j+1] = temp;

void insertSort(struct student list[4]) {

struct student temp;

for(int k = 1; k < size; k++) {

temp = list[k];

int j = k-1;

while(strcmp(list[j].stdname, temp.stdname) > 0 && j >= 0) {

list[j+1] = list[j];

--j;

list[j+1] = temp;

void quickSort(struct student list[4], int first, int last) {

int pivot, i, j;

struct student temp;

if(first < last) {

pivot = first;

i = first;

j = last;

while(i < j) {

while(list[i].SGPA >= list[pivot].SGPA && i < last)

i++;
while(list[j].SGPA < list[pivot].SGPA)

j--;

if(i < j) {

temp = list[i];

list[i] = list[j];

list[j] = temp;

temp = list[pivot];

list[pivot] = list[j];

list[j] = temp;

quickSort(list, first, j-1);

quickSort(list, j+1, last);

void search(struct student list[4]) {

float SGPA;

cout << "\nEnter SGPA: ";

cin >> SGPA;

cout << "\n\nRollno\tName\tSGPA\n";

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

if(SGPA == list[i].SGPA)

cout << "\n" << list[i].rollno << "\t" << list[i].stdname << "\t" << list[i].SGPA;

void binarysearch(struct student list[4]) {

int lower = 0, upper = size-1, mid;


char search[80];

cout << "\n Enter name of student you want to search: ";

cin >> search;

mid = (lower + upper) / 2;

while(lower <= upper) {

if(strcmp(list[mid].stdname, search) < 0)

lower = mid + 1;

else if(strcmp(list[mid].stdname, search) == 0) {

cout << "\n" << list[mid].rollno << "\t" << list[mid].stdname << "\t" << list[mid].SGPA;

break;

} else

upper = mid - 1;

mid = (lower + upper) / 2;

if(lower > upper)

cout << search << " not found in the list";

You might also like