Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class merg{
- int arr[15] = {};
- public:
- void input(int n){
- for(int i = 0; i < n; i++){
- cout << "Enter salary" << i + 1 << ": " << endl;
- cin >> arr[i];
- }
- }
- void merge(int l, int m, int r) {
- int i, j, k, nl, nr;
- nl = m-l+1; nr = r-m;
- int leftarr[nl], rightarr[nr];
- for(i = 0; i<nl; i++)
- leftarr[i] = arr[l+i];
- for(j = 0; j<nr; j++)
- rightarr[j] = arr[m+1+j];
- i = 0; j = 0; k = l;
- while(i < nl && j<nr) {
- if(leftarr[i] <= rightarr[j]) {
- arr[k] = leftarr[i];
- i++;
- }else{
- arr[k] = rightarr[j];
- j++;
- }
- k++;
- }
- while(i<nl) {
- arr[k] = leftarr[i];
- i++; k++;
- }
- while(j<nr) {
- arr[k] = rightarr[j];
- j++; k++;
- }
- }
- void mergesort(int start, int end){
- if(start < end){
- int middle = (start + end)/2;
- mergesort(start, middle);
- mergesort(middle + 1, end);
- merge(start, middle, end);
- }
- else{
- return;
- }
- }
- void display(int n){
- if(n < 5){
- cout << "Top " << n << " employee salary: " << endl;
- for(int i = n - 1; i >= 0; i--){
- cout << " " << arr[i];
- }
- }
- else{
- cout << "Top 5 employee salary: " << endl;
- for(int i = n - 1; i > n - 6; i--){
- cout << " " << arr[i];
- }
- }
- }
- };
- int main(){
- merg n;
- int num = 0;
- cout << "Enter no of employees (<15): " << endl;
- cin >> num;
- n.input(num);
- n.mergesort(0, num-1);
- n.display(num);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement