Vous êtes sur la page 1sur 14

DSA PROJECT

HOSTEL RANKING SYSTEM USING MERGE


SORT

Akarsh Srivastava 17BCI0091

HOSTEL RANKING SYSTEM:


It is a program that sorts the list of students given in the form of a file in the
order of their normalized cgpa. This order is the priority according to which
students will be sent to choose the rooms during room counselling. But before
that let us learn about the algorithm used, merge sort.

HISTORY OF SORTING ALGORITHMS

Since computing started, even though sorting problem have an easy and familiar
statement, it has always been interesting due to how complex it is to solve it.
Among the authors of early sorting algorithms around
1951 was Betty Holberton, who worked on ENIAC and UNIVAC.
Bubble sort is known since 1956. Comparison sorting algorithms have a need of Ω(n
log n) comparisons; and algorithms not relying on comparisons, like counting sort,
can have better accomplishment. Even though a lot of people think about sorting a
programme that is already solved, asymptotically optimal algorithms have been
known since the mid 20th century, invention of the new and beneficial ones is going
on, with the now widely used Timsort dating to 2002, and the library sort to 2006.
A variety of core algorithm concepts, like big O notation, divide and conquer
algorithms, data structures such as heaps and binary trees, randomized algorithms, best,
worst and average case analysis, time–space tradeoffs, and upper and lower bounds are
used extensively. These sorting algorithms are extensive in minor CS lectures, where
the abundance of algorithms for the problem provides a gentle introduction to such
concepts.

COMPARISON OF SORTING ALGORITHMS

In the table given, n is the number of the records that need to be sorted. The columns
"Average" and "Worst" provide with the time complexity in each case, considering that
the length of each key is not variable, and that therefore all comparisons, swaps, and
other required operations can happen in a time that is constant. Inside big O notation,
the run times and the memory requirements listed below are there,and hence the base of
the logarithms does not matter; the notation log2 n means (log n)2.
Here, The amount of auxiliary storage needed beyond that used by the list itself, under
the same assumption is denoted by “Memory”.

The comparison sorts are there, and so can’t perform better than O(n log n) in the
average or worst case.

Time complexity of other sorting algorithms

Worst-case running Best-case running Average-case running

Algorithm time time time

Selection sort Theta(n^2)Θ(n2) Theta(n^2)Θ(n2) Theta(n^2)Θ(n2)

Insertion sort Theta(n^2)Θ(n2) Theta(n)Θ(n) Theta(n^2)Θ(n2)


Theta(n \lg Theta(n \lg
Merge sort n)Θ(nlgn) n)Θ(nlgn) Theta(n \lg n)Θ(nlgn)

Theta(n \lg
Quicksort Theta(n^2)Θ(n2) n)Θ(nlgn) Theta(n \lg n)Θ(nlgn)

Divide-and-conquer
Many concepts are based on recursion, such as both merge sort and
quicksort and they employ a common algorithmic paradigm. The original
problem is solved by his paradigm, divide-and-conquer, and breaking the
problem into sub problems that are very similar.

Then solves the sub problems recursively, and then joins the solutions to
the sub problems to find the answer of the original problem. Because
divide-and-conquer answers sub problems recursively, the original
problem should be divided into smaller problems, and there must be a base
case for sub problems. Imagine a divide-and-conquer algorithm as having
three parts:

1. The sub problems are smaller instances of the same problem, and the
problem is divided in such a way.
2. Solve the sub problems by answering them recursively. If they are quite
small, solve the sub problems as base cases.
3. Join the answers of the sub problems into the solution for the original
answer.
4. It is easy to remember the steps of a divide-and-conquer algorithm as
divide, conquer, combine. Here's how to view one step, assuming that each
divide step creates two subproblems (though some divideand-conquer
algorithms create more than two):
If we expand out two more recursive steps, it looks like this:

0
Because divide-and-conquer creates at least two subproblems, a divide-
and-conquer algorithm makes multiple recursive calls.
Merge Sort

Time complexity derivation

CGPA RANKING PROGRAM


INPUT: Name, Registration number, branch, CGPA
PROCESSING: Using merge sort to sort the student list according to the
difference between the CGPA of students and the normalized CGPA of their
branch.
ALGORITHM USED: Merge Sort
DATA STRUCTURE USED: ARRAY
OUTPUT: Name, Registration number, branch and CGPA of students in sorted
order of the difference between their CPGA’s and normalized CGPA’s of their
branches in descending order.

PROGRAM CODE:
#include<fstream>
#include<iostream>
#define MAX 1000
#include<stdlib.h>
#include<string> using
namespace std;
EXPLANATION1: The structure Rank stores all the details of each student. The array of the given
structure stores all the students.
struct Rank
{
string name;
string regno; string
branch; float cgpa;
float newcgpa;

};
struct Rank students[MAX];

EXPLANATION 2: It is clearly not possible to give an input for each and every student since each
student has 4 inputs and there can be 1000-2000 inputs given. Hence we have used fstream so that
data can be input in the form of a file.
void Input(int n)
{ string
str;

ifstream bfile;
bfile.open("New.txt");
int i;
char* s;

for(i=0;i<n;i++)
{
bfile>>str;
students[i].name=str;
bfile>>str;
students[i].regno=str;
bfile>>str;
students[i].branch=str;
bfile>>str;

EXPLANATION-3: In the file, the cgpa of the student is stored as a string. But in order to sort the
file using cgpa we need it as a float. Therefore we use the strtof((str).c str(),0) method to convert
cgpa from string to float.
students[i].cgpa=strtof((str).c_str(),0);
if(students[i].cgpa>10||students[i].cgpa<0)
{
cout<<"Invalid Entry"<<endl;

exit(0);

}
}
EXPLANATION 4: The norm function finds the average cgpa of each branch in the input. This is
gives a measure of how difficult the courses in the branch are.
float norm(string b,int n)
{ int
i;
float number=0;
float sum=0;
for(i=0;i<n;i++)
{
if(students[i].branch==b)
{
sum=sum+students[i].cgpa;
number=number+1;
}

float avg = sum/number;


return avg;

EXPLANATION 5: The normalize functions subtracts the average found using norm from the cgpa
of the student according to his branch. Every branch has different levels of difficulties. For example,
the CSE branch is has more 9-pointers as compared to the mechanical branch. If we take the cgpa at
the face value, all the hostel rooms will always be given to the students of the CSE branch and the
Mechanical branch will be left with all the unwanted rooms.
void Normalize(int n)
{ int
i;
for(i=0;i<n;i++)
{
students[i].newcgpa=students[i].cgpa-norm(students[i].branch ,n);
}
}
EXPLANATION 6: Using Merge Sort. This is where we use our algorithm. We sort the array of
Rank, according to the difference obtained through the Normalize function. Hence the ones with the
higher difference I.e who are higher above the branch average are ranked higher and the one below
the branch average are ranked lower. How the algorithm works is explained above.
void merge(struct Rank arr[], int l, int m, int r)
{ int i, j,
k;
int n1 = m - l + 1;
int n2 = r - m;

/* create temp arrays */


struct Rank L[n1], R[n2];

/* Copy data to temp arrays L[] and R[] */


for (i = 0; i < n1;
i++) L[i] = arr[l +
i]; for (j = 0; j < n2;
j++)
R[j] = arr[m + 1+ j];

/* Merge the temp arrays back into arr[l..r]*/


i = 0; // Initial index of first subarray j
= 0; // Initial index of second subarray k
= l; // Initial index of merged subarray
while (i < n1 && j < n2)
{
if (L[i].newcgpa >= R[j].newcgpa)
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}

/* Copy the remaining elements of L[], if there


are any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}

/* Copy the remaining elements of R[], if there


are any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}

/* l is for left index and r is right index of the


sub-array of arr to be sorted */ void
mergeSort(struct Rank arr[], int l, int r)
{ if (l <
r)
{
// Same as (l+r)/2, but avoids overflow for
// large l and h
int m = l+(r-l)/2;

// Sort first and second halves


mergeSort(arr, l, m); mergeSort(arr,
m+1, r);

merge(arr, l, m, r);
}
}

/* UTILITY FUNCTIONS */
/* Function to print an array */
EXPLANATION 7: The printArray() function simply writes the sorted list onto a new file and
gives us the sorted list in the command line. void printArray(struct Rank A[], int size)
{
ofstream afile;
afile.open("New.txt");
int i;
string a;
int i;
cout<<"NAME"<<"\t\t"<<"REG. NO."<<"\t\t"<<"BRANCH"<<"\t\t"<<"RANK"<<endl;
for (i=0; i <=size-1; i++)
{cout<<A[i].name<<"\t\t"<<A[i].regno<<"\t\t"<<A[i].branch<<"\t\t"<<i+1<<endl;
afile<<A[i].name<<"\t\t"<<A[i].regno<<"\t\t"<<A[i].branch<<"\t\t"<<i+1<<endl;

}
}

/* Driver program to test above functions */


This is the main() function where we use all the functions we defined above.
int main()
{
cout<<"*****HOSTEL RANKING SYSTEM******"<<endl;
int a;
cout<<"Enter the number of students"<<endl;
cin>>a;

Input(a);
Normalize(a);

mergeSort(students,0,a-1);

printArray(students,a);

}
SNAPSHOTS OF THE PROGRAM:
INITIAL FILE INSERTED INTO THE PROGRAM
OUTPUT GENERATED IN COMMAND LINE AFTER EXECUTION
THE RESULT FILE:

Vous aimerez peut-être aussi